abu abu
abu abu

Reputation: 7022

Display 2 types of data in Laravel

I have 2 types of user Athletes and Teams. I created 3 tables users,athletes & teams. I am storing username & password in users table and others information in athletes & teams table. I would like to display Athletes and Teams information in home page.

My User model is like below

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = [];

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

    protected $fillable = ['email','password','remember_token','category_id'];

    public function team()
    {
        return $this->hasOne(Team::class);
    }

    public function athlete()
    {
        return $this->hasOne(Athlete::class);
    }
}

My Team model is like below

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Team extends Model
{    
  public $guarded = [];

  protected $fillable = ['first_name','last_name','user_id','how_did_find','social_media',];

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

My Athlete model is like below

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Athlete extends Model
{    
  public $guarded = [];

  protected $fillable = ['user_id','social_media','youtube_video','website'];

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

I am using below code in controller.

$staff_picks = User::orderBy('id','desc')->take(10)->with('athlete','team')->get();

Upvotes: 0

Views: 198

Answers (2)

Vijay Sankhat
Vijay Sankhat

Reputation: 341

This is how it should be

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = [];

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

    protected $fillable = ['email','password','remember_token','category_id'];

    public function team()
    {
        return $this->hasOne(Team::class);
    }

    public function athlete()
    {
        return $this->hasOne(Athlete::class);
    }
}


class Team extends Model
{    
  public $guarded = [];

  protected $fillable = ['first_name','last_name','user_id','how_did_find','social_media',];

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

class Athlete extends Model
{    
  public $guarded = [];

  protected $fillable = ['user_id','social_media','youtube_video','website'];

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

And the query

$staff_picks = User::orderBy('id','desc')->take(10)->with('athlete','team')->get();

And the iteration

foreach($staff_picks as $r){ 
    $r->teams->social_media; //(user can be either teams or athletes so must check for null) 
    $r->athletes->social_media; //(user can be either teams or athletes so must check for null) 
}

Upvotes: 4

Faizan Khan
Faizan Khan

Reputation: 19

In athlete & team model create a column called user_id and in user model create two methods as hasMany relation for athletes and teams models. After login get the data as

User::where('id', 1)->with('teams', 'athletes')->first();

Relations inside User modal can be written as below.

teams() {
  return $this->hasMany(Team::class);
}
athletes() {
  return $this->hasMany(Athlete::class);
}

Upvotes: 1

Related Questions