duanliu6083
2016-07-25 07:07 阅读 188
已采纳

在laravel中将后期数据保存到数据库

I Wanna save post data in Laravel. This is my code:

public function addContact(Request $request){
    $contact = new Contact();
    $contact->addContact($request);
    return back();
}

and in the Model:

public function addContact($contactData){
       return $this->save($contactData);
}

it's saves in the database but the data in it is NOT save. saves like an empty row. if I change it to this, it's working:

public function addContact($contactData){
      $this->user_id = $contactData->user_id;
      $this->text = $contactData->text;
      return $this->save();
}

Why the first one is not working?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    duanchuang1935 duanchuang1935 2016-07-25 07:11

    Try using create()

    return $this->create($contactData);
    
    点赞 评论 复制链接分享
  • dongmu6578 dongmu6578 2016-07-25 07:17

    Are you using $fillable on the model?

    protected $fillable = ['user_id', 'text'];
    

    And in Controller

    public function addContact(Request $request){
        $contact = new Contact();
        $contact->create($request);
        return back();
    }
    
    点赞 评论 复制链接分享

相关推荐