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}}
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
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]);
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community