Saturday, March 18, 2017

Saving form fields dynamically in Laravel

I have a form with a lot of inputs. I do not want to get them one by one. I was thinking about getting them in a loop, or something. So instead of writing this:

$page = new Pages(); //my model
$page->title = $request->input('title');
$page->url_name = $request->input('url_name');
$page->category_id = $request->input('category_id');
$page->page_cols = $request->input('page_cols');
//.......... 30 more 
$page->save();

I was thinking about using a loop:

 foreach ($request->input() as $key => $value) {
   if ($key != "_token"){
   // assign them to the $page
   }
 }
 $page->save

But I m not sure how to do that. IF you have any ideas, please share it with me. Thx !



via Chester

Advertisement