简体   繁体   中英

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="{{$project->id}}">
                    <td>
                        <input type="hidden" value="{{$project->status}}" name="status"> 
                        <input {{isset($project['status']) && $project['status'] == '2' ? 'checked' : ''}} 
                                value="{{$project->status}}" type="checkbox" name="status" 
                                onchange="document.getElementById('statusForm').submit()"
                        >
                    </td>
                </form>
                <td>{{$project->applicant_name}}</td>
                <td>{{$project->project_name}}</td>
                <td><a href="/events/{{$projects->id}}/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');

First, you cant select by ID document.getElementById('statusForm').submit() when you have multiple DOMS with the same ID.

change your loop to something like this

@foreach ($projects as $project)
    <tbody>
        <tr>
             <td>
                 <form action="/projects/plan" method="post" id="statusForm{{$project->id}}">
                @csrf
                 <input name="id" type="hidden" value="{{$project->id}}">
                 <input {{isset($project['status']) && $project['status'] == '2' ? 'checked' : ''}} 
                     value="2" type="checkbox" name="status" 
                     onchange="document.getElementById('statusForm{{$project->id}}').submit()"
                 >
                 </form>
            </td>
            <td>{{$project->applicant_name}}</td>
            <td>{{$project->project_name}}</td>
            <td><a href="/events/{{$projects->id}}/edit" class="btn btn-secondary btn-sm" role="button">Project Details</a></td>
        </tr>
    </tbody>

@endforeach

Now, a checkbox will only be sent in a form when it is checked, so no need for a variable value for that input

<input {{isset($project['status']) && $project['status'] == '2' ? 'checked' : ''}} 
    value="2" type="checkbox" name="status" 
    onchange="document.getElementById('statusForm{{$project->id}}').submit()"
>

Finally, when you recover the input status , set a default value for the unchecked one (you can remove the hidden input with this one). Or as you did, set a hidden input with the original value to be sent every time. Both solution are perfect.

public function plan(Request $request)
{
    $status = $request->input('status', Project::STATUS_requested);
    Event::find($request->id)->projectAccept();
    return Project::STATUS_ACCEPTED;
}

That way if it is checked it will be 2 (in the request) and if not, it will be 1 from the default value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM