Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Alewa's avatar
Level 2

Laravel 10 api, with method

I want to add the with method in laravel to an api code, but can't find the right way api.php file

Route::get('/user', function (Request $request) {
    return $request->user();
})->middleware('auth:sanctum');

Route::namespace('App\Http\Controllers\ApiV1')->group(function(){
    Route::post('register','AuthController@register');
    Route::post('login','AuthController@login');
    Route::post('update_profile','AuthController@updateProfile')->middleware('auth:sanctum');
    Route::post('update_password','AuthController@updatePassword')->middleware('auth:sanctum');
    Route::post('logout','AuthController@logout')->middleware('auth:sanctum');

    Route::apiResource('countries', CountryController::class);

    Route::apiResource('product_categories', ProductCategoryController::class)->middleware('auth:sanctum');
    Route::get('all_product_categories','ProductCategoryController@allCategories');

    Route::apiResource('products', ProductController::class)->middleware('auth:sanctum');
});```

Product.php file

public function admin_created(){ return $this->belongsTo('App\Models\User', 'admin_created_id'); }

public function admin_updated(){
   return $this->belongsTo('App\Models\User', 'admin_updated_id');
}

public function product_category()
{
    return $this->belongsTo('App\Models\ProductCategory', 'product_category_id');
}

public function product_attributes()
{
    return $this->hasMany('App\Models\ProductAttribute');
}

public function product_images()
{
    return $this->hasMany('App\Models\ProductImage');
}```

Product.php file

public function show(Product $product)
    {
        return response()->json([
            "product"=>$product
        ]);
    }```
Want to add the with method to this update function so i can get my product data with other joint database, like admin_created and product_category_id any help?
0 likes
6 replies
Tray2's avatar

You can add a protected property on you product model

protected $with = ['<your model>'];
Alewa's avatar
Level 2

The api is working perfectly but I want to add with method to the ProductController.php show or detail function as written above eg. $products = Product::with(['product_category'])->latest()->get(); return response()->json([ "products"=>$products ]); But want to use the same format I have use in the update function in ProductController to display the data and use the with method

puklipo's avatar

It's all written in the documentation.
https://laravel.com/docs/11.x/eloquent-relationships#eager-loading

Later loading into Eloquent instance

$product->load('product_category');

return response()->json([
            "product" => $product
        ]);

Always eager loading

class Product extends Model
{
    /**
     * The relationships that should always be loaded.
     *
     * @var array
     */
    protected $with = ['product_category'];
Alewa's avatar
Level 2

Thanks to both Tray2 and puklipo, both answers are true.

Please or to participate in this conversation.