Laravel query relationship where clause - php

I need filter by departamento, like where('departamento_id', '=', 1).
Tables:
membro_bancas
id
departamento_id
user_id
trabalhos
id
titulo
orientador_id (references id of membro_bancas)
coorientador_id (references id of membro_bancas)
I have this code:
$trabalho = Trabalho::with('membrobanca.departamento')
->with('coorientador')
->with('academico')
->get();
And returns this:

I got it!
I needed add orWhereHas for coorientador too. The result is this:
Trabalho::whereHas('membrobanca', function($q) use ($departamento_id) {
$q->where('departamento_id', '=', $departamento_id);
})
->orWhereHas('coorientador', function($q) use ($departamento_id) {
$q->where('departamento_id', '=', $departamento_id);
})
->with('academico')
->get();

Try this eloquent filter using whereas
$trabalho = Trabalho::whereHas('membrobanca',function($query) use ($id){
$query->where('departamento_id', '=', $id)
})
->with('coorientador')
->with('academico')
->get();
example of filter using with
$trabalho = Trabalho::with(['membrobanca',function($query) use ($id){
$query->where('departamento_id', '=', $id)
}])
->with('coorientador')
->with('academico')
->get();

Related

How to use Eloquent model property in subquery?

I have two tables (users, posts) and I'm trying to order users according to recent post's date.
But unfortunately I couldn't find a way to use User model's firebase_id to use in subquery. I don't want to use raw query or DB directly if it's possible to use Eloquent for such query.
How can I use User model's firebase_id as subquery parameter?
Here is my non-working code to give an idea about what I try to do:
User::select('users.*', DB::raw("SELECT MAX(id) FROM posts WHERE posts.firebase_id = users.firebase_id as recent_post_date"))
->whereHas('posts', function ($q) {
$q->withCount('comments');
})
->with(['posts' => function ($q) {
$q->withCount('comments')
->orderBy('created_at', 'ASC');
}])
->orderBy('recent_post_date', 'DESC')
->get();
You can use selectSub to add a subquery.
Try the following
$recentPostQuery = Post::select('created_at')
->whereColumn('firebase_id', 'users.firebase_id')
->latest()
->limit(1)
->getQuery();
User::select('users.*')
->selectSub($recentPostQuery, 'recent_post_date')
->whereHas('posts', function ($q) {
$q->withCount('comments');
})
->with(['posts' => function ($q) {
$q->withCount('comments')
->orderBy('created_at', 'ASC');
}])
->orderBy('recent_post_date', 'DESC')
->get();
You can do with QueryBuilder and a join:
$users = DB::table('users')
->join('posts.firebase_id ', '=', 'users.firebase_id ')
->orderBy('posts.recent_post_date', 'DESC')
->get();
UPDATE
You asked for Eloquent. You can do it with whereHas.
$users = User::whereHas('posts', function (Builder $query) {
$query->orderBy('posts.recent_post_date', 'DESC');
})->get();

How to get Complex relation in a Laravel using eloquent

I have below tables
Users
id
name
Companies
id
user_id
name
Product Categories
id
company_id
name
Products
id
product_category_id
name
Sales
id
product_id
price
quantity
total
Now i want to get all sales for specified user's products. I have tried with below query. But i cannot use where for this. It is not apply this filter. Also i want to filter it by few other fields like users.user_name, products.product_name ... etc
$sales = Sale::with(['product'=>function($q) use ($user_id) {
$q->with(['product_category' => function($q) use ($user_id) {
$q->with(['company'=> function($q) use ($user_id) {
$sUserName = Input::get('sUserName');
$q->with(['user'=> function($q) use ($sUserName,$user_id) {
$q->where('id', $user_id);
}]);
}]);
}]);
}]);
Is it possible to use Eloquent for these situations ? Or should i use join queries ? Please advice me. thanks
you can use laravel eloqunt like this.
$Sales = DB::table('Sales')
->join('Products', 'Sales.product_id', '=', 'Products.id')
->join('ProductCategories', 'ProductCategories.id', '=', 'Products.product_category_id')
->join('companies', 'ProductCategories.company_id', '=', 'companies.id')
->join('Users', 'companies.user_id', '=', 'Users.id')
->select('Sales.*')
->get();
After madalinivascu advise i could be able to resolve my problem as below. Now i can filter it by Ordered User, Product name & dates.
$sUserName = Input::get('sUserName');
$sProductName = Input::get('sProductName');
$sFrom = Input::get('sFrom');
$sTo = Input::get('sTo');
$user_id = \Auth::User()->id;
// Show sales only for his products
$sales = Sale::with('sold_to_user')
->whereHas('sold_to_user', function($q) use ($sUserName) {
if(!empty($sUserName)) $q->where('name','LIKE', "$sUserName%");
})->with('product')
->whereHas('product', function($q) use ($sProductName) {
if(!empty($sProductName)) $q->where('name','LIKE', "$sProductName%");
});
if(\Auth::User()->privilege!='administrator') $sales = $sales->where('user_id','=',$user_id);
$sales = $sales->orderBy('updated_at','desc')->paginate(100);

Eloquent join table with two conditions

I have two tables:
A: id | user_id | phonenumber | status | ...
B: id | user_id | phonenumber | name | ...
I would like to list all elements in table A but I have to left join the elements to table B.
I can do it with DB:
DB::table('A')
->leftJoin('B', function ($join) {
$join->on('A.phonenumber', '=', 'B.phonenumber')
->on('A.user_id', '=', 'B.user_id');
})
->select(...)
->where('A.user_id', '=', $userId)
->get();
Is it possible to solve it with eloquent somehow?
You can use eloquent, one way is to change it to
A::leftJoin('B', function ($join) {
$join->on('A.phonenumber', '=', 'B.phonenumber')
->on('A.user_id', '=', 'B.user_id');
})
->select(...)
->where('A.user_id', '=', $userId)
->get();
And with using pure eloquent
You would have to do adjustment to your A model. We would define two model functions.
public function phoneNumber()
{
return $this->hasMany(B::class, "phonenumber", "phonenumber");
}
public function userId()
{
return $this->hasMany(B::class, "user_id", "user_id");
}
Now we will change our query to
A::with("phonenumber")->has("phonenumber")
->with("userId")->has("userId")
->where("user_id","=",$userId)
->get();
This should work for you.
** Updated ***
if you want all the record in A then simply do
A::with("phonenumber","userId")
->where("user_id","=",$userId)
->get();
modelname::where(condition)->leftjoin(tablename,fieldname,othertable.fieldname)->get()
This is format how we write join in laravel
$postdata = post::select('categories.id', 'posts.id', 'categories.categoryname', 'posts.title', 'posts.body', 'posts.image', 'posts.created_at', 'users.name')
->where('post_type', 'publish')
->where('status', 'active')
->where('categories.id', $id)
->leftJoin('users', 'users.id', '=', 'posts.user_id')
->leftJoin('categories', 'categories.id', '=', 'posts.category')
->orderBy('created_at', 'desc')
->paginate(5);
This query display blog with username and with category using join it's my proect convenient query you can also create for you in this manner

Laravel Eloquent query get users from another model

I have another table called tableb and it has a user relationship defined through the user_id field.
I want to run a query against tableb where a certain date is within a certain range but then I want to grab the user table associated with that row but I only want it to grab the user if it's not been grabbed yet. I'm trying to do this all in 1 DB query. I have most of it done, but I'm having trouble with the unique part of it.
Here's what I have right now:
$tableB = TableB::select('users.*')
->join('users', 'tableb.user_id', '=', 'users.id')
->where('tableb.start_date', '>', date('Y-m-d'))
->get();
So right now I have 3 entries in tableB from the same user, and ideally I'd like to only get 1 entry for that user.
How would I go about doing this?
Since you're selecting only users data, just add a groupBy clause in your query.
$tableB = TableB::select('users.*')
->join('users', 'tableb.user_id', '=', 'users.id')
->where('tableb.start_date', '>', date('Y-m-d'))
->groupBy('users.id')
->get();
You should just add groupBy like this :
$tableB = TableB::select('users.*')
->join('users', 'tableb.user_id', '=', 'users.id')
->where('tableb.start_date', '>', date('Y-m-d'))
->groupBy('users.id')
->get
Try This Code
App/user.php
public function getrelation(){
return $this->hasMany('App\tableB', 'user_id');
}
In Your Controller
Controller.php
use App/user;
public funtion filterByDate(user $user)
{
$date = '2016-02-01';
$result = $user->WhereHas('getrelation', function ($query) use($date) {
$query->whereDate('tableb.start_date', '>', $date)
->first();
});
}

Laravel Eloquent, return rows where both conditions are True

I have a table of Weeks that is joined to a property table, Weeks table looking like this:-
PropID, WeekDate, Available
1 , 2015-07-04, Yes
1 , 2015-07-11, Yes
1 , 2015-07-18, No
2 , 2015-07-04, Yes
2 , 2015-07-11, No
2 , 2015-07-18, No
I want to select properties where both the weeks of the 4th and 11th are available. In the example above, I want to return two rows with PropID 1 as both are available and no rows from PropID 2 as only one of the weeks are available.
I've tried various ways, but either get nothing or always return the 1st, 2nd and 4th rows.
I think this is close, but it's still missing something as it is looking for dates that are <= AND >=
$query = Property::whereHas('Week', function($q) use ($arrive)
{
$q->where(function($sub)
{
$sub->where('WeekDate', '>=', '2015-07-04');
$sub->where('WeekDate', '<=', '2015-07-11');
});
$q->where('Available', '=', 'Yes');
})
->get();
Not sure this helps, but the Property table is simply
PropID, PropName
1 , Property 1
2 , Property 2
Just found that this SQL works.
SELECT PropID FROM tblweeks WHERE WeekDate IN ('2015-07-04', '2015-07-11') AND Available = 'yes' GROUP BY PropID HAVING COUNT(*) = 2
This will give your result as Property 1 only:
$weeks = Property::whereHas('Week', function ($q) {
$q->where(function ($sub) {
$sub->whereIn('WeekDate', array('2015-07-04', '2015-07-11'));
$sub->where('Available', '=', 'y');
});
$q->groupBy('property_id');
$q->having('count(*)', '=', '2');
})->get();
There are some changes i have did in your query :
Please check this solution
$query = Property::whereHas('Week', function($q) use ($arrive)
{
$q->where('WeekDate', '=', '2015-07-04');
$q->orWhere('WeekDate', '=', '2015-07-11');
$q->where('Available', '=', 'Yes');
})
->get();
This should work and will return desire output
There are some changes, the query might be using group by query :
Please check this solution
$query = Property::whereHas('Week', function($q) use ($arrive)
{
$q->where('WeekDate', '=', '2015-07-04');
$q->orWhere('WeekDate', '=', '2015-07-11');
$q->where('Available', '=', 'Yes');
$q->group('WeekDate');
})
->get();
This should work and will return desire output
You actually need two whereHas for this:
$query = Property::whereHas('Week', function($q) use ($arrive)
{
$q->where('WeekDate', '>=', '2015-07-04');
$q->where('Available', '=', 'Yes');
})
->whereHas('Week', function($q) use ($arrive)
{
$q->where('WeekDate', '<=', '2015-07-11');
$q->where('Available', '=', 'Yes');
})
->get();
I believe you do not need the second nested query.
$query = Property::whereHas('Week', function($q) use ($arrive)
{
$q->where('WeekDate', '>=', '2015-07-04');
$q->where('WeekDate', '<=', '2015-07-11');
$q->where('Available', '=', 'Yes');
})
->get();
Updated
Have you looked into whereBetween.
$query = Property::whereHas('Week', function($q) use ($arrive)
{
$q->whereBetween('WeekDate', '2015-07-04', '2015-07-11');
$q->where('Available', '=', 'Yes');
})
->get();

Categories