dongzhenbi8919 2014-12-04 11:42
浏览 50
已采纳

Laravel创建方法

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条回答 默认 最新

  • donglang6656 2014-12-04 11:52
    关注

    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'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)