0

in below code i have Eloquent command and that return Collection object and i can not fetch that into view.

$collection = DB::table('contents')
    ->join('categories', function($join)
    {
        $join->on('categories.id', '=', 'contents.category');
    })
    ->where('contents.id', '=', $id)
    ->get();

thats return single Collecton Object and i do not need to use foreach. how to fetch this collection object in view without usign foreach?

i get error after using this :

echo $collection->title;
{{ $collection->title }}

ERROR:

Trying to get property of non-object
3
  • On the view? {{ $collection->title }} if you dont want to use foreach then {{ var_dump($collection) }} Commented Mar 16, 2014 at 20:05
  • @majimboo i get this error:Trying to get property of non-object for {{ $collection->title }} Commented Mar 16, 2014 at 20:08
  • because that is a array of objects. see my answer. Commented Mar 16, 2014 at 20:09

1 Answer 1

1

What is a single collection object? You must be talking about a single model? Collections are always an array of models.

Your query should be like:

$collection = DB::table('contents')
                ->join('categories', 'contents.category', '=', 'categories.id')
                ->where('contents.id', '=', $id)
                ->first();

echo $collection->title;

passing data to the view can be like:

$data = array(
   'collection' => $collection
);
return View::make('collection', $data);

accessing the data from the template:

{{ $collection->title }}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.