Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save form data in laravel

Tags:

php

laravel

Right now I have a form with gender, options and user_id. My public function store (Request $request) looks like this :

public function store(Request $request)
{
    $task = new Appointment;
    $task->gender = $request->gender;
    $task->options = $request->options;
    $task->user_id = $request->user_id;
    $task->save();
}

This works completely fine but this is just 3 fields ?! Eventually I want my forms 5 times bigger. My function will be huge. Is there a way to save everything with less code?

I found this : $data = Input::all(); This gets all the data but I don't know how to save it in the database.

like image 762
twoam Avatar asked Sep 11 '16 12:09

twoam


1 Answers

You can use mass assignment feature by using create() method:

public function store(Request $request)
{
    Appointment::create($request->all());
}

Don't forget to fill all columns in $fillable array in Appointment model:

protected $fillable = ['gender', 'options', 'user_id', 'another_one'];
like image 77
Alexey Mezenin Avatar answered Oct 20 '22 19:10

Alexey Mezenin