mardi 27 août 2019

Update status in database using checkbox - Laravel

The project can have different statuses. With the checkbox, I am trying to switch the status of the project from requested (1) to accepted (2). If the checkbox is unchecked the status is 1, checked it's 2.

When I check the checkbox I got a 419 but this is normally related to the token but I added a @csfr field. Why is the status not changed in the database? Thanks for any help.

index.blade.php (summary of all the projects)

 @foreach ($projects as $project)

        <tbody>
            <tr>
                <form action="/projects/plan" method="post" id="statusForm">
                 @csrf
                    <input name="id" type="hidden" value="">
                    <td>
                        <input type="hidden" value="" name="status"> 
                        <input  
                                value="" type="checkbox" name="status" 
                                onchange="document.getElementById('statusForm').submit()"
                        >
                    </td>
                </form>
                <td></td>
                <td></td>
                <td><a href="/events//edit" class="btn btn-secondary btn-sm" role="button">Project Details</a></td>
            </tr>
        </tbody>

    @endforeach

Project.php (functions to update status)

    const STATUS_requested  = 1;
    const STATUS_ACCEPTED   = 2;

    public function updateStatus( $status )
    {
        $this->update([
            'status'    => $status
        ]);
        $this->refresh();
        return $this;
    }

    public function projectAccept()   { 

        return $this->updateStatus( self::STATUS_ACCEPTED );   

    }

ProjectsController.php (dd('hello') is not printed it seems like data is not sent to this resource)

    public function plan(Request $request)
    {
        dd('hello');
        Event::find($request->id)->projectAccept();
        return Project::STATUS_ACCEPTED;
    }

web.php

// Update status project
Route::post('/projects/plan',                 'ProjectsController@plan');




Aucun commentaire:

Enregistrer un commentaire