Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * 1 - Make all functions public - add [public] keyword before each one
- * 2 - use camelCase name syntax [allData] not [AllData]
- * 3 - leave some spaces between the variable name and the value
- * 4 - you can use the Validator facade to catch the errors or validate your comming request before doing the logic
- * 5 - you cant return dd() [ dd() for the development things ] instead return response()->json(array $data);
- * 6 - use request advantage to detrmine if that request is a POST, GET, or PUT request not in the Controller
- * such as this Route::put('/url', 'SomeController@editUser');
- * Route::post('/url', 'SomeController@updateUser');
- *
- */
- namespace App\Http\Controllers;
- use App\Post;
- use Illuminate\Http\Request;
- class PostController extends Controller
- {
- public function __construct()
- {
- // You don't really need this here
- // You can add it to the route - routes/web.php
- $this->middleware('auth')->except(['AllData','show']);
- }
- public function allData(){
- $posts = Post::whereId(1)->paginate(5);
- $postsCount = Post::count();
- return view('pages.Home', compact('posts', 'postsCount'));
- }
- public function show($id)
- {
- $validator = Validator::make(['id' => $id], [
- 'id' => 'required'
- ]);
- if ($validator->fails()){
- return back()->withErrors($validator->errors());
- }
- $post = Post::findOrFail($id);
- return view('pages.showpost', compact('post'));
- }
- public function create(){
- return view('pages.create');
- }
- public function store(Request $req){
- $req->validate([
- 'title' => 'required|max:255',
- 'body' => 'required|max:500',
- 'up' => 'image|mimes:jpeg,png,jpg,gif'
- ]);
- if ($req->hasFile('up')){
- $file = $req->file('up');
- $exteneion=$file->getClientOriginalExtension();
- $dbfile='image_'.time().date('Y_m_d').".".$exteneion;
- $file->storeAs('public/uploads',$dbfile);
- } else {
- $dbfile = 'noimg.jpg';
- }
- $post = new Post();
- $post->title = $req->title;
- $post->body = $req->body;
- $post->publisher_name = auth()->user()->name;
- $post->image = $dbfile;
- $post->save();
- return redirect('/posts')->with('status','Post inserted');
- }
- public function edit(Request $req, $id)
- {
- $validator = Validator::make(['id' => $id], [
- 'id' => 'required'
- ]);
- if ($validator->fails()){
- return back()->withErrors($validator->errors());
- }
- if ($req->isMethod('put')){
- $req->validate([
- 'title'=>'required|max:255',
- 'body'=>'required|max:500'
- ]);
- $post = Post::findOfFail($id);
- if (auth()->user()->name == $post->publisher_name){
- $post->title=$req->title;
- $post->body=$req->body;
- $post->save();
- return redirect('posts')->with('status','post Updated');
- } else{
- return redirect('/posts')->with('error','You can not edit these post');
- }
- } else {
- $post = Post::findOrFail($id);
- if (auth()->user()->name == $post->publisher_name){
- return view('pages.editpost',['post'=>$post]);
- }
- else{
- return redirect('/posts')->with('error','You can not edit these post');
- }
- }
- }
- public function delete($id){
- $validator = Validator::make(['id' => $id], [
- 'id' => 'required'
- ]);
- if ($validator->fails()){
- return back()->withErrors($validator->errors());
- }
- $post = Post::findOrFail($id);
- if (auth()->user()->name == $post->publisher_name){
- if ((!empty($post->image) || $post->image != null) && $post->image != "noimg.jpg" ){
- unlink('storage/uploads/'.$post->image);
- }
- $post->delete();
- return redirect('/posts')->with('status','Post Deleted');
- }
- else{
- return redirect('/posts')->with('error','You can not delete these post');
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement