Laravel passing variables to a view

Source: Internet
Author: User

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

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.