laravel 将数据库数组从控制器传递到视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24028663/
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 passing database array from controller to view
提问by Friend
hello people here is my code below for controller
大家好,这里是我的代码 controller
$siteinformation = DB::table('siteinformation')->where('Site',$myarr[0]['site'])->select()->get();
print_R($siteinformation); will display
Array ( [0] => stdClass Object ( [Site] => site1 [Nickname] => friend [ProgramID] => 1 [CreateDateTime] => 2014-06-03 18:05:39 ) )
I am trying to pass it to view
我试图通过它来查看
return Redirect::to('sbs_site')->with('siteinformation',$siteinformation);
In my view i have...
在我看来,我有...
@if(Session::has('siteinformation'))
@foreach ($siteinformation as $key => $value)
{{ $value->Nickname }}
@endforeach
@endif
Iam gettng an error Undefined variable: siteinformation
我收到一个错误 Undefined variable: siteinformation
my route file contains... Route::post('sbs_site', array('uses' => 'myController@sys_config'));
我的路由文件包含... Route::post('sbs_site', array('uses' => 'myController@sys_config'));
what could be the problem??
可能是什么问题呢??
回答by The Alpha
I think you don't need to redirect; probably you are doing it wrong, instead you should pass the data to the view directly using something like this:
我认为你不需要重定向;可能你做错了,你应该使用这样的东西直接将数据传递给视图:
$siteinformation = DB::table('siteinformation')
->where('Site',$myarr[0]['site'])
->get();
// View Name: "sbs_site.blade.php"
return View::make('sbs_site')->with('siteinformation', $siteinformation);
Then in the view:
然后在视图中:
@foreach ($siteinformation as $value)
{{ $value->Site }}
{{ $value->Nickname }}
@endforeach
But if your really need to use a redirect then you may do it like this:
但是如果你真的需要使用重定向,那么你可以这样做:
// Route Is: "sbs_site"
return Redirect::to('sbs_site')->with('siteinformation', $siteinformation);
Then in your view:
那么在你看来:
@if(Session::has('siteinformation'))
@foreach (Session::get('siteinformation') as $value)
{{ $value->Site }}
{{ $value->Nickname}}
@endforeach
@endif
回答by Friend
This worked for me...
这对我有用...
in view
鉴于
@if(Session::has('siteinformation'))
@foreach (Session::get('siteinformation')as $key => $value)
{{ $value->Nickname}}
@endforeach
@endif
In controller
在控制器中
return Redirect::to('sbs_site')->with('siteinformation',$siteinformation);
回答by chanchal
Your view file is not getting session set if(Session::has('siteinformation')), because you did not set session in your controller. You set in your controller 'siteinformation' as a normal variable. If you want to set session, instead of this.
如果(Session::has('siteinformation')),您的视图文件没有设置会话,因为您没有在控制器中设置会话。您在控制器中将“站点信息”设置为普通变量。如果你想设置会话,而不是这个。
Redirect::to('sbs_site')->with('siteinformation',$urserializedobj);
Do this
做这个
Session::put('siteinformation',$urserializedobj);
Redirect::to('sbs_site');
OR
或者
Simply don't check session in your view instead of this
只是不要在您的视图中检查会话而不是这个
if(Session::has('siteinformation'))
Just do this
就这样做
if (!empty($siteinformation)) :
回答by themightysapien
I don't know why u are redirecting to pass it to the view
我不知道你为什么要重定向以将其传递给视图
return View::make('yourview',array('siteinformation'=>$siteinformation));
This will do the trick if you can make the view directly from the controller BUT if u need to redirect than i suppose you can serialize your array or change it to json first because the with function passes the data as flash so only strings
如果您可以直接从控制器创建视图,这将起作用,但是如果您需要重定向,我想您可以序列化您的数组或首先将其更改为 json,因为 with 函数将数据作为 flash 传递,因此只有字符串
$urserializedobj = $siteinformation.toJson();//if this doesn't work try serealizing
return Redirect::to('sbs_site')->with('siteinformation',$urserializedobj );
now in your view you can do this
现在在你看来你可以这样做
@if(Session::has('siteinformation'))
$siteInformation = json_decode(Session::get('siteinformation'));
@foreach ($siteinformation as $key => $value)
{{ $value->Nickname }}
@endforeach
@endif