laravel relation belongtomanythrough code example
Example 1: laravel polymorphic model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Image extends Model
{
public function imageable()
{
return $this->morphTo();
}
}
class Post extends Model
{
public function image()
{
return $this->morphOne('App\Image', 'imageable');
}
}
class User extends Model
{
public function image()
{
return $this->morphOne('App\Image', 'imageable');
}
}
Example 2: laravel eloquent associate
$account = App\Account::find(10);
$user->account()->associate($account);
$user->save();