Support the ongoing development of Laravel.io →
posted 8 years ago
Views
Last updated 2 years ago.
0

The forum code-tag doen't recognize comments.

0

You need to pass an array as the second argument to view

I tend to set up an array at the start of my controller method to hold all the data for the view

controller:

$data = []; // array that will hold any data that is being passed to the view
$customer = Customer::where('name', '=', strtolower($name))->first();
if(!empty($customer))
{
    $data['customer'] = $customer;
    return view('customer.info', $data);
}

view (customer/info.blade.php):

{{$customer->name}}
0

Thanks, now that I pretty much copied your code suggestion it works, but I'm still a little confused about why my try didn't work. Too me it's kind of the same thing

0

Customer is an object, not an array. Passing an array in is very flexible, as you could do

$data['customer'] = $customerObject;
$data['invoices'] = $collectionOfCustomerInvoices;

You can then pass all of that to the view using

return view('customer.info', $data);

If you didn't want to add the data to an array in your controller, you could do it when passing it to the view (as the first example you gave above):

return view('customer.info', ['customer' => $customer]);

https://laravel.com/docs/5.2/views#passing-data-to-views

0

Sign in to participate in this thread!

Eventy

Your banner here too?

muppbarn muppbarn Joined 29 Mar 2016

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.