Php Laravel-传递多个变量以查看
我有这个网站,它的一个页面从数据库中创建了一个简单的人员列表。我需要将一个特定的人添加到我可以访问的变量中 如何使用('persons',$persons)修改Php Laravel-传递多个变量以查看,php,laravel,laravel-3,Php,Laravel,Laravel 3,我有这个网站,它的一个页面从数据库中创建了一个简单的人员列表。我需要将一个特定的人添加到我可以访问的变量中 如何使用('persons',$persons)修改return$view->withline是否也将$ms变量传递给视图 function view($view) { $ms = Person::where('name', 'Foo Bar'); $persons = Person::order_by('list_order', 'ASC
return$view->with
line是否也将$ms变量传递给视图
function view($view)
{
$ms = Person::where('name', 'Foo Bar');
$persons = Person::order_by('list_order', 'ASC')->get();
return $view->with('persons', $persons);
}
这就是你如何做到的:
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);
}
您还可以使用:
或者在一行中完成:
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 = [
'name' => 'Raphael',
'age' => 22,
'email' => 'r.mobis@rmobis.com'
];
return View::make('user')->with($data);
但是,在这种情况下,您必须通过以下方式访问它们:
{{ $data['ms'] }}
只需将其作为数组传递:
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 = [
'name' => 'Raphael',
'age' => 22,
'email' => 'r.mobis@rmobis.com'
];
return View::make('user')->with($data);
或者像@Antonio提到的那样把它们锁起来
使用紧凑型
遇到类似问题,但如果不一定要返回带有视图文件的视图,可以执行以下操作:
return $view->with(compact('myVar1', 'myVar2', ..... , 'myLastVar'));
将多个变量传递到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]);
请阅读此处关于的内容。要将多个阵列数据从控制器传递到视图,请尝试。它正在工作。在本例中,我从一个表中传递主题详细信息,主题详细信息包含category id,如果category id是从另一个表类别中获取的,则包含name等详细信息
$category = Category::all();
$category = Category::pluck('name', 'id');
$item = Subject::find($id);
return View::make('subject.edit')->with(array('item'=>$item, 'category'=>$category));
请试试这个
$ms = Person::where('name', 'Foo Bar')->first();
$persons = Person::order_by('list_order', 'ASC')->get();
return View::make('viewname')->with(compact('persons','ms'));
这个答案似乎是正确的 位有用,同时声明函数中的大量变量 拉威尔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 );
}
当您看到返回的最后一行时,它看起来不太好
当你的项目越来越大的时候,它就不好了
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));
}
正如您看到的,所有变量都声明为$viewShareVars的数组
,并在视图中访问
但是我的函数变得非常大,所以我决定做这行
非常简单
本机php函数get_defined_vars()
从函数中获取所有定义的变量
而数组\u键
将获取变量名
因此,在您的视图中,您可以访问函数中所有声明的变量
作为{{$todaypostative}}
它很简单:)
具有
功能和单个参数:
$ms = Person::where('name', 'Foo Bar');
$persons = Person::order_by('list_order', 'ASC')->get();
return $view->with(compact('ms', 'persons'));
$ms = Person::where('name', 'Foo Bar');
$persons = Person::order_by('list_order', 'ASC')->get();
$array = ['ms' => $ms, 'persons' => $persons];
return $view->with($array);
具有
功能和数组
参数:
$ms = Person::where('name', 'Foo Bar');
$persons = Person::order_by('list_order', 'ASC')->get();
return $view->with(compact('ms', 'persons'));
$ms = Person::where('name', 'Foo Bar');
$persons = Person::order_by('list_order', 'ASC')->get();
$array = ['ms' => $ms, 'persons' => $persons];
return $view->with($array);
是的,同时我也走了这么远。但是现在当我尝试在视图中访问它时,例如,
{{$ms->name}}
我得到了未定义的属性:Laravel\Database\Eloquent\Query::$name
我缺少什么?你缺少方法第一行的->first()
。pjmil116刚刚编辑添加了拉斐尔评论的->first()
。啊,是的。我刚刚意识到这个项目是Laravel3,因此是我沮丧的一个巨大来源。不得不将查询语法更改为$ms=DB::first('select*from people where name=“Foo Bar”)代码>Laravel4和3语法,对于像这样的常见事物,几乎是相同的。您仍然可以使用Person::where('name','=','Foo Bar')->first()
。编辑。远比链接更优雅推荐的方法是使用压缩方法传递变量。这感觉相当优雅。我认为部分选择将基于数据。对于一个简单的变量,这是可行的,但是对于变量压缩中的嵌套数据,或者对于在视图中进行操作来说,这是最有效的。与其他答案相比,这一个似乎有点不合适。您节省了我的时间,谢谢!我认为这是一个很好的解决方案。
$ms = Person::where('name', 'Foo Bar');
$persons = Person::order_by('list_order', 'ASC')->get();
$array = ['ms' => $ms, 'persons' => $persons];
return $view->with($array);