{"_id":"57ffd85f755a2e1700c2bb18","parentDoc":null,"user":"567082433a32d20d00c45cab","__v":0,"category":{"_id":"57ffd85f755a2e1700c2bb07","project":"567083021dabd80d00b122ae","__v":0,"version":"57ffd85f755a2e1700c2bb05","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-12-18T23:07:56.909Z","from_sync":false,"order":1,"slug":"tutorial","title":"Tutorial"},"version":{"_id":"57ffd85f755a2e1700c2bb05","project":"567083021dabd80d00b122ae","__v":1,"createdAt":"2016-10-13T18:54:23.018Z","releaseDate":"2016-10-13T18:54:23.018Z","categories":["57ffd85f755a2e1700c2bb06","57ffd85f755a2e1700c2bb07","57ffd85f755a2e1700c2bb08","57ffd85f755a2e1700c2bb09","57ffd85f755a2e1700c2bb0a","57ffd85f755a2e1700c2bb0b","57ffd85f755a2e1700c2bb0c","57ffd85f755a2e1700c2bb0d"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"3.4.0","version":"3.4"},"project":"567083021dabd80d00b122ae","updates":["56bc16f6e59a312b00a15c67"],"next":{"pages":[],"description":""},"createdAt":"2015-12-18T23:09:10.887Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":3,"body":"Now we need to write the create function inside the controller.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?php\\n\\nnamespace App\\\\Http\\\\Controllers;\\n\\nuse App\\\\Post;\\nuse App\\\\Http\\\\Requests;\\nuse Illuminate\\\\Http\\\\Request;\\n\\nclass CreatePostController extends Controller\\n{\\n  public function create(Request $request)\\n  {\\n   \\t\\t$this->validate($request, [\\n        'name'  => 'required',\\n        'topic' => 'required',\\n        ]);\\n    \\n      $post = new Post;\\n      $post->name = $request->input('name');\\n      $post->topic = $request->input('topic');\\n      $post->save();\\n    \\n      return response()->success(compact('post'));\\n  }\\n  \\n}\",\n      \"language\": \"php\",\n      \"name\": \"CreatePostController.php\"\n    }\n  ]\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"warning\",\n  \"body\": \"This tutorial assumes you have created the model and appropriate migration for the table `posts`.\\n\\nIf you're not comfortable with this, make sure to follow the official [laravel tutorials](https://laravel.com/docs/5.3/quickstart-intermediate) before reading this.\"\n}\n[/block]\n> Prefer a screencast?\n[block:html]\n{\n  \"html\": \"<iframe width=\\\"560\\\" height=\\\"315\\\" src=\\\"https://www.youtube.com/embed/_ZWV9KBK2N8\\\" frameborder=\\\"0\\\" allowfullscreen></iframe>\"\n}\n[/block]","excerpt":"","slug":"laravel-controller","type":"basic","title":"Laravel Controller"}

Laravel Controller


Now we need to write the create function inside the controller. [block:code] { "codes": [ { "code": "<?php\n\nnamespace App\\Http\\Controllers;\n\nuse App\\Post;\nuse App\\Http\\Requests;\nuse Illuminate\\Http\\Request;\n\nclass CreatePostController extends Controller\n{\n public function create(Request $request)\n {\n \t\t$this->validate($request, [\n 'name' => 'required',\n 'topic' => 'required',\n ]);\n \n $post = new Post;\n $post->name = $request->input('name');\n $post->topic = $request->input('topic');\n $post->save();\n \n return response()->success(compact('post'));\n }\n \n}", "language": "php", "name": "CreatePostController.php" } ] } [/block] [block:callout] { "type": "warning", "body": "This tutorial assumes you have created the model and appropriate migration for the table `posts`.\n\nIf you're not comfortable with this, make sure to follow the official [laravel tutorials](https://laravel.com/docs/5.3/quickstart-intermediate) before reading this." } [/block] > Prefer a screencast? [block:html] { "html": "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/_ZWV9KBK2N8\" frameborder=\"0\" allowfullscreen></iframe>" } [/block]