我有这个网站,其中一个页面会从数据库中创建一个简单的人员列表。我需要将一个特定的人添加到我可以访问的变量中。
如何修改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);
}
答案 0 :(得分:84)
只需将其作为数组传递:
$data = [
'name' => 'Raphael',
'age' => 22,
'email' => 'r.mobis@rmobis.com'
];
return View::make('user')->with($data);
或链接他们,就像@Antonio提到的那样。
答案 1 :(得分: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'] }}
答案 2 :(得分:27)
使用紧凑型
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'));
}
答案 3 :(得分:18)
将多个变量传递给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]);
请在此处阅读old link
答案 4 :(得分:3)
遇到了类似的问题,但是如果您不一定要返回包含视图文件的视图,则可以执行以下操作:
return $view->with(compact('myVar1', 'myVar2', ..... , 'myLastVar'));
答案 5 :(得分:1)
请尝试一下
$ms = Person::where('name', 'Foo Bar')->first();
$persons = Person::order_by('list_order', 'ASC')->get();
return View::make('viewname')->with(compact('persons','ms'));
答案 6 :(得分: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));
答案 7 :(得分: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));
答案 8 :(得分:0)
这个答案似乎是
有帮助,同时在函数
中声明大量变量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
数组并在视图中访问
但是我的功能变得非常大,所以我决定排队 非常简单
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}}
答案 9 :(得分:0)
很简单:)
<link rel="icon" href="{{ asset('favicon.ico')}}" type="image/x-icon" />
答案 10 :(得分:0)
with
函数和 single
参数:
$ms = Person::where('name', 'Foo Bar');
$persons = Person::order_by('list_order', 'ASC')->get();
return $view->with(compact('ms', 'persons'));
with
函数和 array
参数:
$ms = Person::where('name', 'Foo Bar');
$persons = Person::order_by('list_order', 'ASC')->get();
$array = ['ms' => $ms, 'persons' => $persons];
return $view->with($array);