When working with ORM, Laravel encountered the need to add a new record to the table by assigning all the necessary data to the object. It turned out the following:

$article = new Articles; $article->title = $request->input("title"); $article->link = $request->input("link"); $article->rubric = $request->input("rubric"); $article->section = $request->input("section"); $article->description = $request->input("description"); $article->keywords = $request->input("keywords"); $article->text = $request->input("text"); $article->functions = $request->input("functions"); $article->sort = $request->input("sort"); $article->public = $request->input("public", 0); $article->save(); 

Is it possible to somehow reduce the entire list, or write it more succinctly?

    1 answer 1

    Is it possible to somehow reduce the entire list, or write it more succinctly?

    Because The names of the properties and arguments you have are the same - you can assemble an array and process it in a loop, like this:

     $args = ["title", "link", "rubric", "section", "description", "keywords", "text", "functions", "sort", "public"]; $article = new Articles; foreach ($args as $arg) { if ($arg == 'public') { $article->$arg = $request->input($arg, 0); } else { $article->$arg = $request->input($arg); } } $article->save();