Advertisement
langbung01

laravel:insert database

Aug 6th, 2018
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.68 KB | None | 0 0
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Posts;
  4.  
  5. class PostsController extends Controller
  6. {
  7.     //
  8.     public function index(){
  9.         $posts = Posts::all();
  10.         return view("post.index",compact("posts"));
  11.     }
  12.  
  13.     public function create(){
  14.         return view("post.create");
  15.     }
  16.  
  17.     public function store(){
  18.         $post = new Posts;
  19.         $post->title = request("title");
  20.         $post->body = request("body");
  21.         $post->save();
  22.  
  23. //        or
  24. //      must set $fillable in model
  25.        
  26.         Posts::create([
  27.             "title" => request("title"),
  28.             "body" => request("body")
  29.         ]);
  30.         return redirect("posts");
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement