Skip to content

PostController

Tady edited this page Sep 6, 2023 · 1 revision
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;

class PostController extends Controller
{

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    // public function __construct()
    // {
    //     $this->middleware('auth', ['except' => ['index', 'show']]);
    // }


    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $posts = Post::all();

        $data = [
            'posts' => $posts
        ];

        return view('posts.index')->with($data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('posts.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // $this->validate($request, [
        //     'col_1' => 'required',
        //     'col_2' => 'required',
        //         .
        //         .
        //         .
        //     'col_n' => 'required',
        // ]);

        
       $id = $request->input('id'); 
       $title = $request->input('title'); 
       $content = $request->input('content'); 
       $created_at = $request->input('created_at'); 
       $updated_at = $request->input('updated_at'); 


        $post = new Post;

        
       $post->title = $title; 
       $post->content = $content; 
       $post->created_at = $created_at; 
       $post->updated_at = $updated_at; 


        if($post->save()){
            return redirect("/posts")->with('success', 'Data saved successfully');
        }

         return redirect()->back()->with('error', 'Error in the process of saving the data');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $post = Post::find($id);
        
        if(!$post){
            return redirect()->back()->with('error', 'post not found');
        }
        $data = [
            'post' => $post
        ];

        return view('posts.show')->with($data);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $post = Post::find($id);

        if(!$post){
            return redirect()->back()->with('error', 'post not found');
        }
        
        $data = [
            'post' => $post
        ];

        return view('posts.edit')->with($data);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        // $this->validate($request, [
        //     'col_1' => 'required',
        //     'col_2' => 'required',
        //         .
        //         .
        //         .
        //     'col_n' => 'required',
        // ]);

        $post = Post::find($id);

        if(!$post){
            return redirect()->back()->with('error', 'post not found');
        }

        
       $id = $request->input('id'); 
       $title = $request->input('title'); 
       $content = $request->input('content'); 
       $created_at = $request->input('created_at'); 
       $updated_at = $request->input('updated_at'); 


        
       $post->title = $title; 
       $post->content = $content; 
       $post->created_at = $created_at; 
       $post->updated_at = $updated_at; 


        if($post->save()){
            return redirect()->back()->with('success', 'Data updated successfully');
        }

        return redirect()->back()->with('error', 'Error during DB update');



    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {

        $post = Post::find($id);

        if(!$post){
            return redirect()->back()->with('error', 'post not found');
        }

        // It is recommended to check authorization before performing a deletion

        if($post->delete()){
            return redirect()->back()->with('success', 'Data deleted successfully');
        }

        return redirect()->back()->with('error', 'Error during deleting data from DB!');
    }
}
Clone this wiki locally