简体   繁体   English

Laravel:选中复选框true或false,然后更新数据库

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

Just started with laravel using phpStorm. 刚开始使用phpStorm编写laravel。 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. 数据库中的所有行都在视图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. 应该允许用户激活或停用(使用复选框)浏览index.html文件时应列出的消息。 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. 我也不确定当复选框被选中或未选中时如何运行功能ActivatedOrDeactivated。

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: 当您想要在用户单击不带ajax的复选框时更新新闻项时,可以使用以下方法:

router.php router.php

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

HTML index.blade.php 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 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 顺便说一句:你可以,如果你只是救自己一些打字路线REST的使用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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