2014-09-27 91 views
0

我正面临以下错误,当我尝试创建数据到数据库中时: [2014-09-27 09:02:40] production.ERROR:exception'Illuminate \ Database \ Eloquent \在电子商务MassAssignmentException”有消息 '名':\网络\ XAMPP \ htdocs中\ laravel \引导\ compiled.php:6397laravel 4.2未创建到数据库中

在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 

和routes.php文件我的路线设置为:

Route::get('authors', array('uses'=>'[email protected]', 'as'=>'authors')); 
Route::post('authors/create', array('uses'=>'[email protected]', 'as'=>'create_author')); 

在我Authorscontroller.php文件以下等为代码:

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

这里是版本4.2 laravel问题?

+0

可能重复[Laravel 4照亮\数据库\雄辩\ MassAssignmentException错误](http://stackoverflow.com使用质量分配/ questions/22747061/laravel-4-illuminate-database-eloquent-massassignmentexception-error) – Marwelln 2014-09-27 10:22:53

回答

0

在你的模型中你必须给私人$守卫=数组('ID');这里的id是表的主键,这意味着当你使用create或者update方法时,使用批量赋值时,保护数组中的给定字段将不会被更新!您必须在模型中定义此属性格式使用create()或update()方法

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

Sam谢谢你的回复,现在它的工作 – 2014-09-28 05:54:19

+0

随时..你是最受欢迎的.. :) – 2014-09-29 09:13:24