更新 laravel 5.2 中多对多关系 table 中存在的列

Update a column that exist in the many to many relationship table in laravel 5.2

我有三个 table,它们是用户 table、事件 table 和 Events_user table。我在 Events_user table 中有四列,它们是:

   Schema::create('events_user', function(Blueprint $table){

            $table->increments('id');
            $table->integer('user_id')->unsigned()->index();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

            $table->integer('events_id')->unsigned()->index();
            $table->foreign('events_id')->references('id')->on('events')->onDelete('cascade');

            $table->integer('eventstatus')->unsigned()->index();

            $table->timestamps();
        });

我有一个基于用户 ID 添加事件的功能,显示一个用户可以拥有多个事件。但事件可以分享给其他用户。函数是这样的:

  public function store(EventsRequests $request){

        $id = Auth::user()->id;
        $events= Events::create($request->all());
        $events->user()->attach($id); //Store the User_ID and the Event_ID at the events_user table.

        Flash::success('Your event has been created!');
        return redirect('/');//Return into home page

//        return $events;
    }

然后它将帮助我更新我的 events_user table,但是我如何更新 eventstatus 列? 谁能告诉我如何更新它?因为我想使用 eventstatus 帮助我确定是否有 '1' 然后不能编辑,然后编辑按钮将消失。 有多少种方式可以实现功能?


对于展示页面: 当用户单击日历视图中存在的事件时,我使用了该功能,然后 javascript 会将 id 传回并重定向到具有该 id 的视图。

public function show($id){
//            $user_id=Auth::user()->name;
            $showevents = Events::findOrFail($id);
            return view('events.show',compact('showevents','user'));
    }

当我想将事件状态传递到视图中时怎么样?所以我可以检查我的视图,如果 eventstatus 等于 1,则编辑按钮不会显示。


更新关系模型

User.php

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

Events.php

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

能分享下你的事件和用户模型关系代码吗? 所以,它会帮助我深入。

或 你可以在事件模型中尝试这个:

public function user()
{
    return $this->belongsToMany('App\Event')
        ->withPivot('eventstatus')
        ->withTimestamps();
}

然后在更新时你可以做这样的事情我不确定但希望这能有所帮助。

$events->user()->attach($id,['eventstatus' => 1]);

谢谢

获取额外的列:

public function show($id){
        $showevents = Events::findOrFail($id);
        $event_status = $showevents->pivot->eventstatus;
        return view('events.show',compact('showevents','event_status'));
}

但为此您必须在模型中定义此列:

// User class:
public function events() {
    return $this->belongsTo(Event::class)->withPivot('eventstatus');
}

// Event class:
public function users() {
    return $this->belongsTo(User::class)->withPivot('eventstatus');
}

如果要在进行关联时更新字段,可以将 field=>value 对数组作为第二个参数传递给 attach() 方法。您可以阅读文档 here.

$events->users()->attach($id, ['eventstatus' => 1]);

如果要更新现有关联的字段,可以将 field=>value 对数组传递给 save() 方法:

$user = Auth::user();
$event = $user->events()->first();
$user->events()->save($event, ['eventstatus' => 1]);

如果要访问字段读取数据,可以通过pivot属性:

访问
$user = Auth::user();
foreach ($user->events as $event) {
    echo $event->pivot->eventstatus;
}

除此之外,您还需要确保您的关系定义包括对额外 属性:

的访问
// User class:
public function events() {
    return $this->belongsTo(Event::class)->withPivot('eventstatus');
}

// Event class:
public function users() {
    return $this->belongsTo(User::class)->withPivot('eventstatus');
}

编辑

附加问题的更新答案

要获取您要查找的信息,您需要能够访问数据透视表记录。数据透视记录在加载关系中的模型上可用。

既然你有事件,你需要通过相关的用户模型,找到你想要的用户(假设是登录用户),然后从该用户的 pivot 属性中获取状态。

public function show($id)
{
    // get your event
    $showevents = Events::findOrFail($id);

    // get your user
    $user = Auth::user();

    // default status to send to view
    $status = 0;

    // if there is a user...
    if ($user) {
        // find the user in the "user" relationship
        // NB: calling "find" on a Collection, not a query
        $eventUser = $showevents->user->find($user->id);

        // if the user is associated to this event,
        //   get the status off the pivot table,
        //   otherwise just set the default status
        $status = $eventUser ? $eventUser->pivot->eventstatus : $status;
    }

    // add your status to the data sent to the view
    return view('events.show', compact('showevents', 'user', 'status'));
}

除此之外,确保两个关系定义都包含 ->withPivot('eventstatus')。如果需要的话,你想确保你可以从两方面得到它。