Laravel 8 API RESTful with many to many relationship - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Sunday, June 6, 2021

Laravel 8 API RESTful with many to many relationship

I want to set many to many relation between category and product and make product category api.

my product model:

class Product extends Model
{
use HasFactory;
protected $fillable=['name','desc','price'];

public function categories(){
    return $this->belongsToMany(Category::class, 'product_categories');
}
}

my category model:

class Category extends Model
{
use HasFactory;
protected $fillable=['name','desc'];

public function products(){
    return $this->belongsToMany(Product::class , 'product_categories');
}
}

my Product controller:

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

    return response(Product::with(['categories'=>function($q){
        $q->select('name');
}])->paginate(10), 200);
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(ProductStoreRequest $request)
{
    $product = Product::create($request->validated());
    $product->save();

    $category=$request->get('categories');


    $product->categories()->sync($category);

    return response([
        'data' => $product,
        'message' => 'product created'
    ],201);
}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function show($id)
{
    try {
        $product = Product::findOrFail($id);
        return response([
            'data' => $product,
            'message' => 'Product Found'
        ],200);
    }
    catch(ModelNotFoundException $exception) {
        return response(['message' => 'Product Not Found!'],404);
    }
}

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

    $product=Product::find($id);
    $product->update($request->validated());

    $category=$request->get('categories');
    $product->categories()->sync($category);

    return response([
        'data' => $product,
        'message' => 'product updated'
    ],200);
}

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function destroy($id)
{
    $product=Product::find($id);
    $product->categories()->detach();
    $product->delete();
    return response([
        "message" =>"product deleted"
    ],200);
}
 }

*Many to many relationships are working properly.I wanted to add my product category to list ,store ,update ,delete but I couldn't. How can I edit it? *



source https://stackoverflow.com/questions/67853030/laravel-8-api-restful-with-many-to-many-relationship

No comments:

Post a Comment