You can add a protected property on you product model
protected $with = ['<your model>'];
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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?
Please or to participate in this conversation.