Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have three tables which are User table, Events table, and Events_user table. I have four columns in the Events_user table which are :

   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();
        });

I have a function which add an event base on the user id, show one user can have many of the events. But the event can share to other user. The function is like this :

  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;
    }

Then it will help me update my events_user table, but how do I update the column which is eventstatus? Can anyone show me how could I update it? Cause I want to use the eventstatus help me determine if there is '1' then cant be edit, then the edit button will disappear. How many way can accomplish to the function?


For the show page: I have use the function when the user click the event that exist on the calendar view, then the javascript will pass the id back and redirect to the view with the id.

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

How about when i want to pass the eventstatus into the view? So i can check on my view, if the eventstatus equal to one, the edit button wont show up.


Updated relationship model

User.php

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

Events.php

   public function user(){
        return $this->belongsToMany('App\User')->withPivot('eventstatus');
    }
like image 905
Ben Avatar asked Jan 28 '16 06:01

Ben


2 Answers

If you want to update the field while making the association, you can pass an array of field=>value pairs as the second parameter to the attach() method. You can read in the documentation here.

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

If you want to update the field for an existing association, you can pass an array of field=>value pairs to the save() method:

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

If you want to access the field to read the data, you can access it through the pivot property:

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

In addition to all of this, you need to make sure your relationship definitions include access to the extra property:

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

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

Edit

updated answer for additional question

To get the information you're looking for, you need to be able to get to the pivot record. The pivot record is available on the models in the loaded relationship.

Since you have the Event, you need to go through the related User models, find the user you want (assuming the logged in user), and then get the status off of the pivot attribute on that user.

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'));
}

In addition to all this, make sure both relationship definitions include the ->withPivot('eventstatus'). You want to make sure you can get at it from both sides, if need be.

like image 172
patricus Avatar answered Nov 17 '22 10:11

patricus


Can you share your event and user model relationship code? so , it will help me to get into deep.

OR you can try this in event model like:

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

then while updating you can do something like this iam not sure but hope this help.

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

Thanks

For fetching extra column:

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

but for that you have to define this column in models:

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

// Event class:
public function users() {
    return $this->belongsTo(User::class)->withPivot('eventstatus');
}
like image 24
Amit Ramoliya Avatar answered Nov 17 '22 12:11

Amit Ramoliya