0

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();
16
  • 4
    Show us your models and what you already tried. Commented Oct 1, 2019 at 4:13
  • 1
    What are active() and notAdmin(). They are not defined in the user model Commented Oct 1, 2019 at 7:06
  • 1
    It seems that you don't have method 'category' in your user model, based on query you are using. try like $staff_picks = User::notAdmin()->active()->orderBy('id','desc')->take(10)->with('athlete','team')->get(); Commented Oct 1, 2019 at 7:06
  • 1
    Is it working now? Commented Oct 1, 2019 at 7:11
  • 1
    Are your other tables populated as well? Commented Oct 1, 2019 at 7:15

2 Answers 2

4

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) 
}
Sign up to request clarification or add additional context in comments.

Comments

1

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);
}

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.