Advertisement
Guest User

Untitled

a guest
Feb 16th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. /**
  4.  * 1 - Make all functions public - add [public] keyword before each one
  5.  * 2 - use camelCase name syntax [allData] not [AllData]
  6.  * 3 - leave some spaces between the variable name and the value
  7.  * 4 - you can use the Validator facade to catch the errors or validate your comming request before doing the logic
  8.  * 5 - you cant return dd() [ dd() for the development things ] instead return response()->json(array $data);
  9.  * 6 - use request advantage to detrmine if that request is a POST, GET, or PUT request not in the Controller
  10.  *     such as this Route::put('/url', 'SomeController@editUser');
  11.  *                  Route::post('/url', 'SomeController@updateUser');
  12.  *
  13.  */
  14.  
  15. namespace App\Http\Controllers;
  16.  
  17. use App\Post;
  18. use Illuminate\Http\Request;
  19.  
  20. class PostController extends Controller
  21. {
  22.     public function __construct()
  23.     {
  24.         // You don't really need this here
  25.         // You can add it to the route - routes/web.php
  26.         $this->middleware('auth')->except(['AllData','show']);
  27.     }
  28.  
  29.     public function allData(){
  30.         $posts      = Post::whereId(1)->paginate(5);
  31.         $postsCount = Post::count();
  32.  
  33.         return view('pages.Home', compact('posts', 'postsCount'));
  34.     }
  35.  
  36.     public function show($id)
  37.     {
  38.         $validator = Validator::make(['id' => $id], [
  39.             'id' => 'required'
  40.         ]);
  41.  
  42.         if ($validator->fails()){
  43.             return back()->withErrors($validator->errors());
  44.         }
  45.  
  46.        
  47.             $post = Post::findOrFail($id);
  48.  
  49.             return view('pages.showpost', compact('post'));
  50.     }
  51.  
  52.     public function create(){
  53.  
  54.         return view('pages.create');
  55.  
  56.     }
  57.  
  58.     public function store(Request $req){
  59.  
  60.         $req->validate([
  61.             'title' => 'required|max:255',
  62.             'body' => 'required|max:500',
  63.             'up' => 'image|mimes:jpeg,png,jpg,gif'
  64.         ]);
  65.  
  66.         if ($req->hasFile('up')){
  67.             $file = $req->file('up');
  68.             $exteneion=$file->getClientOriginalExtension();
  69.             $dbfile='image_'.time().date('Y_m_d').".".$exteneion;
  70.             $file->storeAs('public/uploads',$dbfile);
  71.         } else {
  72.             $dbfile = 'noimg.jpg';
  73.         }
  74.  
  75.         $post = new Post();
  76.         $post->title = $req->title;
  77.         $post->body = $req->body;
  78.         $post->publisher_name = auth()->user()->name;
  79.         $post->image = $dbfile;
  80.         $post->save();
  81.  
  82.         return redirect('/posts')->with('status','Post inserted');
  83.     }
  84.  
  85.     public function edit(Request $req, $id)
  86.     {
  87.         $validator = Validator::make(['id' => $id], [
  88.             'id' => 'required'
  89.         ]);
  90.  
  91.         if ($validator->fails()){
  92.             return back()->withErrors($validator->errors());
  93.         }
  94.  
  95.         if ($req->isMethod('put')){
  96.             $req->validate([
  97.                 'title'=>'required|max:255',
  98.                 'body'=>'required|max:500'
  99.             ]);
  100.             $post = Post::findOfFail($id);
  101.  
  102.             if (auth()->user()->name == $post->publisher_name){
  103.                 $post->title=$req->title;
  104.                 $post->body=$req->body;
  105.                 $post->save();
  106.                 return redirect('posts')->with('status','post Updated');
  107.             } else{
  108.                 return redirect('/posts')->with('error','You can not edit these post');
  109.             }
  110.  
  111.         } else {
  112.  
  113.             $post = Post::findOrFail($id);
  114.  
  115.             if (auth()->user()->name == $post->publisher_name){
  116.                 return view('pages.editpost',['post'=>$post]);
  117.             }
  118.             else{
  119.                 return redirect('/posts')->with('error','You can not edit these post');
  120.             }
  121.            
  122.  
  123.         }
  124.     }
  125.  
  126.     public function delete($id){
  127.         $validator = Validator::make(['id' => $id], [
  128.             'id' => 'required'
  129.         ]);
  130.  
  131.         if ($validator->fails()){
  132.             return back()->withErrors($validator->errors());
  133.         }
  134.  
  135.         $post = Post::findOrFail($id);
  136.  
  137.         if (auth()->user()->name == $post->publisher_name){
  138.             if ((!empty($post->image) || $post->image != null) && $post->image != "noimg.jpg" ){
  139.                 unlink('storage/uploads/'.$post->image);
  140.             }
  141.             $post->delete();
  142.             return redirect('/posts')->with('status','Post Deleted');
  143.         }
  144.         else{
  145.             return redirect('/posts')->with('error','You can not delete these post');
  146.         }
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement