0

hello people here is my code below for 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

my route file contains... Route::post('sbs_site', array('uses' => 'myController@sys_config'));

what could be the problem??

0

4 Answers 4

2

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
Sign up to request clarification or add additional context in comments.

4 Comments

even i want to do without redirecting, but its not working , i get an error Undefined variable: siteinformation
It should work if you load the view directly from the controller, what URI you are using to see the page and what is your view name?
your second answer is exactly same as mine,ya that worked...` view:make` did not work.. uri =http://localhost/sup/public/sbs_site sbs_site is my view name... i get an error message on the uri page...
It must work if you did everything properly, check it again. There is nothing very complex, it's a very simple task in Laravel.
0

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

Comments

0

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.

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

Comments

-1

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

$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

2 Comments

i tried to your first solution but did not work it will make a view but does hold any variable values.. :( let me try your second solution...
second solution,I am getting an error Call to a member function toJson() on a non-object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.