pjm*_*mil 65 php laravel laravel-3
我有这个站点,其中一个页面从数据库中创建一个简单的人员列表.我需要将一个特定的人添加到我可以访问的变量中.
如何修改该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);
}
Run Code Online (Sandbox Code Playgroud)
rmo*_*bis 90
只需将其作为数组传递:
$data = [
'name' => 'Raphael',
'age' => 22,
'email' => 'r.mobis@rmobis.com'
];
return View::make('user')->with($data);
Run Code Online (Sandbox Code Playgroud)
或链接他们,就像@Antonio提到的那样.
Ant*_*iro 81
这是你如何做到的:
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);
}
Run Code Online (Sandbox Code Playgroud)
你也可以使用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'));
}
Run Code Online (Sandbox Code Playgroud)
或者在一行中完成:
function view($view)
{
return $view
->with('ms', Person::where('name', '=', 'Foo Bar')->first())
->with('persons', Person::order_by('list_order', 'ASC')->get());
}
Run Code Online (Sandbox Code Playgroud)
或者甚至将其作为数组发送:
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]));
}
Run Code Online (Sandbox Code Playgroud)
但是,在这种情况下,您必须以这种方式访问它们:
{{ $data['ms'] }}
Run Code Online (Sandbox Code Playgroud)
sum*_*mit 29
使用紧凑
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'));
}
Run Code Online (Sandbox Code Playgroud)
Aks*_*ale 20
将多个变量传递给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]);
Run Code Online (Sandbox Code Playgroud)
阅读此处关于将数据传递给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 );
}
Run Code Online (Sandbox Code Playgroud)
当你看到返回的最后一行时,它看起来不太好
当你的项目变得越来越大时,这并不好
所以
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));
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,所有变量都声明为数组$viewShareVars并在视图中访问
但我的功能变得非常大,所以我决定让这条线变得 非常简单
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));
}
Run Code Online (Sandbox Code Playgroud)
原生 php 函数从函数中get_defined_vars() 获取所有定义的变量
并array_keys 会获取变量名称
所以在你看来你可以访问函数内所有声明的变量
作为{{$todayPostActive}}