问题描述:

I am working like a social media application... but in my controller last else if not working for status upload. but file and video uload working nicely. what is the correct format for this. here is my controller

public function getclass(Request $request)

{

if (Input::has('post_comment'))

{

$status=Input::get('post_comment');

$commentBox=Input::get('comment_text');

$selectedStatus=Status::find($status);

$selectedStatus->comments()->create([

'comment_text'=>$commentBox,

'user_id'=>Auth::user()->id,

'status_id'=>$status

]);

Flash::message('Your comments has been posted');

return redirect(route('class'));

}

if(Input::has('status-text'))

{

$text=e(Input::get('status-text'));

$rules = [

'status_text'=>'required|string',

];

$validator = Validator::make($request->all(), $rules);

if(Input::hasFile('status_image_upload'))

{

$rules['status_image_upload'] = 'image';

$validator = Validator::make($request->all(), $rules);

if($validator->fails())

{

$image = $request->file('status_image_upload');

$imageName = str_random(8).'_'.$image->getClientOriginalName();

$image->move('status_images', $imageName);

$userStatus = new Status();

$userStatus -> status_text = $text;

$userStatus -> image_url = $imageName;

$userStatus -> type = 1;

$userStatus -> users_id = Auth::user()->id;

$userStatus -> save();

Flash::success('Your status has been posted');

return redirect(route('class'));

}

}

else if ($validator->fails())

{

$userStatus = new Status();

$userStatus -> status_text = $text;

$userStatus -> video_url = $request['video_url'];

$userStatus -> type = 2;

$userStatus -> users_id = Auth::user()->id;

$userStatus -> save();

Flash::success('Your status has been posted');

return redirect(route('class'));

}

else if($validator->fails())

{

$userStatus = new Status();

$userStatus -> status_text = $text;

$userStatus -> type = 3;

$userStatus -> users_id = Auth::user()->id;

$userStatus -> save();

Flash::success('Your status has been posted');

return redirect(route('class'));

}

}

return view('class',[

'posts'=>status::orderBy('id','DESC')->get()

]);

}

In this controller this code not working

else if($validator->fails())

{

$userStatus = new Status();

$userStatus -> status_text = $text;

$userStatus -> type = 3;

$userStatus -> users_id = Auth::user()->id;

$userStatus -> save();

Flash::success('Your status has been posted');

return redirect(route('class'));

}

网友答案:

That portion of code will never get executed. Because the else if block above your code i.e this part

else if ($validator->fails())
{
    $userStatus = new Status();
    $userStatus -> status_text = $text;
    $userStatus -> video_url = $request['video_url'];
    $userStatus -> type = 2;
    $userStatus -> users_id = Auth::user()->id;
    $userStatus -> save();

    Flash::success('Your status has been posted');
    return redirect(route('class'));
}

has kind of block it. You are using the same condition in both the else if block. This means if the above else if condition fails then the other will also fail. And the validation rules you are using in both conditions are also same.

相关阅读:
Top