路线或控制器在Laravel 5.2中不起作用

时间:2016-08-22 13:57:07

标签: laravel-5.2

首先,我使用路线和控制器发布状态视频链接上传文件评论。这是路线和控制器 这是我的路线:

Route::any('/class',[
'uses'=> 'classroom@getclass',
'as'=>'class',
]);

这是我的控制器:

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


return view('class',[
'posts'=>status::orderBy('id','DESC')->get()
]);}

这个位置很好用。 但是当我使用另一条路线和另一个控制器时,我现在面临的问题。 这是我的问题路线:

Route::post('/class',[
'uses'=>'classroomdata@showclassdata',
'as'=>'createclass',]);

我的控制员:

 public function showclassdata(Request $request)
{
    $randomnumber = rand(50001,1000000);

    $classrooms = new Classrooms();
    $classrooms->class_name = $request['class_name'];
    $classrooms->subject_name = $request['subject_name'];
    $classrooms->section = $request['section'];
    $classrooms->class_code = $randomnumber;
    $classrooms -> user_id = Auth::user()->id;
    $classrooms -> save();
    return view('class');
}

此时我的问题是数据插入。我有一个错误,错误是:未定义的变量:posts(查看:C:\ xampp \ htdocs \ myproject11 \ resources \ views \ class.blade.php) 为什么我要面对这个问题?

1 个答案:

答案 0 :(得分:0)

根据您的第二条评论,您将在 showclassdata 方法中返回没有'帖子'的视图。

比较

getclass方法:

return view('class',[
'posts'=>status::orderBy('id','DESC')->get()
]);}

showclassdata方法:

return view('class');

这就是你收到错误的原因。

解决方案以showclassdata方式返回您的帖子并显示。