rezamalik15

method store() pada PostController.php

Jul 19th, 2023
1,010
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.16 KB | None | 0 0
  1. <?php
  2.  
  3. use Illuminate\Support\Facades\Validator;
  4.  
  5. class PostController extends Controller
  6. {
  7.     public function store(Request $request): RedirectResponse
  8.     {
  9.         $rules = [
  10.             'title' => 'required|min:3|max:50',
  11.             'text' => 'required|min:3|max:140',
  12.         ];
  13.        
  14.         $messages = [
  15.             'title.required' => 'Kolom judul harus diisi',
  16.             'title.min' => 'Judul minimal mengandung :min karakter',
  17.             'title.max' => 'Judul maksimal mengandung :max karakter',
  18.             'text.required' => 'Kolom post harus diisi',
  19.             'text.min' => 'Post minimal mengandung :min karakter',
  20.             'text.max' => 'Post maksimal mengandung :max karakter',
  21.         ];
  22.  
  23.         $validator = Validator::make($request->all(), $rules, $messages);
  24.  
  25.         if ($validator->fails()) {
  26.             return redirect('/post/create')
  27.                 ->withErrors($validator)
  28.                 ->withInput($request->input());
  29.         }
  30.  
  31.         $post = new Post;
  32.         $post->title = $request->title;
  33.         $post->text = $request->text;
  34.         $post->save();
  35.  
  36.         return redirect('/post');
  37.     }
  38.    
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment