php - Having trouble with using ORM inside of route::resource -


i'm following book of martin bean learn laravel 5. start teach laravel routers , after using basic route::get , route::delete methods gives short example of how use route::resource , says let :)

structually there no problem i'm having trouble when i'm trying pass orm inside of method.

here catscontroller.php

namespace firstapp\http\controllers;  use illuminate\http\request;  use firstapp\http\requests; use firstapp\http\controllers\controller; public function show(\firstapp\cat $cat) {       return $cat;       //return view('cats.show')->with('cat', $cat); } 

here how use router

route::resource('cats', 'catscontroller'); 

and cat.php

- namespace firstapp;  use illuminate\database\eloquent\model;  class cat extends model {   public $timestamps = false;   protected $fillable = ['name', 'date_of_birth', 'breed_id'];   public function breed(){     return $this->belongsto('firstapp\breed');   } } 

when call http://localhost/firstapp/public/cats/2 empty object got..

what problem?

thanks.

you're injecting model show method not doing queries result.

to fix problem, change code this:

public function show(\firstapp\cat $cat, $id) {     return $cat->find($id); } 

note in code above inject $id show method, when hit http://localhost/firstapp/public/cats/2 url, 2 stored in variable.

in cases people laravel community same thing follows:

public function show($id) {     return \firstapp\cat::find($id); } 

good luck.


Comments