Search code examples
laravellaravelcollective

Retrieve data from Collection object in Laravel


I have something like this in my controller:

$item = item::where('id',1)->first();

I get a Collective object.

I want to retrieve a specific table from such item.

$item->only(['name]);

So I can give it to the view. However it won't work.

BadMethodCallException in Builder.php line 2508: Call to undefined method Illuminate\Database\Query\Builder::only()

How do I retrieve this concrete variable?


Solution

  • When you're using first() method, you get an object, so you can just access it's properties:

    $item = item::where('id',1)->first();
    $name = $item->name;