I'm just following the book of Martin Bean to learn about Laravel 5. He start to teach about laravel with routers and after using some basic Route::get and Route::delete methods he gives a really short example of how to use Route::resource and he says that I let you to do this yourself :)
Structually there is no problem but I'm having trouble when I'm trying to pass ORM inside of the method.
Here is my 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 is how i use router
Route::resource('cats', 'CatsController');
And this is my 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 I call http://localhost/firstApp/public/cats/2
an empty object is what I got..
What is the problem?
Thanks.
You're injecting a model into the show
method but not doing any queries to get the result.
To fix the problem, change your code to something like this:
public function show(\firstApp\Cat $cat, $id)
{
return $cat->find($id);
}
Note that in the code above I also inject an $id
into the show
method, so when you hit the http://localhost/firstApp/public/cats/2
URL, 2
will be stored in this variable.
In most cases people from Laravel community do the same thing as follows:
public function show($id)
{
return \firstApp\Cat::find($id);
}
Good luck.