Laravel Compact() and ->With()

laravel compact() and -with()

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'));

With() vs Compact() 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.

See compact() for more info on this matter.

What is the difference between with, compact and array in when return view in laravel

Array : You may pass an array of data to views, like this :

return view('post', ['post' => $post]);

When passing information in this manner, the data should be an array with key / value pairs. Inside your view, you can then access each value using its corresponding key, such as <?php echo $key; ?>


with() : As an alternative to passing a complete array of data to the view helper function, you may use the with method to add individual pieces of data to the view :

return view('post')->with('post' => $post);
// multiple with method
return view('post')->with('post' => $post)->with('comment' => $comment);

compact() : Instead of using this type of passing data, you can use compact() to passing data. compact() is a built in php function that allows you create an array with variable names and their values. variable names must be pass to compact function as string argument and then, you with receive an array, so compact passing the varibale on your view like the first method :

return view('post', compact('post'));
// same as
return view('post', ['post' => $post]);

See the official documentation of Passing Data To Views

What's the difference between with() and compact() in Laravel

Well compact() is a PHP function that converts a list of variables into an associative array where the key is the variable name and the value the actual value of that variable.

The actual question should be: What is the difference between

return View::make('books.index')->with('booksList', $booksList);

and

return View::make('books.index', array('booksList' => $booksList));

The answer there isn't really one. They both add items to the view data.

Syntax-wise, View::make() only accepts an array while with() takes both, two strings:

with('booksList', $booksList);

Or an array that can possibly hold multiple variables:

with(array('booksList' => $booksList, 'foo' => $bar));

This also means that compact() can be used with with() as well:

return View::make('books.index')->with(compact($booksList));

How can I send a Success Message with compact in Laravel

You can use compact() or with(), but generally not both.

Here's some options:

  1. Use ->with() with a single array:
return view('cars.index')->with(['cars' => $cars, 'success' => 'Results Car']);

  1. Chain ->with() calls:
return view('cars.index')->with('cars', $cars)->with('success', 'Results Car');

  1. Pass an array instead of with() or compact():
return view('cars.index', ['cars' => $cars, 'success' => 'Results Car']);

  1. Use compact() with a $success variable:
$success = 'Results Car';
return view('cars.index', compact('cars', 'success'));

laravel compact data wont read in home.blade.php

You can try and rewrite your controller code like this:

return view('home')->with(compact('images'));

Official Laravel docs here does not say anything about any second parameters to view() helper.
with() should actually append the data and $images should be available in blade now.

Also, for simplicity, just use Image::all(). If this again is not working, you can try and ditch the compact method and in with() send an array like:

return view('home')->with(['images' => $images]);

Redirect with compact value in laravel

Try this

return redirect()->route('Merchant view')->with( ['merchant' => $merchant] );

In blade file :

<?php $merchants = Session::get('merchant'); ?>
@foreach ($merchants as $merchant)
//your code
@endforeach

Hope it helps you !

How to use laravel redirect back with compact

Try one of following

 return redirect()->back()->with(compact('fee'));

or

 return redirect()->back()->with('fee', $fee);


Related Topics



Leave a reply



Submit