如何修复NotFoundHttpException laravel?

时间:2015-12-26 15:53:31

标签: laravel laravel-5 laravel-5.1

大家好我是laravel 5.1的新手,我刚开始学习laravel。我有lalavel的问题我无法找到纠正它的方法我尝试搜索但我仍然不能。我想更新表单,但当我点击按钮更新其显示错误

NotFoundHttpException in Handler.php line 46: No query results for model [App\Course].

ModelNotFoundException in Builder.php line 129: No query results for model [App\Course].

这是我的代码:  CourseController.php

public function update(Request $request, $id)
    {
        $input = $request->all();
        $data = Course::findOrFail($id);
        $data->update($input);

        return redirect('course');
    }

和我的模型Course.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;



class Course extends Model
{
    protected $table = "course";
    protected $primaryKey = "course_id";

    protected $fillable=[
    'course_title',
    'course_code',
    'course_credite'
    ];
}

这是我的观点edite.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Hello World</title>
    <link rel="stylesheet" type="text/css" href="">
</head>
<body>
<div class="container">
    {!! Form::open(array('route'=>['course.update','$course->course_id'],'class'=>'jack','method'=>'PUT')) !!}

    {!! Form::label('Course_title','Course Title',array('class'=>'aswe')) !!}
    {!! Form::text('course_title', $value=$course->course_title, $att = array('class'=>'blu','id'=>'single','required')) !!}

    {!! Form::label('Course_code','Code_title',array('class'=>'no')) !!}
    {!! Form::text('course_code',$value=$course->course_code,$att = array('class','blue','required')) !!}

    {!! Form::label('Course_creadit','creadit_title',array('class'=>'no')) !!}
    {!! Form::text('course_credite',$value=$course->course_credite,$att = array('class','blue','required')) !!}

    {!! Form::submit('submit',$att=array('class'=>'has')) !!}

    {!! Form::close() !!}
</div>
</body>
</html>

我的路线

Route::resource('course','CourseController');

2 个答案:

答案 0 :(得分:2)

问题在于您打开表单的方式:

{!! Form::open(array('route'=>['course.update','$course->course_id'],'class'=>'jack','method'=>'PUT')) !!}

路由的参数周围有单引号,因此它希望传入文字字符串&#34; $ course-&gt; course_id&#34;。要解决此问题,您只需删除这些引号:

{!! Form::open(['route' => ['course.update', $course->course_id], 'class' => 'jack', 'method' => 'PUT']) !!}

答案 1 :(得分:0)

肮脏的方式:

您可以仅使用findOrFail()替换find,并使用if(!is_null($user))

以编程方式处理

清洁方式:

创建一个新的异常文件来捕获您的异常,或者只是在代码中使用它,如下所示:

// Will return a ModelNotFoundException if no Course with that id
try
{
    $data = Course::findOrFail($id);
    $data->update($input);

    return redirect('course');
}
// catch(Exception $e) catch any exception
catch(ModelNotFoundException $e)
{
    dd(get_class_methods($e)); // lists all available methods for exception object
    dd($e);
    // you might do here
    session()->flash('message', ['danger', 'Oops no such course found !']);
    return redirect('course'); // please mind to change it later to use names route or perhabs action()
}

在您的课程索引视图中右键以下HTML来呈现错误

@include('errors.alert',['type'=> Session::get('message.0'),'message' => Session::get('message.1') ])

并确保创建此部分视图errors/alert.blade.php

<div class="alert alert-{{ $type }} alert-page alert-dark">
    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
    {!! $message  !!}
</div>