Home  >  Article  >  Backend Development  >  What's going on with the with function in the laravel framework? ! , waiting online...

What's going on with the with function in the laravel framework? ! , waiting online...

WBOY
WBOYOriginal
2016-08-04 09:19:461223browse

In this tutorial about getting started with laravel, I saw this way of writing:

<code><?php namespace App\Http\Controllers;
use App\Page;

class HomeController extends Controller {

    public function index()
    {
        return view('home')->withPages(Page::all());
    }

}</code>

The withPages() function here does not exist in laravel at all. I searched on Google and could not find the answer. Based on my personal guess, I think the pages after withPages is a data variable mapped to the blade template. ($pages), but I didn’t find this way of writing when I checked the laravel documentation. I only found with('name', 'Victoria');, this way of assigning variables. Does anyone know what is going on? Can you give me some information? Tell me what's going on? Quite urgent.

Reply content:

In this tutorial about getting started with laravel, I saw this way of writing:

<code><?php namespace App\Http\Controllers;
use App\Page;

class HomeController extends Controller {

    public function index()
    {
        return view('home')->withPages(Page::all());
    }

}</code>

The withPages() function here does not exist in laravel at all. I searched on Google and could not find the answer. Based on my personal guess, I think the pages after withPages is a data variable mapped to the blade template. ($pages), but I didn’t find this way of writing when I checked the laravel documentation. I only found with('name', 'Victoria');, this way of assigning variables. Does anyone know what is going on? Can you give me some information? Tell me what's going on? Quite urgent.

Dynamic view assignment!

The following are equivalent:

<code>view('home')->withPages(Page::all());
view('home')->with('pages', Page:all());</code>

In the previous chapter: Laravel 5 series introductory tutorial (2) [The most suitable Laravel tutorial for Chinese people] there is a comment:

withPages refers to querying the Pages table

This article is from last year and is relatively old. You can read the new version of the tutorial: 2016 version of Laravel series introductory tutorial (1)
Of course, Laravel Academy is also a good tutorial website.

You can understand this method as syntactic sugar

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn