View difference between Paste ID: 8HAawh08 and xDvwP6QP
SHOW: | | - or go back to the newest paste.
1-
<?php
1+
<?php
2-
2+
3-
namespace App\Http\Controllers;
3+
/**
4-
4+
 * 1 - Make all functions public - add [public] keyword before each one
5-
use App\Post;
5+
 * 2 - use camelCase name syntax [allData] not [AllData] 
6-
use Illuminate\Http\Request;
6+
 * 3 - leave some spaces between the variable name and the value
7-
7+
 * 4 - you can use the Validator facade to catch the errors or validate your comming request before doing the logic
8-
class PostController extends Controller
8+
 * 5 - you cant return dd() [ dd() for the development things ] instead return response()->json(array $data);
9-
{
9+
 * 6 - use request advantage to detrmine if that request is a POST, GET, or PUT request not in the Controller
10-
    function __construct()
10+
 *     such as this Route::put('/url', 'SomeController@editUser');
11-
    {
11+
 *                  Route::post('/url', 'SomeController@updateUser');
12-
        $this->middleware('auth')->except(['AllData','show']);
12+
 * 
13-
    }
13+
 */
14-
14+
15-
    function AllData(){
15+
namespace App\Http\Controllers;
16-
        $AllPosts=Post::where('id','>',0)->paginate(5);
16+
17-
        $count=Post::count();
17+
use App\Post;
18-
        return view('pages.Home',['posts'=>$AllPosts,'count'=>$count]);
18+
use Illuminate\Http\Request;
19-
    }
19+
20-
    function show($id)
20+
class PostController extends Controller
21-
    {
21+
{
22-
        if (!empty($id)){
22+
    public function __construct()
23-
            $post=Post::where('id',$id)->first();
23+
    {
24-
            if (count($post)>0){
24+
        // You don't really need this here
25-
                return view('pages.showpost',['post'=>$post]);
25+
        // You can add it to the route - routes/web.php 
26-
            }else{
26+
        $this->middleware('auth')->except(['AllData','show']);
27-
                return dd("No post with this id");
27+
    }
28-
            }
28+
29-
29+
    public function allData(){
30-
        }else{
30+
        $posts      = Post::whereId(1)->paginate(5);
31-
            return dd("Can not be empty");
31+
        $postsCount = Post::count();
32-
        }
32+
33-
    }
33+
        return view('pages.Home', compact('posts', 'postsCount'));
34-
    function create(){
34+
    }
35-
        return view('pages.create');
35+
36-
    }
36+
    public function show($id)
37-
    function store(Request $req){
37+
    {
38-
        $req->validate([
38+
        $validator = Validator::make(['id' => $id], [
39-
            'title' => 'required|max:255',
39+
            'id' => 'required'
40-
            'body' => 'required|max:500',
40+
        ]);
41-
            'up' => 'image|mimes:jpeg,png,jpg,gif'
41+
42-
        ]);
42+
        if ($validator->fails()){
43-
        if ($req->hasFile('up')){
43+
            return back()->withErrors($validator->errors());
44-
            $file = $req->file('up');
44+
        }
45-
            $exteneion=$file->getClientOriginalExtension();
45+
46-
            $dbfile='image_'.time().date('Y_m_d').".".$exteneion;
46+
       
47-
            $file->storeAs('public/uploads',$dbfile);
47+
            $post = Post::findOrFail($id);
48-
        }else{
48+
49-
            $dbfile = 'noimg.jpg';
49+
            return view('pages.showpost', compact('post'));
50-
        }
50+
    }
51-
        $post=new Post();
51+
52-
        $post->title = $req->title;
52+
    public function create(){
53-
        $post->body = $req->body;
53+
54-
        $post->publisher_name = auth()->user()->name;
54+
        return view('pages.create');
55-
        $post->image = $dbfile;
55+
56-
        $post->save();
56+
    }
57-
        return redirect('/posts')->with('status','Post inserted');
57+
58-
    }
58+
    public function store(Request $req){
59-
    function edit(Request $req,$id)
59+
60-
    {
60+
        $req->validate([
61-
        if (!empty($id))
61+
            'title' => 'required|max:255',
62-
        {
62+
            'body' => 'required|max:500',
63-
            if ($req->isMethod('put')){
63+
            'up' => 'image|mimes:jpeg,png,jpg,gif'
64-
                $req->validate([
64+
        ]);
65-
                    'title'=>'required|max:255',
65+
66-
                    'body'=>'required|max:500'
66+
        if ($req->hasFile('up')){
67-
                ]);
67+
            $file = $req->file('up');
68-
                $post=Post::where('id',$id)->first();
68+
            $exteneion=$file->getClientOriginalExtension();
69-
                if (auth()->user()->name == $post->publisher_name){
69+
            $dbfile='image_'.time().date('Y_m_d').".".$exteneion;
70-
                    $post->title=$req->title;
70+
            $file->storeAs('public/uploads',$dbfile);
71-
                    $post->body=$req->body;
71+
        } else {
72-
                    $post->save();
72+
            $dbfile = 'noimg.jpg';
73-
                    return redirect('posts')->with('status','post Updated');
73+
        }
74-
                } else{
74+
75-
                    return redirect('/posts')->with('error','You can not edit these post');
75+
        $post = new Post();
76-
                }
76+
        $post->title = $req->title;
77-
77+
        $post->body = $req->body;
78-
            }else{
78+
        $post->publisher_name = auth()->user()->name;
79-
                $post=Post::where('id',$id)->first();
79+
        $post->image = $dbfile;
80-
                if (!count($post)>0){
80+
        $post->save();
81-
                    return dd("No Post With this id");
81+
82-
                }else{
82+
        return redirect('/posts')->with('status','Post inserted');
83-
                    if (auth()->user()->name == $post->publisher_name){
83+
    }
84-
                        return view('pages.editpost',['post'=>$post]);
84+
85-
                    }
85+
    public function edit(Request $req, $id)
86-
                    else{
86+
    {
87-
                        return redirect('/posts')->with('error','You can not edit these post');
87+
        $validator = Validator::make(['id' => $id], [
88-
                    }
88+
            'id' => 'required'
89-
                }
89+
        ]);
90-
90+
91-
            }
91+
        if ($validator->fails()){
92-
92+
            return back()->withErrors($validator->errors());
93-
93+
        }
94-
        }
94+
95-
    }
95+
        if ($req->isMethod('put')){
96-
    function delete($id){
96+
            $req->validate([
97-
        if (!empty($id)){
97+
                'title'=>'required|max:255',
98-
            $post=Post::where('id',$id)->first();
98+
                'body'=>'required|max:500'
99-
            if (count($post)>0){
99+
            ]);
100-
                if (auth()->user()->name == $post->publisher_name){
100+
            $post = Post::findOfFail($id);
101-
                    if ((!empty($post->image) || $post->image != null) && $post->image != "noimg.jpg" ){
101+
102-
                        unlink('storage/uploads/'.$post->image);
102+
            if (auth()->user()->name == $post->publisher_name){
103-
                    }
103+
                $post->title=$req->title;
104-
                    $post->delete();
104+
                $post->body=$req->body;
105-
                    return redirect('/posts')->with('status','Post Deleted');
105+
                $post->save();
106-
                }
106+
                return redirect('posts')->with('status','post Updated');
107-
                else{
107+
            } else{
108-
                    return redirect('/posts')->with('error','You can not delete these post');
108+
                return redirect('/posts')->with('error','You can not edit these post');
109-
                }
109+
            }
110-
            }else{
110+
111-
                return dd("No post With this id");
111+
        } else {
112-
            }
112+
113-
        }
113+
            $post = Post::findOrFail($id);
114-
    }
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
}