Thursday 15 May 2014

model binding - Issues with Laravel 5's Route::bind() -



model binding - Issues with Laravel 5's Route::bind() -

i'm having little problem trying out laravel 5.

i followed whole tutorial in laracasts , tried same exact way there, changed name of model, table , controller.

at point route::bind() stopped working , because of that, when seek access route wild card, view come up, without data.

this routes.php

route::bind('singers', function($slug, $route){ homecoming app\singer::whereslug($slug)->first(); }); route::resource('singers', 'singercontroller', [ 'names' => [ 'index' => 'singers_index', 'show' => 'singers_show', 'edit' => 'singers_edit', 'update' => 'singers_update', 'create' => 'singers_create', 'store' => 'singers_store', 'destroy' => 'singers_destroy', ], ]);

these snippets of singercontroller

namespace app\http\controllers; utilize illuminate\routing\controller; utilize illuminate\http\request; utilize app\http\requests\createsingerrequest; utilize app\singer; class singercontroller extends controller { public $restful = true; public function __construct(singer $singer){ $this->singer = $singer; } public function index() { $singers = $this->singer->orderby('id', 'desc')->get(); homecoming view('singers.list',compact('singers')); } public function show(singer $singer){ homecoming view('singers.show', compact('singer')); } public function edit(singer $singer){ homecoming view('singers.edit', compact('singer')); } public function update(singer $singer, request $request){ $singer->fill($request->input()); $singer->save(); homecoming view('singers.show', compact('singer')); } public function create(){ homecoming view('singers.new'); } public function store(singer $singer, createsingerrequest $request){ $singer->create($request->all()); homecoming redirect()->route('singers_index'); } public function destroy(singer $singer){ $singer->delete(); homecoming redirect()->route('singers_index'); } }

now. reason seek bind variable 'singers' in routes.php, because in videos, , way, code in controller shorter. , working. , added destroy function , stopped working. said, can see views, tags, , other text in them, not info pass, except index function, since eloquent search in function itself.

now here's snippet of show.blade.php

<b>full name: </b> {{ $singer->name.' '.$singer->lastname }} <br> <b>age: </b> {{ $singer->age }} <br> <b>country: </b> {{ $singer->country }} <br> <br> <p> <b>bio: </b> {!! nl2br($singer->bio) !!} </p> {!! html::linkroute('singers_edit', 'update', [$singer->slug], ['class' => 'btn btn-primary']) !!} {!! form::open(['method'=>'delete', 'route'=>['singers_destroy', $singer->slug]]) !!} <div class="form-group"> {!! form::submit('delete', ['class'=>'btn btn-danger']) !!} </div> {!! form::close() !!}

my index view works fine, , other views forms, view i'm passing variables isn't working, whether pass doing this:

return view('singers.show',compact('singer'));

or this:

return view('singers.show')->with('singer',$singer);

so recap:

index -> fine. show -> won't show data. create -> works , new record saved. edit -> error comes because wildcard isn't sent controller.

edit

paths

index (get): /singers show (get): /singers/{singers} create (get): /singers/create store (post): /singers edit (get): /singers/{singers}/edit update (patch): /singers/{singers} destroy (delete): /singers/{singers}

keep in mind {singers} wildcard $singer->slug in each case, route::bind() function doesn't allow me phone call whatever want. , of course of study before first slash comes myserver/myproject/public

i've tested it.

singer model:

<?php namespace app; utilize illuminate\database\eloquent\model; class singer extends model { }

singercontroller - exact code provided (this file placed of course of study in app\http\controllers path.

in routes.php :

route::bind('singers', function($slug, $route){ homecoming app\singer::whereslug($slug)->first(); }); route::resource('singers', 'app\http\controllers\singercontroller', [ 'names' => [ 'index' => 'singers_index', 'show' => 'singers_show', 'edit' => 'singers_edit', 'update' => 'singers_update', 'create' => 'singers_create', 'store' => 'singers_store', 'destroy' => 'singers_destroy', ], ]);

as see i've added here total namespace path: 'app\http\controllers\singercontroller' , not singercontroller.

i'ce created simple singer table, fields id , slug - test (so have 2 columns) , added 2 records:

id slug 1 abc 2 def

views i've created:

list.blade.php:

@foreach ($singers $s) {{ $s->slug }}<br /> @endforeach

edit.blade.php , show.blade.php (exact same code):

{{ $singer->slug }} {{ $singer->id }}

now when run:

http://testpro/singers result:

def abc

as expected.

when run:

http://testpro/singers/abc/edit

i get:

abc 1

as expected.

and when run:

http://testpro/singers/def

i get:

def 2

as expected.

everything seems work fine here. create sure using way showed , should work without problem

laravel model-binding blade laravel-routing laravel-5

No comments:

Post a Comment