0

I am already sending a variable using Auth::login($user) and setting it inside an array in my routes. Want to send an additional array, everything i tried failed.

Code I have

Controller

public function callback(){
        if( !$this->fb->generateSessionFromRedirect() ){
            return Redirect::to('/')->with('message', "Error logging into facebook");
        }

        $user_fb = $this->fb->getGraph();

        if(empty($user_fb)) {
            return Redirect::to('/')->with('message', "User Not Found");
        }

        $user = User::whereUidFb($user_fb->getProperty('id'))->first();

        if(empty($user)){
            $user = new User;
            $user->name = $user_fb->getProperty('name');
            $user->uid_fb = $user_fb->getProperty('id');

            $user->save();
        }

        $user->access_token_fb = $this->fb->getToken();
        $user->save();

        $user_pages = $this->fb->getPages();
        // var_dump($user_pages);

        Auth::login($user);

        return Redirect::to('/')->with(array('pages' => $user_pages));

    }

ROUTE

Route::get('/', function()
{
    $user = array();
    if(Auth::check()) {
        $user = Auth::user();
    }
    return View::make('hello', array('user' => $user));
});

So I want to send $user_pages, right now I have access to only $user. Tried running a foreach loop for $pages in my view, but doesnt identify the variable. So i am guessing I have to do something in routes and send it the same way I am sending $data.

Any Help?

3
  • Why on earth would you call a user $data? You make things more confusing for yourself and others if you come up with arbitrary variable names. Commented Jul 5, 2016 at 18:40
  • I don't see the controller method callback() being called in your code. In the declared route you're only returning the data variable (which I would rename to user). I'd recommend you to watch the Laravel beginner video's on Laracast. Commented Jul 5, 2016 at 18:40
  • have changed the variable name to user. and the callback is working fine, I just haven't posted the entire code, only the relevant code for this problem. on how to send the $user_pages to my view. Commented Jul 5, 2016 at 18:45

1 Answer 1

1

The with method flash data to the session so to retrieve it you need to use session('key');

Controller:

public function callback(){
    //.......
    return Redirect::to('/')->with('pages', $user_pages);
    //.......
}

Then:

Route::get('/', function() {
    //From session
    $user_pages = Session::get('pages'); //session('pages'); for laravel >= 5.0
    $data = array();
    if(Auth::check()) {
        $data = Auth::user();
    }
    return View::make('hello', array('data' => $data));
});
Sign up to request clarification or add additional context in comments.

4 Comments

What version of laravel are you using ?
Session::get('pages'); may work instead of session('pages');.
@Press exactly ! he uses version 4.2 ;)
using laravel 4.2 & Session::get('pages'); did work. Thank you

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.