phphtmlmysqllaravelcrud

Route Model Binding not working


I'm trying to use Route Model Binding for Simple CRUD but Update And Delete Function Not Working. and I'm Using laravel 5.5

Route::resource('admin/file','AdminController');

My View For Edit and Delete Buttons

<a href="{{ route('file.edit', ['id'=>$file->id]) }}">

<form action="{{ route('file.destroy', ['id'=>$file->id]) }}" method="post">
   {{method_field('DELETE')}}
   {{csrf_field()}}
   <button type="submit" class="delete">delete</button>
</form>

My Resource Controller :

namespace App\Http\Controllers;

use App\Files;
use Illuminate\Http\Request;

Store Work Fine

  public function store(Request $request)
{
    $this->validate($request,[
        'title'=>'required',
        'body'=>'required',
        'price'=>'required',
        'linkFile'=>'required',
    ]);

     Files::create($request->all());
    return redirect(route('file.index'));
}

But Edit and Delete Not Working

public function edit(Files $files)
{
   return view('admin.edit',compact('files'))->with('title','Edit File');
}

public function destroy(Files $files)
{
    $files->delete();
    return redirect(route('file.index'));
}

My Model:

protected $table='files';

protected $fillable=[
    'title','body','price','linkFile'
];

When I Delete Button Nothing Happens and Edit as Same

If I Add dd($files) at First Column for Edit and Delete Function Then Response Will be [] and There's No Error for handle

Here My Route Lists

enter image description here

Anyone Can help Please?


Solution

  • Finally, after 2 days I found my answer and I would like to provide my answer here for everyone who maybe has the same problem.

    For route binding to work, your type-hinted variable name must match the route placeholder name

    For example my edit method

    Here is my route URI for the edit

    admin/file/{file}/edit
    

    As you can see there is a {file} placeholder in the route definition, so the corresponding variable must be called $file.

    public function edit(Files $file)
    {
       return view('admin.edit',compact('file'));
    }