splendidkeen
1 year ago

Method update does not exist (Event destroy method)

Posted 1 year ago by splendidkeen

Hello, I am currently trying to update a record of status_id for each Event's participant's record.

I face two problems:

  1. I run into an error of method ::update does not exist, when I try to update the status_id to 5, on each related array of the collection $participants

  2. When queuing a SendUserEventDeletedMail Job, I try to send an email to each of the participants->user_id->email. Somehow I run into an exception of undefined variable $participant

public function destroy($id){
                
                $event = Auth::user()->events()->findOrFail($id);
                $participants = $event->participants()->get();


                dd($participants); // I am getting a collection of arrays of Participants, with all the data. Then I want to update status_id on each of the arrays, running into update does not exist.

                $participants->where('event_id', $event->id)->update([ 
                    'status_id' => 5, 
                ]);

        // Here, I want to update every participants record's status_id to 5, which is equal to Event was deleted.

                dd($participants);
            
                if($event->delete()){

                    $participants = $event->participants()->get();

                    dispatch(new SendUserEventDeletedMail($participants));

                }; 

                return redirect()
                    ->route('partner.dashboard.events');
        }
class SendUserEventDeletedMail implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $participants; 
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($participants)
    {
        $this->participants = $participants; 
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        foreach($participants as $user) // Here I get undefined variable $participants
        {
            $email = new UserEventDeletedMail($this->user);
            
            Mail::to($user->user_id->email)->send($email);
        }  // As the collection I am getting (participants) is a record filled with integers, see table below, how can I grab the user_id records, user->email record?

        
    }
}

participants table

public function up()
    {
        Schema::create ('participants', function(Blueprint $table){
             $table->increments('id');
             $table->integer('user_id');
             $table->integer('event_id');
             $table->integer('status_id');
             $table->timestamps();
         });
    }

Any suggestions for the update method?

Thank you

Please sign in or create an account to participate in this conversation.