Advertisement
Rudi_S

PostController (Mailto)

Jun 2nd, 2023
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.14 KB | Source Code | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use App\Mail\BlogPosted;
  6. use App\Models\Post;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\Auth;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Facades\Storage;
  11.  
  12. class PostController extends Controller
  13. {
  14.     /**
  15.      * Display a listing of the resource.
  16.      */
  17.    
  18.    
  19.     public function index()
  20.     {
  21.         if(!Auth::check()) {
  22.             return redirect('login');
  23.         }
  24.  
  25.         $posts = Post::active()->get();
  26.         $view_data = [
  27.             'posts' => $posts,
  28.         ];
  29.        
  30.         return view('posts.index', $view_data);
  31.     }
  32.            
  33.        
  34.  
  35.  
  36.     /**
  37.      * Show the form for creating a new resource.
  38.      */
  39.     public function create()
  40.     {
  41.         if(!Auth::check()) {
  42.             return redirect('login');
  43.         }
  44.  
  45.         return view('posts.create');
  46.     }
  47.  
  48.     /**
  49.      * Store a newly created resource in storage.
  50.      */
  51.     public function store(Request $request)
  52.     {
  53.         if(!Auth::check()) {
  54.             return redirect('login');
  55.         }
  56.  
  57.         $title = $request->input('title');
  58.         $content = $request->input('content');
  59.  
  60.        Post::create([
  61.         'title' => $title,
  62.         'content' => $content,
  63.        ]);
  64.  
  65.        \Mail::to('admin@codepolitan.com')->send(new BlogPosted());
  66.  
  67.        return redirect('posts');
  68.     }
  69.  
  70.     /**
  71.      * Display the specified resource.
  72.      */
  73.     public function show($id)
  74.     {
  75.         if(!Auth::check()) {
  76.             return redirect('login');
  77.         }
  78.  
  79.         $post = Post::where('id', $id)->first();
  80.         $comments = $post->comments()->limit(2)->get();
  81.         $total_comments = $post->total_comments();
  82.  
  83.         $view_data = [
  84.             'post' => $post,
  85.             'comments' => $comments,
  86.             'total_comments' => $total_comments,
  87.         ];
  88.         return view('posts.show', $view_data);
  89.     }
  90.  
  91.     /**
  92.      * Show the form for editing the specified resource.
  93.      */
  94.     public function edit(string $id)
  95.     {
  96.         if(!Auth::check()) {
  97.             return redirect('login');
  98.         }
  99.  
  100.         $post = Post::where('id', $id)->first();
  101.  
  102.         $view_data = [
  103.             'post' => $post
  104.         ];
  105.        
  106.         return view('posts.edit', $view_data);
  107.     }
  108.  
  109.     /**
  110.      * Update the specified resource in storage.
  111.      */
  112.     public function update(Request $request, string $id)
  113.     {
  114.         if(!Auth::check()) {
  115.             return redirect('login');
  116.         }
  117.  
  118.         $title = $request->input('title');
  119.         $content = $request->input('content');
  120.  
  121.         Post::where('id', $id)
  122.             ->update([
  123.                 'title' => $title,
  124.                 'content' => $content,
  125.                 'updated_at' => date('Y-m-d H:i:s'),
  126.             ]);
  127.  
  128.         return redirect("posts/{$id}");
  129.     }
  130.  
  131.     /**
  132.      * Remove the specified resource from storage.
  133.      */
  134.     public function destroy(string $id)
  135.     {
  136.         if(!Auth::check()) {
  137.             return redirect('login');
  138.         }
  139.  
  140.         Post::where('id', $id)->delete();
  141.  
  142.         return redirect('posts');
  143.     }
  144. }
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement