Php 更新laravel 5.2中多对多关系表中的列

Php 更新laravel 5.2中多对多关系表中的列,php,mysql,laravel,laravel-5,laravel-5.2,Php,Mysql,Laravel,Laravel 5,Laravel 5.2,我有三个表,分别是用户表、事件表和事件用户表。我在Events_user表中有四列: Schema::create('events_user', function(Blueprint $table){ $table->increments('id'); $table->integer('user_id')->unsigned()->index(); $table->foreign('us

我有三个表,分别是用户表、事件表和事件用户表。我在Events_user表中有四列:

   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\u用户表,但是如何更新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传递到视图中时如何?因此,我可以检查我的视图,如果eventstatus等于1,则编辑按钮不会显示


更新的关系模型

User.php

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

   public function events(){
        return $this->belongsToMany('App\Events');
    }
   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()
方法。您可以在文档中阅读


如果要更新现有关联的字段,可以将
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');
}
除此之外,您还需要确保您的关系定义包括对额外属性的访问:

$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记录。pivot记录在加载关系中的模型上可用

由于您拥有该事件,因此需要遍历相关的用户模型,找到您想要的用户(假设是登录的用户),然后获取该用户的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')
。如果需要的话,您需要确保可以从两个方面获得它。

您总是希望eventstatus的值为“1”?是的,因为我想使用1来确定事件无法编辑。Show我不想显示编辑按钮Show my Show view如果eventstatus=1这是有效的,@patricus但这是用于Show方法的。当我想用eventstatus创建事件时如何?我已经更新了我的问题。您好,@patricus,您设置为零的状态是为了什么目的?
未定义的属性:Illumb\Database\Eloquent\Collection::$pivot
当我想使用pivot时,会出现此错误。@Ben我刚才假设0与您的状态1相反。您可以将其设置为对您的应用程序有意义的任何值。@Ben请确保您是在模型上调用
pivot
,而不是在集合上调用。在我上面的代码中,
find()
方法将返回一个模型,因此应该可以工作。如果你需要进一步的帮助,你可能会更好地发布一个新的问题,新的代码是导致问题。我已经更新了问题。你可以看看。将数据传递到视图后,
$event\u status->eventstatus像这样调用数据?不,我想你只需要使用$event\u status。它将包含您的值(1或0),我是说,无论什么…当我在视图窗体中尝试`{$event\u status}}}`时,它会显示试图获取非对象属性的错误。您不能对这样的事件调用
pivot
pivot
属性位于相关模型上,否则显示的代码如何知道要访问哪个pivot记录?