Skip to content

Instantly share code, notes, and snippets.

@programarivm
Last active October 23, 2019 12:28
Show Gist options
  • Save programarivm/e990795786e54288d724b12cbed19d18 to your computer and use it in GitHub Desktop.
Save programarivm/e990795786e54288d724b12cbed19d18 to your computer and use it in GitHub Desktop.
Many-to-many relationship linked through an entity table
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Restaurant extends Model
{
public function reviews()
{
return $this->hasMany(Review::class);
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Review extends Model
{
public function restaurant()
{
return $this->belongsTo(Restaurant::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
}
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* 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 reviews()
{
return $this->hasMany(Review::class);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment