1

I retrieved those from the database and assigned them to variables, but i am not able to pass both of them to the view.

$Posts = posts::find($id);

$image = images::find($id);

I tried passing them both in an array but haven't had luck. tried this as well

return View::make('Index',['x_var' => $Posts, 'y_var' => $image]);

In the view it can only recognise the x_var and when i use the y_var the page crashes.

Nick
  • 106
  • 10
user30102967
  • 221
  • 2
  • 13

3 Answers3

3

You can use following...

return View::make('index', compact('Posts', 'image'));
vps
  • 422
  • 2
  • 6
1

Or put it in a array like that..

$data = array(
   'Posts'  => posts::find($id),
   'image'   => images::find($id),
);

return View::make('Index')->with($data);

Edit

If you get an error with the y_var maybe there is a problem with the image you want to find. Please give us more information about the error.

Nick
  • 106
  • 10
1

There are two ways you can do this:

The first way:

return View::make('view', ['Posts' => $posts, 'image', $image]);

And the second way, Which is for me, is much cleaner:

return View::make('view', compact('Posts', 'image'));

And please be consistent when you're coding.

It's really annoying to see a variable capitalized.

Akar
  • 5,075
  • 2
  • 25
  • 39
  • I worked at a PHP role for several years where the convention was to capitalise object variables, lower-case for everything else. Took me ages to unlearn it `;-)`. – halfer May 18 '15 at 21:53
  • Yeah, right now I'm learning Python. and I'm a PHP Developer. It is really hard to not use the braces for functions and classes. because I'm a PHP Developer. – Akar May 19 '15 at 07:48