Home » Php » php – Route Model Binding not working

php – Route Model Binding not working

Posted by: admin July 12, 2020 Leave a comment

Questions:

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?

How to&Answers:

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 you should have type-hinted variable names match a route segment name, as the doc required :

For Example My Edit Function:

Here my Route URI for Edit

admin/file/{file}/edit

as You Can See There’s {file} parameter or anything you call
Now just Need Write Exactly $file in Function Parameter

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

Sorry If My English is Poor

Answer:

I know this problem already has a solution, but let me add some knowledge that might be useful to others…

As the documentation says, when using resource controllers, the resource routes will use parameters based on the “singularized” name of the resource. In the case of the question, since @siros was using the “file” resource name (singular form) in the route, the binding name in the controller method should be “file”, although his model is named Files. If he attempted to use:

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

The controller would still need Files $file to work, since file is the singularized form of files.

However, there is other (and more elegant) solution to the problem. You can change the type-hinted variable in the URL by providing a parameters option in the configuration of the route, as shown in the documentation, which would automatically apply for the show, edit, update and destroy methods. This will let you keep the variable name in your controller matching the model name, for example.

So, in this case, @siros could use this in the routes file:

Route::resource('admin/file','AdminController', [
    'parameters' => [
        'file' => 'files'
    ]
]);

And he could use this in the controller:

public function edit(Files $files)

Hope this helps someone.