Support the ongoing development of Laravel.io →
posted 9 years ago
Eloquent

Hi, I'm new to Laravel.

I use the Laravel 5.2 default authentication; in App/User.php we have the default:


namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

Then, for users, I want to set a relation like:


public function posts( ) 
    { 
        return $this->hasMany('App\Post'); 
    } 

But to do this I need to extend Model and not Authenticatable like the user class above; so the question is: can I insert the relation inside the "class User extends Authenticatable" (maybe dependency injection?), and if yes in wich way? Thanks!

Last updated 3 years ago.
0

Illuminate\Foundation\Auth\User extends model - so you should be able to include the relation as you suggest out of the box.

Last updated 9 years ago.
0
Solution

thank you Simon, Now I understand it!

0

Sign in to participate in this thread!

PHPverse

Your banner here too?

clabal clabal Joined 22 Jan 2016

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.