Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel compact() and ->with()

Tags:

php

laravel-4

I have a piece of code and I'm trying to find out why one variation works and the other doesn't.

return View::make('gameworlds.mygame', compact('fixtures'), compact('teams'))->with('selections', $selections); 

This allows me to generate a view of arrays for fixtures, teams and selections as expected.

However,

return View::make('gameworlds.mygame', compact('fixtures'), compact('teams'), compact('selections')); 

does not allow the view to be generated properly. I can still echo out the arrays and I get the expected results but the view does not render once it arrives at the selections section.

It's oké, because I have it working with the ->with() syntax but just an odd one.

Thanks. DS

like image 659
dstewart101 Avatar asked Mar 14 '14 18:03

dstewart101


People also ask

What is difference between compact and with in Laravel?

with() is a Laravel function and compact() is a PHP function and have totally different purposes. with() allows you to pass variables to a view and compact() creates an array from existing variables given as string arguments to it.

What is compact () in Laravel?

Using compact() in Laravel So with() allows you to pass variables to a view and compact() creates an array from existing variables given as string arguments to it. Below is an example of how we'd use the compact function. $user = User::all();

What is -> and => in Laravel?

The difference is that => is the assign operator that is used while creating an array. For example: array(key => value, key2 => value2) And -> is the access operator. It accesses an object's value. Follow this answer to receive notifications.

What is the purpose of the explode () extract () and compact () PHP function?

PHP | compact() Function The compact() function is an inbuilt function in PHP and it is used to create an array using variables. This function is opposite of extract() function. It creates an associative array whose keys are variable names and their corresponding values are array values.


2 Answers

The View::make function takes 3 arguments which according to the documentation are:

public View make(string $view, array $data = array(), array $mergeData = array()) 

In your case, the compact('selections') is a 4th argument. It doesn't pass to the view and laravel throws an exception.

On the other hand, you can use with() as many time as you like. Thus, this will work:

return View::make('gameworlds.mygame')  ->with(compact('fixtures'))  ->with(compact('teams'))  ->with(compact('selections')); 
like image 164
tliokos Avatar answered Oct 20 '22 01:10

tliokos


I just wanted to hop in here and correct (suggest alternative) to the previous answer....

You can actually use compact in the same way, however a lot neater for example...

return View::make('gameworlds.mygame', compact(array('fixtures', 'teams', 'selections'))); 

Or if you are using PHP > 5.4

return View::make('gameworlds.mygame', compact(['fixtures', 'teams', 'selections'])); 

This is far neater, and still allows for readability when reviewing what the application does ;)

like image 22
j5Dev Avatar answered Oct 19 '22 23:10

j5Dev