简体   繁体   English

laravel从控制器传递数据库数组到视图

[英]laravel passing database array from controller to view

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?? 可能是什么问题呢??

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

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);

Your view file is not getting session set if(Session::has('siteinformation')), because you did not set session in your controller. 您的视图文件未设置会话if(Session :: has('siteinformation')),因为您未在控制器中设置会话。 You set in your controller 'siteinformation' as a normal variable. 您在控制器“ siteinformation”中将其设置为普通变量。 If you want to set session, instead of this. 如果要设置会话,请使用此设置。

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)) :

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函数将数据作为闪存传递,因此仅字符串

$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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM