Laravel - 传递多个变量进行查看

问题描述 投票:59回答:9

我有这个站点,其中一个页面从数据库中创建一个简单的人员列表。我需要将一个特定的人添加到我可以访问的变量中。

如何修改return $view->with('persons', $persons);行以将$ ms变量传递给视图?

    function view($view)
    {
        $ms = Person::where('name', 'Foo Bar');

        $persons = Person::order_by('list_order', 'ASC')->get();

        return $view->with('persons', $persons);
    }
php laravel laravel-3
9个回答
86
投票

只需将其作为数组传递:

$data = [
    'name'  => 'Raphael',
    'age'   => 22,
    'email' => '[email protected]'
];

return View::make('user')->with($data);

或链接他们,就像@Antonio提到的那样。


78
投票

这是你如何做到的:

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);
}

你也可以使用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'));
}

或者在一行中完成:

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

或者甚至将其作为数组发送:

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]));
}

但是,在这种情况下,您必须以这种方式访问​​它们:

{{ $data['ms'] }}

28
投票

使用紧凑

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'));
}

19
投票

将多个变量传递给Laravel视图

//Passing variable to view using compact method    
$var1=value1;
$var2=value2;
$var3=value3;
return view('viewName', compact('var1','var2','var3'));

//Passing variable to view using with Method
return view('viewName')->with(['var1'=>value1,'var2'=>value2,'var3'=>'value3']);

//Passing variable to view using Associative Array
return view('viewName', ['var1'=>value1,'var2'=>value2,'var3'=>value3]);

在这里阅读有关Passing Data to Views in Laravel的信息


3
投票

遇到了类似的问题,但如果您不一定要返回带有视图文件的视图,则可以执行以下操作:

return $view->with(compact('myVar1', 'myVar2', ..... , 'myLastVar'));

1
投票

请试试这个,

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

1
投票

这个答案似乎是

在声明函数中的大量变量时有点有用

Laravel 5.7。*

例如

public function index()
{
    $activePost = Post::where('status','=','active')->get()->count();

    $inActivePost = Post::where('status','=','inactive')->get()->count();

    $yesterdayPostActive = Post::whereDate('created_at', Carbon::now()->addDay(-1))->get()->count();

    $todayPostActive = Post::whereDate('created_at', Carbon::now()->addDay(0))->get()->count();

    return view('dashboard.index')->with('activePost',$activePost)->with('inActivePost',$inActivePost )->with('yesterdayPostActive',$yesterdayPostActive )->with('todayPostActive',$todayPostActive );
}

当你看到退货的最后一行时看起来不太好

当你的项目越来越大它不好

所以

public function index()
    {
        $activePost = Post::where('status','=','active')->get()->count();

        $inActivePost = Post::where('status','=','inactive')->get()->count();

        $yesterdayPostActive = Post::whereDate('created_at', Carbon::now()->addDay(-1))->get()->count();

        $todayPostActive = Post::whereDate('created_at', Carbon::now()->addDay(0))->get()->count();

        $viewShareVars = ['activePost','inActivePost','yesterdayPostActive','todayPostActive'];

        return view('dashboard.index',compact($viewShareVars));
    }

如您所见,所有变量都声明为$viewShareVars数组和View中的Accessed

但是我的功能变得非常大,所以我决定让这条线变得非常简单

public function index()
    {
        $activePost = Post::where('status','=','active')->get()->count();

        $inActivePost = Post::where('status','=','inactive')->get()->count();

        $yesterdayPostActive = Post::whereDate('created_at', Carbon::now()->addDay(-1))->get()->count();

        $todayPostActive = Post::whereDate('created_at', Carbon::now()->addDay(0))->get()->count();

        $viewShareVars = array_keys(get_defined_vars());

        return view('dashboard.index',compact($viewShareVars));
    }

本机php函数get_defined_vars()从函数中获取所有已定义的变量

array_keys将获取变量名称

所以在你的视图中,你可以访问函数内的所有声明变量

作为{{$todayPostActive}}


0
投票

要将多个数组数据从控制器传递到视图,请尝试它。这是工作。在此示例中,我从表中传递主题详细信息,主题详细信息包含类别ID,如果从另一个表类别中获取类别ID,则详细信息如名称。

$category = Category::all();
$category = Category::pluck('name', 'id');
$item = Subject::find($id);
return View::make('subject.edit')->with(array('item'=>$item, 'category'=>$category));

0
投票
    $oblast = Oblast::all();
    $category = Category::where('slug', $catName)->first();
    $availableProjects = $category->availableProjects;
    return view('pages.business-area')->with(array('category'=>$category, 'availableProjects'=>$availableProjects, 'oblast'=>$oblast));
© www.soinside.com 2019 - 2024. All rights reserved.