简体   繁体   中英

save post data to database in 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?

尝试使用create()

return $this->create($contactData);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM