0

I have the following model for Users:

class User extends Authenticatable
{
    use Notifiable;
    protected $table = 'login_info';

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

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function getDashboards()
    {
        return \DB::table('dashboard')
               ->select('type')
               ->where('id', Auth::id())
               ->orderBy('column', 'asc')
               ->get();
    }
}

Users have different information in many tables

  • user info like name, office, dashboard,2FA etc

Is the way I do it now "best practice" (like the getDashboards function) for getting information from different tables?

Or should I create a model for each of the tables and then "join them" (hasMany, belongsToMany, and so on) for each of the tables?

EDIT:

I am now using models, but the result of the query is always an empty array.

class Dashboard extends Model
{
    protected $table = 'dashboard';

    public function user()
    {
        return $this->belongsTo(User::class,'user_id','id');
        //user_id
    }
}

user_id is the id of the user which is used in the login_info table.

And in the User class I have:

public function dashboards()
{
    return $this->hasMany(Dashboard::class,'id','user_id');
}

In the login controller I have:

$user = \App\User::find(1);
$user->dashboards;

Anyone see what the problem could be?

Thanks for any help!

2
  • You should create a separate model for each table Commented Jun 10, 2018 at 18:27
  • definitely create a model for each of the tables Commented Jun 10, 2018 at 18:52

3 Answers 3

1
public function dashboards()
{return $this->hasMany(\App\Dashboard::class);
}

And in your Dashboard Model you do it this way

protected $casts = [
        'user_id' => 'int',
    ];
public function user()
    {
        return $this->belongsTo(\App\User::class);
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Sweet, that worked! What was the problem with the way I had done it, thought that was the right way?
you forget casts,and the way you had declared it
I see. Thanks again! :)
1

The more Laravel way is to rather created the related Dashboard model and use the eloquent relationships, and harness the features of the ORM. Nothing wrong to include an orderBy on the relationship if you always need ordering on that column.

class User extends Authenticatable
{
    public function dashboards()
    {
        return $this->hasMany(Dashboard::class)
            ->orderBy('column', 'asc');
    }
}

class Dashboard extends Model
{
    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

Comments

0

You do not have to do anything in the model! Just refer to the model in the controller, for example:

User::where('id', Auth::id())->pluck('type');

Comments

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.