how to join 3 table wherehas eloquent using laravel - php

for example I have 3 tables and that tables has a relation,
Table A
id
name
Table B
id
tableId_A
tableId_C
Table C
id
name
i'm using table C but i want to search items by request name that have the same name as in table A using query eloquent
and this is the query, i'm using a laravel
$this->model->query()
->withWhereHas('tableB', function($query) use ($names) {
$query->tableB->where('name', 'LIKE', "%{$names}%");})
how to fix it using eloquent where has used 3 tables relation?

I think your relation name should be tableA not tableB.
public function tableA()
{
return $this->belongsToMany(TableAModel::class, 'TableB', 'tableId_C', 'tableId_A')
->where('TableA.status', CoreStatusEnum::ACTIVE);
}
And your query goes like this..
$this->model->query()
->withWhereHas('tableA', function($query) use ($names) {
$query->where('name', 'LIKE', "%{$names}%");
})
->get();

Related

Laravel - Check all instances of relationship for values on pivot table

I have a two models that have a many-to-many relationship, with some addtional fields on the pivot table:
Shift table:
------------
id - uuid
date - date
address - string
...
Employee table:
---------------
id - uuid
first_name - string
...
shift_employee table:
---------------------
id - integer
shift_id - uuid
employee_id - uuid
hours_worked - integer
...
Now, I'm making a Lens in Laravel Nova, and I want to use the query object to check if any of the instances on shift_employee related to a specific shift has a value bigger than 0 for hours_worked on the shift_employee table.
My first idea is to somehow use whereHas assuming that the Shift model has a relationship employees, like this:
$query->whereHas('employees' function ($q) {
$q->where('hours_worked', '>', 0);
});
But... this is not working... There are shifts with more than 0 hours_worked for certain employees and this query string is not working for me. How would I do this?
First make sure your models are modeled correctly. If they are, you can access any attribute of an intermediate table with the pivot attribute as below:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
/**
* The users that belong to the role.
*/
public function users()
{
return $this->belongsToMany('App\User');
}
}
exemple :
$user = App\User::find(1);
foreach ($user->roles as $role) {
echo $role->pivot->created_at;
}
In your case, try :
$employee = Employee::with('Shift');
foreach($employee as $e){
$employeeHw[] = $e->shift_employee->where('hours_worked', '>', 0)->get();
}
I'm also new to laverel, so I'm not absolutely sure it works, but in theory: P
Usually in these cases I use the query bilder with join which I find easier
$users = DB::table('users')
->join('contacts', 'users.id', '=', 'contacts.user_id')
->join('orders', 'users.id', '=', 'orders.user_id')
->select('users.*', 'contacts.phone', 'orders.price')
->get();

laravel model relation for nested models

I have three tables in a laravel project, namely:
-center
-payments
-payment_details
center table:
payment table:
payment_details table:
now i want to select all data from payment_details table,i got it it displayed well, but there is a search option for payment_type for the list,But the search filed is not in payment-details table , so i don't know how to retrieve that fro m another table
If you are modeling your data with Eloquent relationships Do somthing like:
$type = 'c'; // For example
PaymentDetail::whereHas('payment', function ($query) use ($type) {
$query->where('type', $type);
})->get();
If not you had to do join statement with payments table

Laravel Eloquent and Mysql join a table IF another join is null

Three main tables:
products
advertisers
locations
Two pivot tables:
advertisers_locations
products_locations
Relationships:
A product belongs to an advertiser and an advertiser has many locations (Locations it can ship products to)
A product can also have it own set of locations that override the advertiser locations (Some products have delivery restrictions)
What I need to do is:
Select all products
Check if products_locations table for product ID and join it.
If it does not exist then join the advertisers locations table
Is this possible to do in one query and using eloquent? Here's my code - struggling with the conditional:
public function scopeWhereShippableToLocation($query)
{
$location_id = session('location_id');
$query->where(function ($q) use ($location_id) {
$q->join('products_locations', 'products_locations.product_id', '=', 'products.id')
->where('products_locations.location_id', '=', $location_id);
});
$query->orWhere(function ($q) use ($location_id) {
$q->join('advertisers_locations', 'advertisers_locations.advertiser_id', '=', 'products.advertiser_id')
->where('advertisers_locations.location_id', '=', $location_id);
});
//dd($q->toSql());
return $query;
}
This is currently producing a MySQL error:
Column not found: 1054 Unknown column 'products_locations.location_id' in 'where clause' (SQL: select `products`.*,
I think I have a solution for you using eloquent, rather than the query builder. You need to check to see if the relationship exists, if not you need another query. This can be done using the following:
public function scopeWhereShippableToLocation($query)
{
$location_id = session('location_id');
// WhereHas check to see if a relationship exists, IE: The pivot table
// orWhereHas will be checked if the first where does not exist
$query->whereHas('products_locations', function ($q) use ($location_id) {
$q->where('location_id', $location_id);
})->orWhereHas('advertisers_locations', function ($q) use ($location_id) {
$q->where('location_id', $location_id);
});
return $query;
}
This should work providing that your Products, Advertisers and Locations relationship methods are set up.

get table data base on pivot table in laravel

I have M:N relationship between tables handymen and categories. So, pivot table is category_handyman. How to fetch all handymen data, who have category_id=1 in pivot table? I wanted to do something like this: (but this doesnt work)
$handymen = Handyman::with('categories')
->where('category_id', 1)
->get();
You can use whereHas() method to filter on related records:
$handymen = Handyman::whereHas('categories', function($query) {
$query->whereId(1);
})->get();

How to get a collection of models using where and relationships?

I have an Order table with some relationships (Status, User) using their IDs to create it (status_id, user_id)
I need to get a collection of Order models using and Eloquent ORM but I need to filter by their status name (which is in Status table only) and user name (which is in User table only)
When I use 'join' in my query builder. The relationships aren't hydrating, like in this case:
$emprestimo = DB::table('emprestimos')
->join('status', 'status_id', '=', 'status.id')
->where('status.nome', 'Solicitado')
->where('emprestimos.dono_id', Auth::user()->id)
->get();
How can I filter using joins and also hydrate my collection of models?
you can try something like this
see if that works
$emprestimo = DB::table('emprestimos')
->join('status', function ($join) {
$join->on('emprestimos.status_id', '=', 'status.id')
->where('status.nome', 'Solicitado')
})
->where('emprestimos.dono_id', Auth::user()->id)
->get();

Categories