简体   繁体   English

Laravel 5.1:多对多以及一对多关系

[英]Laravel 5.1: Many-to-many alongside one-to-many relationship

I'm making an event RSVP app where a user creates an event (or events), and can allow other users to manage the event with them. 我正在制作一个事件RSVP应用程序,用户可以在其中创建一个(或多个)事件,并可以允许其他用户使用它们来管理事件。 Each event also has an owner (the user that created the event). 每个事件还具有一个所有者(创建事件的用户)。

My Goal: 我的目标:

How I can create an event, assign its owner and also add it to the event_user pivot table at the same time 如何创建事件,分配事件所有者并将其同时添加到event_user数据透视表中

These are my current relationships... 这些是我目前的关系...

Event Class - App\\Event 事件类别 App\\Event

public function owner()
{
    return $this->belongsTo('App\User', 'owner_id');
}

public function managers()
{
    return $this->belongsToMany('App\User');
}

User Class - App\\User 用户类别 App\\User

public function owns()
{
    return $this->hasMany('App\Event', 'owner_id');
}

public function manages()
{
    return $this->belongsToMany('App\Event');
}

Removed migrations as not really relevant. 删除了与迁移无关的迁移。

Updated methods - as per Mina Youssef's suggestion. 更新了方法-根据Mina Youssef的建议。

$user = App\User::find(1);
$event = new App\Event(['name' => 'Event 2']);

$event = $user->owns()->save($event);

$user->manages()->attach($event->id);

This now assigns the owner and attaches the user to the event in the pivot table correctly. 现在,这将分配所有者并将用户正确地附加到数据透视表中的事件。

How can I join the owns and manages methods together when creating an event so I don't have to call them separately? 创建事件时如何将ownsmanages方法结合在一起,这样就不必分别调用它们了?

UPDATE: Added boot event to AppServiceProvider 更新:将boot事件添加到AppServiceProvider

namespace App\Providers;

use App\Event;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Event::created(function ($event) {
            $event->owner->manages()->attach($event->id);
            return true;
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

If you want to add entries to Many to Many relationship, then use the attach() method 如果要向多对多关系添加条目,请使用attach()方法

So you can do the following: 因此,您可以执行以下操作:

$user->manages()->attach($event->id);

However, this works after saving the $event model, to get its id. 但是,在保存$ event模型以获取其ID之后,此方法有效。

To trigger the attach method directly, you can do the following in your AppServiceProvider 若要直接触发attach方法,可以在AppServiceProvider执行以下操作

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Event::created(function ($event) {
            $event->owner->manages()->attach($event->id);
            return true;
        });
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM