php artisan make:model --resource在控制器中提供模型参数功能。如何使用它们?

时间:2019-03-10 05:32:52

标签: php laravel

  

php artisan make:model --resource -m

给了我控制器的功能。我不明白如何使用它们。 这是控制器在运行上述命令后的功能。

    <?php

    namespace App\Http\Controllers;

    use App\test1;
    use Illuminate\Http\Request;

    class Test1Controller 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  \App\test1  $test1
     * @return \Illuminate\Http\Response
     */
    public function show(test1 $test1)
    {
        //
    }

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

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

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

如何使用这些?

但我知道

  

php artisan:make控制器--resource

将为控制器中的函数提供$ id参数,我知道如何使用它们。 这就是控制器的功能。

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class test2 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)
    {
        //
    }
}

1 个答案:

答案 0 :(得分:0)

Laravel中有一个称为“路由模型”绑定的东西,在这种情况下,您的路由应如下所示:

/test1/{test1} //instead of /test1/{id}

您可以不使用findwhere来访问对象:

public function show(test1 $test1)
{
    // The $test1 is accessible and you can get any property you want like $test1->id;
}

您可以阅读更多here