Laravel - Pass more than one variable to view

前端 未结 11 1492
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-29 18:54

I have this site and one of its pages creates a simple list of people from the database. I need to add one specific person to a variable I can access.

How do I modif

相关标签:
11条回答
  • 2020-11-29 19:17

    This is how you do it:

    function view($view)
    {
        $ms = Person::where('name', '=', 'Foo Bar')->first();
    
        $persons = Person::order_by('list_order', 'ASC')->get();
    
        return $view->with('persons', $persons)->with('ms', $ms);
    }
    

    You can also use compact():

    function view($view)
    {
        $ms = Person::where('name', '=', 'Foo Bar')->first();
    
        $persons = Person::order_by('list_order', 'ASC')->get();
    
        return $view->with(compact('persons', 'ms'));
    }
    

    Or do it in one line:

    function view($view)
    {
        return $view
                ->with('ms', Person::where('name', '=', 'Foo Bar')->first())
                ->with('persons', Person::order_by('list_order', 'ASC')->get());
    }
    

    Or even send it as an array:

    function view($view)
    {
        $ms = Person::where('name', '=', 'Foo Bar')->first();
    
        $persons = Person::order_by('list_order', 'ASC')->get();
    
        return $view->with('data', ['ms' => $ms, 'persons' => $persons]));
    }
    

    But, in this case, you would have to access them this way:

    {{ $data['ms'] }}
    
    0 讨论(0)
  • 2020-11-29 19:17

    Came across a similar problem, but if you do not necessarily want to return a view with view file, you can do this:

    return $view->with(compact('myVar1', 'myVar2', ..... , 'myLastVar'));
    
    0 讨论(0)
  • 2020-11-29 19:18

    Use compact

    function view($view)
    {
        $ms = Person::where('name', '=', 'Foo Bar')->first();
    
        $persons = Person::order_by('list_order', 'ASC')->get();
        return View::make('users', compact('ms','persons'));
    }
    
    0 讨论(0)
  • 2020-11-29 19:30

    For passing multiple array data from controller to view, try it. It is working. In this example, I am passing subject details from a table and subject details contain category id, the details like name if category id is fetched from another table category.

    $category = Category::all();
    $category = Category::pluck('name', 'id');
    $item = Subject::find($id);
    return View::make('subject.edit')->with(array('item'=>$item, 'category'=>$category));
    
    0 讨论(0)
  • 2020-11-29 19:31

    with function and single parameters:

        $ms = Person::where('name', 'Foo Bar');
        $persons = Person::order_by('list_order', 'ASC')->get();
        return $view->with(compact('ms', 'persons'));
    

    with function and array parameter:

        $ms = Person::where('name', 'Foo Bar');
        $persons = Person::order_by('list_order', 'ASC')->get();
        $array = ['ms' => $ms, 'persons' => $persons];
        return $view->with($array);
    
    0 讨论(0)
  • 2020-11-29 19:37

    Its simple :)

    <link rel="icon" href="{{ asset('favicon.ico')}}" type="image/x-icon" />
    
    0 讨论(0)
提交回复
热议问题