Laravel create method

百般思念 提交于 2019-12-13 04:08:07

问题


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

}

回答1:


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'];


来源:https://stackoverflow.com/questions/27293177/laravel-create-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!