• How To Add A Resource Controller In Laravel In cmd



    Type the following command.

    php artisan make:controller MyController --resource

    (Here myController is the controller name).

    The resource controller will create  the default functions index,create,store,show,edit,update & destroy() as follows.

    <?php

    namespace App\Http\Controllers;

    use Illuminate\Http\Request;

    class MyController extends Controller
    {
        /**
         * Display a listing of the resource.
         *
         * @return \Illuminate\Http\Response
         */
        public function index()
        {
            //
        }

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

        /**
         * Store a newly created resource in storage.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return \Illuminate\Http\Response
         */
        public function store(Request $request)
        {
            //
        }

        /**
         * Display the specified resource.
         *
         * @param  int  $id
         * @return \Illuminate\Http\Response
         */
        public function show($id)
        {
            //
        }

        /**
         * Show the form for editing the specified resource.
         *
         * @param  int  $id
         * @return \Illuminate\Http\Response
         */
        public function edit($id)
        {
            //
        }

        /**
         * Update the specified resource in storage.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  int  $id
         * @return \Illuminate\Http\Response
         */
        public function update(Request $request, $id)
        {
            //
        }

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



    Details are as follows:
    • namespace App\Http\Controllers;  - defines the namespace for controller
    • use Illuminate\Http\Request;  - import namespaces with the required classes to run the controller


    • class MyController extends Controller : defines the class for the controller. MyController controller extends the base controller.
    • public function index() : the default function for the controller
    • public function create() :the function that can be used to create new resources such as new record in a table.
    • public function store(Request $request) : the function used to store a newly created resource
    • public function show($id) : the function that retrieves a single record  to view based on the get id 
    • public function edit($id) :the function used to retrieved a single record based on the get id for edit
    • public function update(Request $request, $id) : the function used to update a record based on the getid
    • public function destroy($id) : the function used to remove a record from table based on getid.
  • You might also like

    No comments:

    Post a Comment