doomm4711 2017-09-08 03:28
浏览 52
已采纳

如何使用Laravel 5.5或Laravel 5.4创建REST JSON API

Am just starting out with laravel and i wanted to create a simple rest api that accepts json data and returns a json data but didnt seem to know how to do this. I was wondering if anyone could help. below is what i want my controller to look like or do:

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

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // get user values as json
        // save value to database
       // return user data as json.
    }

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

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

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

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //delete value and also return deleted value as json
    }
}

How can i achieve the above using laravel. Mostly 5.5 or 5.4. I dont seem to find anything that could be of help anywhere.

  • 写回答

1条回答 默认 最新

  • douzi2778 2017-09-08 04:23
    关注

    This problem is not too hard; you can get the solution of such problem easily over the internet.

    Anyway, you can accomplish this by using array and json_decode like below

     $data = ['staus' => 'ok',
              'id' => 10,
              'name'=>'something',
              'address'=>'something'
              ];
    // encode this array to json strig you can use return or echo, print to generate the result
     return json_encode($data);
    

    If you want to convert the model objects to json then you can simply use toJson function like below which comes with laravel framework

       return ModelName::all()->toJson();
       // for a single record retrieval
       return ModelName::find(1)->toJson();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?