Saturday, April 15, 2017

laravel eloquent belongsTo fetches wrong data

I have two tables. One contains the user and the other contains meetings. Each meeting belongsTo exact one User.

Meetings

class Meeting extends Model { 
protected $table = 'meetings';
protected $primaryKey = 'owner_id';

/**
 * A meeting belongs to exact one User
 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
 */
public function user(){

    return $this->belongsTo(User::class, 'id');
}}

User

class User extends Authenticatable
{
use Notifiable;

protected $table = 'users';
protected $primaryKey = 'id';

/**
 * One User can have many Meetings
 * @return \Illuminate\Database\Eloquent\Relations\HasMany
 */
public function meetings(){

    return $this->hasMany(Meeting::class, 'owner_id');
}

I fetch the data with

$meetings = Meeting::with('user')->get();

But somehow i don't get the related user. It just iterates over all users in the database and breaks if no more user is given.

What the heck am I doing wrong? O.o



via Ronon

Advertisement