Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 AppHttpControllers;

use AppFiles;
use IlluminateHttpRequest;

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
378 views
Welcome To Ask or Share your Answers For Others

1 Answer

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'));
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...