为什么我的控制器不能在laravel中工作

时间:2016-08-01 16:41:11

标签: laravel-5.2

我的工作方式与社交媒体应用程序相似......但如果不在状态上传工作,请在我的控制器中使用。但文件和视频uload工作得很好。什么是正确的格式。这是我的控制器

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()
    ]);

}

在此控制器中,此代码无法正常工作

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'));
}

1 个答案:

答案 0 :(得分:0)

这部分代码永远不会被执行。因为代码上方的else if块,即此部分

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块中使用相同的条件。这意味着如果上述else if条件失败,则另一个条件也将失败。您在两种情况下使用的validation rules也是相同的。