Laravel eloquent:将belongsToMany关系与hasOne关系组合起来

huangapple go评论102阅读模式
英文:

Laravel eloquent: Combine belongsToMany Relation with hasOne Relation

问题

Is there any chance to combine belongsTo relation with a hasOne relationship?

我有例如以下表格:

users
id
username

events
id
name

states
id
name

event_user
id
event_id
user_id
state_id

在事件模型中,有一个返回belongsTo关系的函数。但我还需要获取State关系。

在事件模型中,有一个返回belongsTo关系的函数。但我还需要获取State关系。

有人有想法吗?

LinuTuris

英文:

is there any chance to combine belongsTo relation with an hasOne relationship?

I have for example this tables:

    users
    id
    username

    events
    id
    name

    states
    id
    name

    event_user
    id
    event_id
    user_id
    state_id

In event model is this function which returns an belongsToMany Relation. But I need to get the State Relation too.

In event model is this function which returns an belongsToMany Relation. But I need to get the State Relation too.

Has anyone an idea?

LinuTuris

答案1

得分: 1

你可以考虑使用 Pivot:

namespace App\Models;
 
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
 
class EventUser extends Pivot
{
  
    public function state(): BelongsTo
    {
        return $this->belongsTo(State::class);
    }
}

在 User 模型内:

class User extends Model
{
    public function events(): BelongsToMany
    {
        return $this->belongsToMany(Event::class)->using(EventUser::class)->withPivot('state_id');
    }
}

然后:

$user = User::find($id);

foreach ($user->events as $event) {
   $state = $event->pivot->state;
}

参考链接:https://laravel.com/docs/10.x/eloquent-relationships#retrieving-intermediate-table-columns

另一种方法是使用 hasMany:

namespace App\Models;
    
use Illuminate\Database\Eloquent\Model;     
use Illuminate\Database\Eloquent\Relations\BelongsTo;
     
class EventUser extends Model
{
  
    public function state(): BelongsTo
    {
       return $this->belongsTo(State::class);
    }

    
    public function event(): BelongsTo
    {
       return $this->belongsTo(State::class);
    }
}

在 User 模型内:

class User extends Model
{
   public function eventUsers(): BelongsToMany
   {
      return $this->hasMany(EventUser::class);
   }
}

然后:

$user = User::with('eventUsers.event', 'eventUsers.state')->first();
英文:

You can consider using Pivot:

namespace App\Models;
 
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
 
class EventUser extends Pivot
{
  
    public function state(): BelongsTo
    {
        return $this->belongsTo(State::class);
    }
}

Within User model:

class User extends Model
{
    public function events(): BelongsToMany
    {
        return $this->belongsToMany(Event::class)->using(EventUser::class)->withPivot('state_id');;
    }
}

Then:

$user = User::find($id);

foreach ($user->events as $event) {
   $state = $event->pivot->state;
}

Reference https://laravel.com/docs/10.x/eloquent-relationships#retrieving-intermediate-table-columns

Another way is using hasMany:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;     
use Illuminate\Database\Eloquent\Relations\BelongsTo;
     
class EventUser extends Model
{
  
    public function state(): BelongsTo
    {
       return $this->belongsTo(State::class);
    }

    
    public function event(): BelongsTo
    {
       return $this->belongsTo(State::class);
    }
}

In User model:

class User extends Model
{
   public function eventUsers(): BelongsToMany
   {
      return $this->hasMany(EventUser::class);
   }
}

Then:

$user = User::with('eventUsers.event', 'eventUsers.state')->first();

huangapple
  • 本文由 发表于 2023年5月8日 00:10:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76194991.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定