Passing variables
->with Method app/http/controllers/sitecontroller.php
Class Sitecontroller extends controller{ //Public function Index () { $first = ' first '; $last = ' last '; Return view (' Welcome ')->with (' name ', $first); This name is the variable name in the view, with the meaning that the controller's $first is assigned to the name variable in the View view. In this case, the view is able to get the value and then display it. }}
app/resources/views/welcome.blade.php
Laravel 5 {$name}}//This is the name variable in the view
Passing arrays
Passing an array through the->with method
app/http/controllers/sitecontroller.php
Class Sitecontroller extends controller{ //Public function Index () { return view (' Welcome ')->with ([ ' first-key ' = ' first ', ' last-key ' = ' last ' ); }}
app/resources/views/welcome.blade.php
Laravel 5 {{$first-key}}{{$last-key}}
or by passing an array directly
app/http/controllers/sitecontroller.php
Class Sitecontroller extends controller{ $data = []; $data [' first] = ' first '; $data [' last '] = ' last '; Public Function Index () { return view (' Welcome ', $data);} }
However, although this is an array, it is used as a variable to use the key of the array directly.
app/resources/views/welcome.blade.php Laravel 5 {{$first}}{{$last}}
or through the compact.
Class Sitecontroller extends controller{public function Index () { $first-key = ' first '; $last-key = ' last '; Return view (' Welcome ', compact (' First-key ', ' Last-key ')); The compact is a basic PHP command, creating an array of variable names and their values, and after creating the array, the variable name is converted to the array key to pass to the view. }}
app/resources/views/welcome.blade.php
Laravel 5 {{$first-key}}{{$last-key}}
This article was authored by Peteryuan and licensed under the Attribution-NonCommercial use of 2.5 mainland China. Please contact the author and the author and indicate the source of the article before reprint or citation. A teenager like God.»laravel passing variables to a view