如何在laravel中的列中插入值?

时间:2016-06-19 10:00:03

标签: php laravel laravel-5

我已经启动并运行了'make:auth()'并且我在迁移中做了很少的修改,即添加了一个名为'admin'的布尔字段,默认情况下我将该字段设为0。现在我在视图'Make Admin'中创建了一个按钮,我希望此按钮将管理字段从零更新为1.

我厌倦了以下但是没有更新数据库列值。

控制器:

public function makeAdmin($id)
    {
        $user = User::find($id);
            if($user){
                $user->admin = '1';
                $user->save();
                return Redirect::to('/users');
            }
}

查看

@extends('layouts.app')

@section('content')
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<div class="container">
    <div class="row">
        <div class="col-md-10 col-md-offset-1">
            <div class="panel panel-default">
                <div class="panel-heading">All Registered User(s)</div>

                <div class="panel-body" id="toPrint">
                   <table class="table table-striped">
                            <thead>
                              <tr>
                                <th>ID</th>
                                <th>Name</th>
                                <th>Email</th>
                                <th>Password</th>
                                <th>Role</th>
                                <th>Edit</th>
                                <th>Delete</th>
                              </tr>
                            </thead>
                            <tbody>
                                @foreach($users as $user)
                                  <tr>
                                    <td>{!! $user->id !!}</td>
                                    <td>{!! $user->name !!}</td>
                                    <td>{!! $user->email !!}</td>
                                    <td>{!! $user->password !!}</td>
                                    <td>
                                        {!! Form::open(array('action'=>'Users@makeAdmin', 'url' => "/users" , 'method' => 'POST')) !!}
                                            {!! Form::hidden('id', $user->id) !!}
                                                <button type="submit" class="btn btn-default">Make Admin</button>
                                        {!! Form::close() !!}
                                    </td>
                                    <td>
                                        {!! Form::open(array('url' => "/users/$user->id/edit" , 'method' => 'GET')) !!}
                                            {!! Form::hidden('id', $user->id) !!}
                                                <button type="submit" class="btn btn-default">Edit</button>
                                        {!! Form::close() !!}
                                    </td>
                                    <td>
                                        {!! Form::open(array('url' => '/users/destroy' ,  'method' => 'delete')) !!}
                                            {!! Form::hidden('id', $user->id) !!}
                                                <button type="submit" class="btn btn-default">Delete</button>
                                        {!! Form::close() !!}
                                    </td>

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

                </div>
            </div>
        </div>
    </div>
</div>

@endsection

2 个答案:

答案 0 :(得分:0)

您添加了布尔字段并尝试使用字符串值更新(&#39; 1&#39;)..这也可能是一个问题。

答案 1 :(得分:0)

确保将新列添加到$fillable模型中的User字段,因为所有Eloquent模型都可以保护您免受批量分配。

应用/ user.php的

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'name', 'admin', 'email', 'password',
];

您还可以阅读Mass Assignment Docs