P
P
prozrost2017-08-05 20:23:51
Laravel
prozrost, 2017-08-05 20:23:51

How to add a model object to a link?

Created Resource Controller, registered routing in web.
It looks like this:
Route::resource('teams','TeamController');
As I understand from the documentation, routes and names for them have been created.
I want to add to my view in front of each element from the model a button like "Edit, delete, view"
I do it like this:

@foreach($teams as $team)
        <tr>
            <td>{{$team->name}}</td>
            <td>{{$team->score}}</td>
            <td>
                <a class="btn btn-info" href="{{ route("teams.show/{$team}") }} ">Show</a>
                {{--<a class="btn btn-danger" href="{{ route("teams.destroy/{$team}") }}">Delete</a>--}}
                <a class="btn btn-info" href="{{ route("teams.edit/{$team}/edit") }}">Update</a>
            </td>
        </tr>
        @endforeach

But now the error is:
Route [show/{"id":8,"name":"Zorya","score":15}] not defined. (View: H:\xampp\htdocs\hockeyapp\resources\views\teams\index.blade.php)

As I understand it, you can now work with Laravel 5 not through $id, but through the object itself, but I don’t know how to do this.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
ajaxtelamonid, 2017-08-05
@prozrost

Autobinding of models works in the routes file ( https://laravel.com/docs/5.4/routing#route-model-b... in generating links, everything is the same, you need to pass the id.
For some reason, you substitute the model id in the route name , and don't pass it as the second argument: https://laravel.com/docs/5.4/routing#named-routes
There is no point in using autobinding, you will confuse yourself, and there are no bonuses.

K
Konstantin Malyarov, 2017-08-05
@Konstantin18ko

href="{{ route("teams.show", ["id"=>$team->id]) }}
Same with delete.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question