jeudi 3 septembre 2015

Laravel 5 extend auth::user() with information from other database table

I am using Auth::user() to get information on the current logged in user. Now I want to extend the Information delivered by Auth::user(). Concrete the activation state of the user shall be available.

This information is stored in the table activations in the column state.

From my understanding Auth::user() delivers what will be retrived from the user model. So I should create a relationship between table users and table activations. The documentation states 2 posibilites, both require that the activations table has a foreign_key user_id.

My table layout does not cover that, I just have refernce in the users table to the actvtion_id.

is there a possibilty to build a relationsship with my table layout, which leads to the desired result, that Auth::user() deliveres me the activation state? If how should I do it?

Or do I have to switch the table layout as recommended in the documentation?

Below my table code:

Controller:

class ProfileController extends Controller {

    public function me()
    {
        return Auth::user(  );
    }
}

users (migration):

    Schema::create('users', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('email')->unique();
        $table->integer('activation_id')->unsigned(); 
        $table->foreign('activation_id')->references( 'id' )->on( 'activations' );
        //...
        $table->timestamps();
    });

activations (migration):

    Schema::create('activations', function(Blueprint $table)
    {
        $table->increments('id');   
        $table->string('activationStatus', 1)->nullable()->default('N');
        // ...
        $table->timestamps();
    });



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1LX90Qx
via IFTTT

Aucun commentaire:

Enregistrer un commentaire