Search code examples
phplaravellaravel-4

4.2 Laravel does not create a database


When I try to insert data into the database, I get the following error:

[2014-09-27 09:02:40] production.ERROR: in E:Webxampphtdocslaravelbootstrapcompiled.php:6397, exception 'IlluminateDatabaseEloquentMassAssignmentException' with message 'name'.

in the file new.blade.php

@extends('layouts.default')

@section('content')

    <h1>New Author page</h1>    

    {{ Form::open(array('url' => 'authors/create', 'method' => 'POST')) }}


    <p>
        {{ Form::label('name', 'Name:') }}
        {{ Form::text('name') }}
    </p>

    <p>
        {{ Form::label('bio', 'Biography:') }}
        {{ Form::textarea('bio') }}
    </p>

    <p>
        {{ Form::submit('Add Author') }}
    </p>

    {{ Form::close() }}

@endsection

and my route is defined in routes.php as:

Route::get('authors', array('uses'=>'AuthorsController@index', 'as'=>'authors'));
Route::post('authors/create', array('uses'=>'AuthorsController@create', 'as'=>'create_author'));

and the following code in my Authorscontroller.php file:

    public function create()
    {
        Author::create(array(
            'name' => Input::get('name'),
            'bio' => Input::get('bio')
            ));
        return Redirect::route('authors');
    }

What is the issue? Laravel 4.2 is the version I'm using.


Solution

  • in your model you have to give private $guarded = array('id'); here id is primary key of the table this means when you use create or update method which uses bulk assignment the given fields in guarded array will not be updated! you must define this propery in your model to use mass assignment using create() or update() method

    private $guarded = array('id'); //id is primary key where it can be any field which will not be updated in mass assignment