This is the 30th day of my participation in the August Text Challenge.More challenges in August

I. Product details API

1.1 Detailed controller method

    /** * Product details */
    public function show(Good $good)
    {
        return $this->response->item($good.new GoodTransformer());
    }
Copy the code

Similarly, we have configured available associations in the commodity, which are also available in the details hereparamsPass the parameter to get.


1.2 Test Effect

You can see that when the item details are returned, it also carries the classification details and the data for which user created the item.


Second, modify the product API

2.1 Methods for Modifying commodity Controllers

    /** * update merchandise */
    public function update(GoodsRequest $request, Good $good)
    {
        // Only level 3 categories can be used, and categories cannot be disabled
        $category = Category::find($request -> category_id);
        if (!$category) return $this->response->errorBadRequest('Classification does not exist! ');
        if ($category->status == 0) return $this->response->errorBadRequest('Classification is disabled! ');
        if ($category->level ! =3) return $this->response->errorBadRequest('You can only add items to a level 3 category! ');

        $good->update($request->all());
        return $this->response->noContent();
    }
Copy the code

2.2 Test Effect

You can see that the product data has been updated successfully.


Three, whether to put API

3.1 Whether to mount the controller

    /** * whether to install */
    public function isOn(Good $good) {
        $good->is_on = $good ->is_on == 0? 1 : 0;
        $good->save();
        return $this->response->noContent();
    }
Copy the code


3.2 Test Effect

You can see if the shelving function can be modified properly.


4. Is API recommended

4.1 Is the API controller method recommended

    /** * whether to recommend */
    public function isRecommend(Good $good) {
        $good->is_recommend = $good ->is_recommend == 0? 1 : 0;
        $good->save();
        return $this->response->noContent();
    }
Copy the code


4.2 Test Effect

You can see if the recommended function can be modified normally.

If you find this article helpful on your way to learning PHP, please follow me to like and comment on it. Thank you, your blog is definitely another support for me to write.