How to fix NotFoundHttpException laravel?

- 1 answer

Ad

Hi all I am a new of laravel 5.1,I just start learning laravel. I have problame in laravel I can not find the way to correct it I try to search but I still can not.I want to update form, but when I click button update its show error

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].

And here is my code: CourseController.php

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

        return redirect('course');
    }

and my model 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'
    ];
}

And here is my view edite.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Hello World</title>
    <link rel="stylesheet" type="text/css" target="_blank" rel="nofollow noreferrer" target="_blank" rel="nofollow noreferrer" 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>

And my route

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

Answer

Ad

It looks like the problem is how you're opening the form:

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

There are single-quotes around the parameter to the route, so it'd be looking to pass in the literal string "$course->course_id". To fix this, you can simply remove those quotes:

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