Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

hasMany Relation with where condition in Laravel

hasMany Relation with where condition in Laravel

In this article, we will discuss how to use hasMany Relation with where condition in Laravel. Laravel provides great features as a model relationship. but if you need to use the where clause on your relation model then how you can do it?

You can make the where condition using whereHas function. it doesn’t matter which relation you used like one to one, one to many, many to many, has many through, etc.

Need of where condition in Relation Data in Laravel

Some time might be you need to add the where condition with your relation model then you can simply use whereHas() as we provide below an example of hasMany Relation with where condition in Laravel with laravel 5, laravel 6, laravel 7, laravel 8, and laravel 9 apps.

We have two tables with some testing records, the first one is orders and the second one is orders_products, in our database.

orders Table:-

orders table

orders_products Table:-

orders_products table

where condition in a relationship in laravel eloquent

In the Product Controller, we will get only the return order with the column item status = ‘Return Approved’ in the orders_products table using the where condition in Relation data in Laravel.

get all the records using where condition in hasMany in laravel

app\Models\Order.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Order extends Model
{
    use SoftDeletes;
    use HasFactory;

    public function order_items(){
        return $this->hasMany('App\Models\OrdersProduct','order_id');
    }
}

Create a function for hasMany Relationship related to OrdersProduct Table with order_id to get all the related records.

app\Models\OrdersProduct.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class OrdersProduct extends Model
{
    use SoftDeletes;
    use HasFactory;
}

routes\web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProductController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/get-return-orders',[ProductController::class,'ReturnOrders']);

app\Http\Controllers\ProductController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Product;
use App\Models\Order;
use App\Models\OrderPoduct;

class ProductController extends Controller
{

public function ReturnOrders()
 {
  $returnOrderDetails = Order::with('order_items')->whereHas('order_items', function($q) {
     $q->where('item_status', 'Return Approved');
    })->get();
  dd($returnOrderDetails);
  }
}

Run application:-

http://127.0.0.1:8000/get-return-orders
how to use where condition in relation data in laravel
hasMany Relation with where condition in Laravel

Read also:- Laravel select a specific column with Relation.