php Laravel - 传递多个变量来查看
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20110757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel - Pass more than one variable to view
提问by pjmil
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 modify the return $view->with('persons', $persons);line to also pass the $ms variable to the view?
如何修改该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);
}
回答by rmobis
Just pass it as an array:
只需将其作为数组传递:
$data = [
'name' => 'Raphael',
'age' => 22,
'email' => '[email protected]'
];
return View::make('user')->with($data);
Or chain them, like @Antonio mentioned.
或者像@Antonio 提到的那样链接它们。
回答by Antonio Carlos Ribeiro
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():
您还可以使用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'] }}
回答by sumit
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'));
}
回答by Akshay Khale
Passing multiple variables to a Laravel view
将多个变量传递给 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]);
Read here about Passing Data to Views in Laravel
在此处阅读有关在 Laravel 中将数据传递给视图的信息
回答by Makan
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'));
回答by Manojkiran.A
This Answer seems to be
这个答案似乎是
bit helpfulwhile declaring the large numbe of variablein the function
位乐于助人,同时宣布大numbe变量的函数
Laravel 5.7.*
Laravel 5.7.*
For Example
例如
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 );
}
When you see the last line of the returns it not looking good
当您看到返回的最后一行时,它看起来不太好
When You Project is Getting Larger its not good
当你的项目越来越大时,这并不好
So
所以
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));
}
As You see all the variables as declared as array of $viewShareVarsand Accessed in View
如您所见,所有变量都声明为数组$viewShareVars并在视图中访问
But My Function Becomes very Larger so i have decided to make the line as very simple
但是我的功能变得非常大所以我决定让这条线变得 非常简单
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));
}
the native php function get_defined_vars()get all the defined variablesfrom the function
原生 php 函数从函数中get_defined_vars()获取所有定义的变量
and array_keyswill grab the variable names
并array_keys会抢了变量名
so in your view you can access all the declared variable inside the function
所以在你看来你可以访问函数内的所有声明的变量
as {{$todayPostActive}}
作为 {{$todayPostActive}}
回答by reshma
Please try this,
请试试这个,
$ms = Person::where('name', 'Foo Bar')->first();
$persons = Person::order_by('list_order', 'ASC')->get();
return View::make('viewname')->with(compact('persons','ms'));
回答by Dragana Poznan
$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));
回答by Deepak Kumar
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.
要将多个数组数据从控制器传递到视图,请尝试。这是工作。在此示例中,我从表中传递主题详细信息,主题详细信息包含类别 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));
回答by Walid Fouad
Its simple :)
这很简单 :)
<link rel="icon" href="{{ asset('favicon.ico')}}" type="image/x-icon" />

