get table data base on pivot table in laravel - php

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();

Related

how to join 3 table wherehas eloquent using laravel

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();

Laravel join two tables for ruled out records

In Laravel, how can I reject a record in a query based on a value with one of the table with which this table has relations?
For example, I have the Products table and the Categories table. The Categories table has a one-to-many relationship, one category can have many products. The categories table are is_visible is_deleted. How to make inquiries on the Products table so that it rejects products that belong to the category that has and set fields is_visible = false or is_deleted = true ?
I tried something like this:
$products = ProductTable::join('product_category_tables', 'product_category_tables.id', '=', 'product_tables.id')
->where('product_category_tables.is_visible', '=', true)
->where('product_category_tables.is_deleted', '=', false)
->where('product_tables.is_visible', '=', true)
->where('product_tables.is_deleted', '=', false)
->paginate(50);
But from this query I have only one record. I can't make this query on Category table becouse I want get only 25/50/100 products for paginate.
If you are using Laravel Modal(Eloquent) and have defined the one to many relationship properly in both Product and Category Model. then you can achieve this by the following query:
$products = Product::where("is_visible", true)
->where("is_deleted", false)
->whereHas('category', function($categories){
$categories->where("is_visible", true)
->where("is_deleted", false);
})
->get();
In Product model you should have defined the relationship as bellow:
public function category()
{
//From your mentioned query I am seeing that
//product table's id is actually categories table's id(which should not be like this though),
//so the relationship is
return $this->belongsTo(Category::class, 'id');
}

Laravel 5 filter on pivot

I have two tables Candidates & Qualifications with a joining pivot table called Results.
What I want to do is select all candidates for this centre and filter them by a value in the pivot table.
So select all candidates from centre 1 where the pivot “status” = “REGISTERED”
I have tried numerous approaches and am currently with the one below but nothing is working.
$candidates = Candidate::where('batch_centre_id','=', $id)->with(array('qualification' => function($q)
{
$q->wherePivot('status','=', 'REGISTERED');
}))->get();
Any advice would be much appreciated
In Eloquent wherePivot method can only be loaded on a relation. The way you're doing it, you're calling it on qualifications relation, therefore you're fetching all candidates and then, for each of them, you're loading qualifications that have status=REGISTERED. That's why you're getting all candidates.
You could work around it by using Eloquent's whereHas() method that let's you filter the base model you're trying to fetch by attributes of some relation. In your case the following should work:
$candidates = Candidate::where('batch_centre_id','=', $id)
->whereHas('qualifications', function($q) {
$q->wherePivot('status','=', 'REGISTERED');
})->with('qualifications')->get();
This way you'll get only candidates that have a qualification with status=REGISTERED and all their qualifications. If you want to additionally filter loaded qualifications by status, add the constraint like you did in your code:
$candidates = Candidate::where('batch_centre_id','=', $id)
->whereHas('qualifications', function($q) {
$q->wherePivot('status','=', 'REGISTERED');
})->with(array('qualifications', function($q) {
$q->wherePivot('status','=', 'REGISTERED');
})->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();

fetch data from multiple tabels

i have some table,one of them is user table that i want fetch id from this table by searching username and second one is prize_info table that i want to fetch user_id from here by searching a prize,after all i want fetch just prize_info.user_id and user.id.what is the best way for this search part in laravel4?? here is my codes
$search = '%'.Input::get('keywords').'%';
$pages = DB::table('user')
->select('user.id','user.username')
->where('username', 'LIKE', $search)->get();
$blogitems = DB::table('prize_info')
->select('prize_info.id', 'prize_info.prize_name', 'prize_info.user_id')
->where('prize_name', 'LIKE', $search)->get();
what is your idea about above codes?????
and i check the result by this line but it does not work!
$results = $pages->union($blogitems)->take(30)->get();
please help me!best regard
I would advise you used Laravel's Eloquent ORM Relationships to do this. You can easily fetch related record using the available relationships;
One-to-One
One-to-Many
BelongsTo
BelongsToMany
HasMany
HasOne
Polymorphic Tables (using pivot tables)
You can use this:
$search = '%'.Input::get('keywords').'%'
$results = DB::table('user')
->select('user.id','user.username','prize_info.id', 'prize_info.prize_name', 'prize_info.user_id')
->leftJoin('prize_info','user.id','prize_info.user_id')
->take(30)
->get();

Categories