Greetings guys can you point to me what is the problem here?

This is the code that produces the error. That is in my views:

<h7 id="header-label">Welcome, {{ $fname }} |</h7>

This is the controller:

class ProfileController extends BaseController {


    public function showProfile() {
        $items = DB::table('item_tbl')->get();
     //     Auth::check();

     //     if (Auth::check())
        // {
        //     return "true";
        // } else {
        //  return "false";
        // }
        //$fname = Auth::user()->[firstname];
        // var_dump(Session::get('fname'));
        return View::make('profile')->with('items', $items);
    }
}

and this is the model:

if (Auth::attempt(array('username' => $data['username'], 'password' => $data['password'])))
        {   
            $fname = Auth::user()->firstname;
            return Redirect::to('profile');
            //return View::make('profile')->with('name', $fname);
            //return Redirect::to('profile')->with('name', $fname);
            // $fname =;

        } else {
            return Redirect::back()
                ->with('message', 'Invalid username/password');
        }

This is my route:

Route::get('profile', 'ProfileController@showProfile');

Recommended Answers

All 11 Replies

I break those codes down. I found out that; when i use the Session::get('fname') it returns null.

Sorry i forgot to mention the models name. the model's name is User

Controller - ProfileController
Views - profile.blade.php

wrong forum sorry

Hi, you can do:

Redirect::to('profile')->with('fname', Auth::user()->firstname);

Or:

Session::put('fname', Auth::user()->firstname);
Redirect::to('profile');

Then you have to get the value from session:

$data['items'] = DB::table('item_tbl')->get();
$data['fname'] = Session::get('fname');
return View::make('profile')->with('items', $data);

But:

  1. when using with() the value is flashed to session, and won't be available in the next request
  2. you can get the value directly in the controller:

    $data['fname'] = Auth::user()->firstname;
    

And also it always return false when i use Auth::check()

Can you show the login process (form and receiving route)? To restrict the access to the profile controller use the auth filter:

class ProfileController extends BaseController {

    public function __construct()
    {
        $this->beforeFilter('auth');
    }

To restrict the single method you can apply the same or at route level:

Route::get('profile', array(
    'before' => 'auth',
    'uses'   => 'ProfileController@showProfile'
    ));
commented: Just what i'm looking for! +2

cereal posting like a pro! Thank you sir! you saved me from a lot of work time. Thanks!

But sir can i ask you a question?

I tweaked my code before i posted this topic. I found out that i can only access the fname on the url with localhost/laravel/public/authenticate

public function authenticate() {

        $username = Input::get('username');
        $password = Input::get('password');

        $login = User::tryAuthenticate(Input::all());

        return $login;
        //return Redirect::to('profile')->with('name', $fname);

        }
    }

And when i go to /profile the value assigned to fname is gone. what happen to the value i assigned on fname?

and why can i just use Auth::user()->firstname on the view page? like this ?

{{ Auth::user()->firstname }}

The Auth class is bounded to the User model, so when the authentication is satisfied you can access his information, check:

in particular read the Accessing The Logged In User paragraph.

In your case you can do:

public function authenticate()
{
    $fname = User::tryAuthenticate(Input::all());
    return Redirect::to('profile')->with('name', $fname);
}

And make the model method return the name, but it is not necessary, as you can always use Auth::user(), so:

public function authenticate()
{
    # true
    if(User::tryAuthenticate(Input::all())) return Redirect::to('profile');

    # false
    return Redirect::to('login')->with('error', 'Error: wrong credentials.');
}

And make User::tryAuthenticate() just return true or false.

Another question sir. I tried this and it says false. it should be true right?

    public function showProfile() {
        $items = DB::table('item_tbl')->get();

        if (Auth::check()) {
            return "true";
        } else {
            return "false";
        }

        // $name = Session::get('fname');
        // return View::make('profile')->with('name', $name)->with('items', $items);


     //     if (Auth::check())
        // {
        //     return "true";
        // } else {
        //  return "false";
        // }
        //$fname = Auth::user()->[firstname];
        // var_dump(Session::get('fname'));

    }

Got my problem fixed. The problem is i was using admin_id on my database when my supervisor changed it to id it fixed my problem. So he told me that laravel is strict with convention.

In such cases you can also set the primary key in the (User) model:

<?php

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

    protected $table = 'users';
    protected $primaryKey = 'admin_id';

API:

Bye!

Ooooooh! didn't know that. Thanks! So it means i can override the default primary key set by the laravel. Thanks sir.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.