Mitch M
Mitch M

Reputation: 157

Laravel multiple belongsTo users

I'm trying to show Races + people that attend the races, I'm using table relationships but I'm not sure if I'm using it correctly.

view:

@foreach($leagues->races as $race)
        <!-- ATTENDANCE track -->
        <div class="col s12 m6">
          <div class="card">
            <div class="card-image">
              <img src="{{ asset("img/tracks/.png") }}">
              <span class="card-title red attendance-track-box"><b>{{$race->event}}</b></span>
            </div>
            <div class="card-content attendance-content">
                <ul>
                  @foreach($race->attendances as $attendance)
                    @if($attendance->status == 1)
                      <li class="border-left-green">{{$attendance->user->username}}</li>
                    @elseif($attendance->status == 2)
                      <li class="border-left-red"></li>
                    @elseif($attendance->status == 3)
                      <li class="border-left-cyan"></li>
                    @endif
                  @endforeach
                </ul>
            </div>
            <div class="card-action">
              <a class="waves-effect waves-light btn green">Yes</a> <a class="waves-effect waves-light btn red">No</a> <a class="waves-effect waves-light btn">Maybe</a>
            </div>
          </div>
        </div>
        @endforeach

model Leagues:

class Leagues extends Model
{
    //
    public function teams(){
        return $this->hasMany('App\teams');
    }
    public function races(){
        return $this->hasMany('App\races');
    }
}

model Races:

class Races extends Model
{
    //
    public function attendances()
    {
        return $this->hasMany('App\attendances');
    }
}

model Drivers: (use this one for drivers listing)

class Drivers extends Model
{
    //
    public function user(){
        return $this->belongsTo('App\Users', 'users_id', 'id');
    }
}

model Attendance:

class attendances extends Model
{
    //
    public function user(){
        return $this->belongsTo('App\Users', 'users_id', 'id');
    }
}

model Users

namespace App;

use Illuminate\Database\Eloquent\Model;

class Users extends Model
{
    //
}

I've posted the Drivers model since I also call upon belongsTo. basically, I've copied the code and it works for drivers yet it doesn't for attendance

Error I get:

Trying to get property 'username' of non-object

User.php

<?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 $fillable = [
        'username', 'psn', 'email', 'password',
    ];

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

Upvotes: 3

Views: 2432

Answers (1)

Cory Fail
Cory Fail

Reputation: 1090

Your model relationships are only doing the inverse belongsTo. Your model relationships need to go both ways if you want to use belongsTo, but your relationships are only going in the inverse. Define the relationship using hasMany, hasOne, or the like first, then you can define the inverse in the other model.

Example Drivers:

class Drivers extends Model
{
    public function user(){
        return $this->belongsTo('App\Users', 'users_id', 'id');
    }
}

Example Attendances:

class attendances extends Model
{
     public function user(){
         return $this->belongsTo('App\Users', 'users_id', 'id');
     }
}

Example Users:

class Users extends Model
{
     public function attendances()
     {
         return $this->hasMany('App\attendances');
     }

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

Each class needs to reference each other. One with the hasMany, hasOne, or the like to define the relationship and the other needs the belongsTo to define the inverse of the relationship.

Upvotes: 2

Related Questions