Integer data not saved in table as Eloquent of Laravel 5

1

I have in my database a column called permission that dictates the permissions of users on my system. User 1 is an administrator and user 0 is not.

The problem is that when I save a new user and select by selecting the user's permission type, it saves all user data except the permission field which is a int(11) . What could be happening?

Migrate

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddPermissionToUsersTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function(Blueprint $table)
        {
            $table->integer('permission');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function(Blueprint $table)
        {
            $table->dropColumn('permission');
        });
    }

}

View

@extends('admin.painel.layout')

@section('conteudoPainel')

    <h2>Usuários: [Novo]</h2>

    {!! Form::open(['role' => 'form', 'method' => 'POST', 'route' => 'admin.usuarios.store']) !!}
        {!! Form::label('Nome') !!}
        {!! Form::text('nome', Input::old('nome'), ['class' => 'form-control']) !!}
        <br />
        {!! Form::label('E-mail') !!}
        {!! Form::text('email', Input::old('email'), ['class' => 'form-control']) !!}
        <br />
        {!! Form::label('Senha') !!}
        {!! Form::password('senha', ['class' => 'form-control']) !!}
        <br />
        {!! Form::label('Permissão') !!}<br />
        {!! Form::select('permissao', [0=>'Usuário padrão', 1=>'Administrador'], Input::old('permissao'), ['class'=>'input-sm', 'style'=> 'border: 1px solid #ccc;']) !!}
        <br />
        <br />

        <button type="submit" class="btn btn-success">
          <i class="glyphicon glyphicon-floppy-saved"></i> Cadastrar
        </button>

        <a href="/admin/usuarios" class="btn btn-danger">
            <span class="glyphicon glyphicon-remove"></span>
            Cancelar
        </a>

    {!! Form::close() !!}
@stop

Controller

public function store(NewUserFormRequest $request)
    {
        $data = [
            'name' => $request->get('nome'),
            'email' => $request->get('email'),
            'password' => Hash::make($request->get('senha')),
            'permission' => $request->get('permissao')
        ];

        if($user = User::create($data)){
            return redirect()->to('admin/usuarios/'.$user->id."/edit")->with(['alertaOk' => 'Usuário cadastrado com sucesso!']);
        } else {
            return redirect()->to('admin/usuarios')->withErrors('Não foi possivel cadastrar o usuario. Entre em contato com o administrador para mais informações!');
        }

    }
    
asked by anonymous 26.05.2015 / 20:35

1 answer

1

Using the create method of a model Eloquent Laravel assigns properties via mass assignment

By default the model User already comes with this pre-populated property:

class User extends Model {

    protected $fillable = ['first_name', 'last_name', 'email'];

}

Since you have added a new field in your table ( permission ), you must also add this field in the property to allow it to be inserted from create() :

protected $fillable = ['first_name', 'last_name', 'email', 'permission'];

As noted in the document, this type of filter is required to avoid a potential risk of inserting unwanted data into the database, such as id or even password .

    
26.05.2015 / 20:50