简体   繁体   中英

Laravel: Check checkbox if true or false, then update database

Just started with laravel using phpStorm. Im trying to create a form that take inputs, and a checkbox, then adds it to a database. All of the rows in the database get listed in the view index.html. The user should be allowed to activate or deactivate (with the checkbox) the the message that should be listed when browsing the index.html file. Because if a news article is active, and you want to deactivate it, you should be able to just uncheck the checkbox, and it will be deactivated. So I am wondering how my function should look like.

Im also unsure how to run the function ActivatedOrDeactivated when the checkbox gets checked or unchecked.

Its my first post, so please tell if something is hard to understand in my description.

My route:

Route::get('admin',
    [
        'uses' => 'NyhetsController@index',
        'as' => 'admin.index'
    ]
);


Route::get('admin/create',
    [
        'uses' => 'NyhetsController@create',
        'as' => 'admin.create'
    ]
);

Route::post('admin',
    [
        'uses' => 'NyhetsController@store',
        'as' => 'admin.store'
    ]
);

Route::get('admin/{id}',
    [
        'uses' => 'NyhetsController@show',
        'as' => 'admin.show'
    ]
);

Route::get('admin/{id}/edit',
    [
        'uses' => 'NyhetsController@edit',
        'as' => 'admin.edit'
    ]
);

Route::put('admin/{id}',
    [
        'uses' => 'NyhetsController@update',
        'as' => 'admin.update'
    ]
);

Route::get('admin/{id}/delete',
    [
        'uses' => 'NyhetsController@delete',
        'as' => 'admin.delete'
    ]
);

Route::delete('admin/{id}',
    [
        'uses' => 'NyhetsController@destroy',
        'as' => 'admin.destroy'
    ]
);

My Controller that contains the function (just something i wrote in anger):

public function ActivatedOrDeactivated($id){
$news = News::findOrFail($id);
$input = Input::get('active');

if($input == true)
{
    $news->active = 'false';
    $news->update($input);
}
else{
    $news->active = 'true';
    $news->update($input);
}
}

My index file that contains the form:

<h1>Alle postene i databasen</h1>

<p>{{ link_to_route('admin.create', 'Legg til ny nyhet') }}</p>


@if($news->count())
    <table class="table table-striped table-boarded">
        <thead>
            <tr>ID</tr>
            <tr>Title</tr>
            <tr>Author</tr>
            <tr>Message</tr>
            <tr>Active</tr>
            <tr>Picture path</tr>
            <tr>Activate/Deactivate</tr>
        </thead>

        <tbody>
            @foreach($news as $n)
                <tr>
                    <td>{{ $n->id }}</td>
                    <td>{{ $n->title }}</td>
                    <td>{{ $n->author }}</td>
                    <td>{{ $n->message }}</td>
                    <td>{{ $n->active }}</td>
                    <td>{{ $n->picture_path }}</td>
                    <td>{{ Form::checkbox('active') }}</td>
                    <td>{{ link_to_route('admin.destroy', 'Delete', array($n->id)) }}</td>

                </tr>
            @endforeach
        </tbody>
    </table>

When you want to update the news item when a user clicks the checkbox without ajax this would a way to do it:

router.php

Route::post('admin/{id}/active',
    [
        'uses' => 'NyhetsController@toggleActive',
        'as' => 'admin.toggle_active'
    ]
);

HTML index.blade.php

@if($news->count())
    <table class="table table-striped table-boarded">
        <thead>
            <tr>
                <th>ID</th>
                <th>Title</th>
                <th>Author</th>
                <th>Message</th>
                <th>Picture path</th>
                <th>Active</th>
            </tr>
        </thead>

        <tbody>
            @foreach($news as $n)
                <tr>
                    <td>{{ $n->id }}</td>
                    <td>{{ $n->title }}</td>
                    <td>{{ $n->author }}</td>
                    <td>{{ $n->picture_path }}</td>                        
                    <td>{{ $n->message }}</td>
                    <td>
                        {{ Form::open(array('url' => 'admin/' . $n->id . '/active')) }}
                        {{ Form::submit('', [ 'class' => ($n->active ? 'glyphicon glyphicon-ok' : 'glyphicon glyphicon-remove') ]);
                        {{ Form::close() }}
                    <td>{{ link_to_route('admin.destroy', 'Delete', array($n->id)) }}</td>

                </tr>
            @endforeach
        </tbody>
    </table>
@endif

NyhetsController

public function toggleActive($id) {
    $news = News::findOrFail($id);
    if($news->active)
    {
        $news->active = 'false';
        $news->update($input);
    }
    else{
        $news->active = 'true';
        $news->update($input);
    }
}

return Redirect::to('admin');

Code is untested!

In your Form::

{{ Form::checkbox('active', 'true') }}

In your Controller

$news = News::findOrFail($id);

if (Input::get('active') === 'true') {
    $news->active = 'true';  // UPDATE:must be true of course
    $news->update($input);
} else {
    $news->active = 'false'; // UPDATE:must be false of course
    $news->update($input);
}

By the way: you could save yourself some typing if you simply route restfully using

Route::resource('admin', 'NyhetsController');

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