19

I am confuse as how to pass variable from controller to view. I know there is a lot about this already on stockoverflow but unable to find one that works, please point me in the right direction. Thank you.

CONTROLLER

 class StoreController extends Controller

 {
    public function index()
    {
        $store = Store::all(); // got this from database model
        return view('store', $store); 
    }
 {

VIEW

// try a couple of differnt things nothing works 

print_r($store); 
print_r($store[0]->id);
print_r($id); 

9 Answers 9

32
public function index()
{
  $store = Store::all(); // got this from database model

  return view('store')->with('store', $store); 
}

You've three options in general.

  1. return view('store')->with('store', $store);

  2. return view('store')->withStore($store);

  3. return view('store')->with(compact('store'));

1.Creates a variable named store which will be available in your view and stores the value in the variable $store in it.

2.Shorthand for the above one.

3.Also a short hand and it creates the same variable name as of the value passing one.

Now in your view you can access this variable using

{{ $store->name }}

it will be same for all 3 methods.

Sign up to request clarification or add additional context in comments.

2 Comments

what is the syntax to call it in view.blade.php? are they all the same?
Yes, it will be same for all three methods.
8

Try this

public function index()
{
    return view('store', ['store' => Store::all()]); 
}

Then you can access $store in your review.

Update: If you have your variable defined already in your code and want to use the same name in your view you can also use compact method.

Example:

public function index()
{
    $store = Store::all();

    return view('store', compact('store')); 
}

1 Comment

Best answer as it doesn't require the extra "with()" method.
6

It works for me. You can use 'compact' like this:

CONTROLLER:

class StoreController extends Controller
{
  public function index()
  {
      $store = Store::all(); // 'don't forget' use App\Driver;
      return view('store', compact('store')); 
  }
}

Don't forget the library: use App\Store;

VIEW:

In the view you have one array with all the data just calling {{ $store }} If you want an specific data use something like:

@foreach($store as $data)
   {{ $data->name }}
@endforeach

Don't forget to use the "Forms & HTML" of Laravel, here is the link of the documentation: https://laravel.com/docs/4.2/html

Comments

3

Both of the answers provided so far will solve your problem but it's also worth knowing that your original approach of providing $store as the second argument to view() was very nearly correct. What you were missing is that the argument needs to be an array of the variables you want to pass to the view.

I would probably have gone with return view('store', compact('store')); but you could also have used return view('store', ['store' => $store]);.

2 Comments

Thank you, now I know the second arg need to be exactly key=>value it's really hard to understand that concept via documentation since I assume array is already in a key=>value format.
No problem. It took me ages when I was learning Laravel to even find out that passing an array as a second argument was even an option. My early code had lots of return view('view.name')->withSomething($something)->withSomethingElse($somethingElse);!
2

Try this :

    class StoreController extends Controller
 {
    public function index()
    {
        $store = Store::all(); // got this from database model
        return view('store')->withStore($store); 
    }
 }


View :


{{$store->id}}

3 Comments

if it helps you, set my answer as solution ^^
it works! I was stuck for the longest time, try to follow the documentation which said return view('store', $store); would you mind explaining withStore(...)? I saw other answer that only do with(...)?
Yes, you can use both codes : return view('view')->withVariable($variable); or :return view('view')->with('variable', $variable); :)
1

Even when the other methods work great, the suggested way, is using the compact() helper as a parameter.

public function index()
{
  $stores = Store::all(); // got this from database model

  return view('store', compact('stores')); 
}

In laravel 5.2 the "with()" method is also used to flash data to your view, the compact helper can handle multiple variables.

Source: Laravel docs and personal experience.

Comments

1

In larval v5.6.3, I am passing Array from Controller to View like this,

$data = array(
    'title' => 'Welcome to Laravel',
    'services' => ['Web Design', 'Programming', 'SEO']
);

return view('pages.index', compact('data'));

And in HTML you can read Array as,

@if(count($data['services']) > 0)
    <ul>
        @foreach($data['services'] as $service)
        <li>{{$service}}</li>
        @endforeach
    </ul>
@endif

Comments

0

I had same challenge using

return view('store', compact('store'));

But I was rather using <?php foreach()... ?>inside the blade file to test. I changed to {{ }} blade syntax and everything worked fine now.

Comments

0

in Laravel 6, 7:

 class StoreController extends Controller

 {
    public function index()
    {
        $store = Store::all(you can write your filed name here); // got this from database model
        return view('store', compact($store)); 
    }
 {

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.