Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 460
siros Avatar asked Sep 19 '17 18:09

siros


People also ask

How do you bind a route model?

Route Model Binding uses Dependency Injection to automatically find the game we are looking for. We can remove this entire line of code $game = Game::find($id);, and simply place the word Game in front of $id like so: show(Game $id). This is an example of making use of something called Type Hinting.

How does laravel route model binding work?

Laravel route model binding provides a convenient way to automatically inject the model instances directly into your routes. For example, instead of injecting a user's ID, you can inject the entire User model instance that matches the given ID.

Which types of route model binding are supported in laravel?

Laravel currently supports two types of route model bindings. We have: Implicit model binding. explicit model binding.

What is data binding in laravel?

it is data binding , the data will be add automatically to the related fields even if the field is null. – Alaa Mohammad. Jan 4, 2016 at 9:24. 1. I mean to say that the values from the database are empty.


2 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, your type-hinted variable name must match the route placeholder name

For example my edit method

Here my route URI for edit

admin/file/{file}/edit

As you can see there is {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'));
}
like image 77
siros Avatar answered Nov 01 '22 05:11

siros


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.

like image 35
Cleber de Souza Alcântara Avatar answered Nov 01 '22 06:11

Cleber de Souza Alcântara