英文:
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();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论