Whole data not saved in table as the Eloquent of Laravel 5

Asked

Viewed 267 times

1

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

The problem is that when I save a new user and select through select the type of user permission, it saves all user data, except the field permission which is a int(11). What might be going on?

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!');
        }

    }
  • what returns a dd($request->get('permissao')) ?

  • When I select administrator it returns 1, and when I select default user it returns 0. But only saved in database as 0.

  • How is the property $fillable in his model User ?

  • protected $fillable = ['name', 'email', 'password'];

1 answer

1


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

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

class User extends Model {

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

}

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

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

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

  • It worked. Thank you!

  • @Samuel, even though it is not the scope of the question, revise the code of your form. You are allowing the user to change the password without any prior confirmation (the old password). There is also no verification that the password typed is correct. I strongly recommend reviewing these points, hug!

  • In this case it is only registering a new user, and not updating. For this reason I have not added the old password field. But thanks for watching.

  • Oh yes, I get it. But I think it’s cool that those confirmations that are typed are hitting, a second field of "Confirm Password" :)

  • 1

    I took your advice and added the "confirm password" field Rsrsrsrsrs. Hugs!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.