Laravel创建方法 [英] Laravel create method
本文介绍了Laravel创建方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Laravel的create
方法存储数组
$input = Input::all();
$new_media = $this->media->create($input);
或
$input = Input::all();
$new_media = Media::create($input);
两个代码都不起作用.
这是我遇到的错误.
Method [create] does not exist.
此处的Laravel文档 http://laravel.com/docs/4.2/eloquent >
来自laravel文档
// Create a new user in the database...
$user = User::create(array('name' => 'John'));
媒体模型
<?php
class Media extends Eloquent {
protected $table = 'media';
protected $guarded = array();
public static $rules_video = array(
'title' => 'required',
'url' => 'required'
);
public static $rules_image = array(
'title' => 'required'
);
public function category(){
return Category::find($this->category_id);
}
}
解决方案
您需要设置使用create
,fill
或update
的>大量分配.参见此答案.
删除您的guarded
属性,并创建一个名为fillable
的新属性,作为一个数组,其中包含允许通过批量分配进行更新的字段.
protected $fillable = ['title', 'url'];
I'm trying to store an array using Laravel's create
method
$input = Input::all();
$new_media = $this->media->create($input);
or
$input = Input::all();
$new_media = Media::create($input);
both codes does not work.
This is the error I'm getting.
Method [create] does not exist.
Laravel documentation here http://laravel.com/docs/4.2/eloquent
From laravel docs
// Create a new user in the database...
$user = User::create(array('name' => 'John'));
Media Model
<?php
class Media extends Eloquent {
protected $table = 'media';
protected $guarded = array();
public static $rules_video = array(
'title' => 'required',
'url' => 'required'
);
public static $rules_image = array(
'title' => 'required'
);
public function category(){
return Category::find($this->category_id);
}
}
解决方案
You need to setup Mass Assignments when using create
, fill
or update
. See this answer.
Remove your guarded
property and create a new one called fillable
as an array with the allowed fields to be updated with mass assignments.
protected $fillable = ['title', 'url'];
这篇关于Laravel创建方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文