jeudi 28 janvier 2016

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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire