Laravel hasManyThrough interrogate the intermediate relationship as well as distant - php

I want to perform the following (with some rather crude pseudo code):
SELECT a users orderLines WHERE the orderheader's status ='paid' AND the orderLine's productId>5
In other words, a user can place many orders. Each order has one or many order lines. I want to find all of the order lines that the user has placed (order lines, not orders) but only if the order header has a certain status, and only if the order line has another parameter checked. This could be the date the line was added, or the productId being x, and so on.
Simple enough to do with a standard MySql query.
I have the nescessary models:
User
OrderHeader (Intermediate relationship)
OrderLine (Distant relationship - this is what I want to fetch, via the intermediate)
Here are how the relationships are defined in each model:
User
public function orders()
{
return $this->hasMany('App\OrderHeader', 'user_id', 'id');
}
public function lines()
{
return $this->hasManyThrough('\App\OrderLine', 'App\OrderHeader', 'user_id', 'order_header_id');
}
OrderHeader
public function lines()
{
return $this->hasMany('App\OrderLine', 'order_header_id', 'id');
}
public function user(){
return $this->belongsTo('User', 'id', 'user_id');
}
OrderLine (Fetch these for the User, using hasManyThrough)
public function header()
{
return $this->belongsTo('App\OrderHeader', 'order_header_id');
}
public function products()
{
return $this->belongsToMany('App\Product');
}
So, I load the User, using:
$person = User::findOrFail($id)
Then I can use:
$user->lines()->where('product_id','>=',10)->paginate(20);
So, that works brilliantly to get ALL of the lines that the user has placed, which match the condition on the line records. However, I can't figure out how to add a second condition on the intermediate, so that not only do I check the product_id, but also interrogate the OrderHeader entity via the orders() relationship.
I've tried:
return $user->orders()->where('status','=','Paid')->lines()->where('product_id','>=',20))->paginate(20);
but that returns the error: Call to undefined method Illuminate\Database\Query\Builder::lines()

hasManyThrough is a special case in Eloquent, where table is joined (intermediate table), so it's pretty simple - just query that table. Nothing to do with the other relation.
This is what you want:
$throughTable = $user->lines()->getParent()->getTable();
$user->lines()
->where('product_id', '>=', 10)
->where('orderheaders.status', 'paid')
// or
// where("{$throughTable}.status', 'paid')
->paginate(20);
Btw this relation is wrong:
// OrderHeader model
public function user(){
return $this->belongsTo('User', 'id', 'user_id'); // wrong keys order
}
// should be
public function user(){
return $this->belongsTo('User', 'user_id', 'id');
}

Related

Using Eloquent and pivot with models, which have various relationships

I want to retrieve data using Eloquent, however I have several relationships between my models. I have four database tables (with the first three also having models):
- users (user_id, username, email ...)
- account (account_id, ...)
- entry (entry_id, account_id, ...)
- account_users (user_id, account_id)
In entry, account_id is a foreign id and the relationship has been implemented in both models.
account_users is an intermediate table for account and users. I have created a many-to-many relationship between account and user by adding the belongsTo-method in each respective model.
In Entry.php:
public function account(){
return $this->belongsTo('App\Account', 'account_id');
}
public function category(){
return $this->belongsTo('App\Category', 'category_id');
}
In Account.php:
public function entry(){
return $this->hasOne('App\Entry', 'account_id');
}
public function users(){
return $this->belongsToMany('App\User', 'account_user', 'account_id', 'user_id');
}
In User.php:
public function accounts(){
return $this->belongsToMany('App\Account', 'account_user', 'user_id', 'account_id');
}
In EntryController I have the method index(), where I am trying to show just the entries belonging to the currently logged in user, however I'm not sure how to structure the select query. I've read in the Laravel documentation about using pivot when structuring a query, however I'm still unsure.
Assuming a variable $user exists, you may do this to retrieve entries:
$user->load('accounts.entry');
$entries = $user->accounts
->flatMap(function ($account) {
return $account->entry;
})
->unique('entry_id');
It will get entries from every account and merge them into a 1-dimension array, and finally remove the duplicates.

Laravel pivot consultation

I need advice about my model relationships,
Logic
Group has many users
Group has many admins
User has many groups (as user)
User has many groups (as admin)
Database Structure
Group Table
User Table
Group_Users table (save id of user and id of group)
Group_Admins table (save id of user and id of group)
Relationship Code
User model
public function groupsUser() {
return $this->hasMany(GroupUser::class);
}
public function groupsAdmin() {
return $this->hasMany(GroupAdmin::class);
}
Group model
public function users() {
return $this->hasMany(GroupUser::class);
}
public function admins() {
return $this->hasMany(GroupAdmin::class);
}
GroupUser model
public function group() {
return $this->belongsTo(Group::class);
}
public function user() {
return $this->belongsTo(User::class);
}
GroupAdmin model
public function group() {
return $this->belongsTo(Group::class);
}
public function user() {
return $this->belongsTo(User::class);
}
Help wanted
Basically as the relationships between users and groups is many to many normally I shouldn't need models of GroupUser and GroupAdmin and then just using sync() function in order to add/remove users and group id's from those tables.
What is my concerns then?
Normally I use that type of connection when I want input bulk ids into database (let say adding tags to posts, suddenly relate 10 tags id to 1 post) that moment using sync() and removing GroupUser and GroupAdmin models makes sense but in my case as users joins/adds to groups one by one, what do you suggest for this relationships?
Is my current approach makes sense?
Is is better if I remove those GroupUser and GroupAdmin models and add them to user, group model like:
public function users()
{
return $this->hasMany(User::class, 'group_users', 'user_id', 'id');
}
and such so?
What do you think is the best practice?
users and groups is many to many
your tables like this?
users, user_group , groups ?
How about use 'belongsToMany' relation?
Laravel many to many relation
// User model
public function groups() {
return $this->belognsToMany(Group::class);
}
// Group model
public function users() {
return $this->belognsToMany(User::class);
}
And use like.
User::find(1)->groups; // return user 1 groups
User::find(1)->groups()->sync($groupIds); // relate user and groups
Group::find(1)->users; // return group 1 users
If you have role column in your users table, you cold add relation like.
// Group model
public function users() {
return $this->belognsToMany(User::class)->where('role', 'the role of normal user');
}
public function admins() {
return $this->belognsToMany(User::class)->where('role', 'the role of admin user');
}
Hope it helps you.

Laravel nested eager load specific columns

I am working with nested eager loading is there a way you can pick out certain columns from the middle relation in account.user.location ?
User Model
public function account(): HasMany
{
return $this->hasMany(Account::class);
}
public function location(): BelongsTo
{
return $this->belongsTo(Location::class);
}
Account model
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
Location model
public function user(): HasMany
{
return $this->hasMany(User::class);
}
Controller method that works
This method returns the nested relation but i want certain columns from the user relation instead of listing them all.
public function show(string $id)
{
$film = Film::with([
'account.user.location'
])->findOrFail($id);
}
Controller method that doesn't work
This is my sample code i've tried to pick out the name column from users then display the location relation.
public function show(string $id)
{
$film = Film::with([
'account.user:id,name',
'account.user.location:id',city
])->findOrFail($id);
}
Response
This is the response which is returned its returning the location as null from the not working method
+"account": {#2061
+"id": "191067a6-4c38-423d-a972-bb3a842ca89e"
+"user": {#2064
+"id": "d9f381c1-3899-367c-8d60-6d2bc3db6d23"
+"name": "Domenick"
+"location": null
Im unsure on how i pick out specific columns from the middle relation and then joining the location. Can i get some assistance on where i am going wrong?
Laravel is loading each level of relationships after another. In other words, if you use A::with('b.c')->get(), then Eloquent will first load all As, then all of their referenced Bs and finally all of the Cs referenced by the loaded Bs. The ORM uses navigation properties, i.e. foreign keys, to do so. If you omit these foreign keys on intermediate models, the framework is not able to load the referenced models anymore.
If you'd do it manually, you would use the following queries (used IDs and foreign keys are examples):
SELECT * FROM a; // returns As with ids 1, 2, 3
SELECT * FROM b WHERE a_id IN (1, 2, 3); // returns Bs with ids 4, 5, 6
SELECT * FROM c WHERE b_id IN (4, 5, 6);
In your case, it should be sufficient to use the following code:
public function show(string $id)
{
$film = Film::with([
'account.user:id,account_id,location_id,name',
'account.user.location:id,city'
])->findOrFail($id);
}
Update your User Model
public function account()
{
return $this->hasMany(Account::class, 'user_id');
}
public function location()
{
return $this->belongsTo(Location::class);
}
Update you Account class to
public function user()
{
return $this->belongsTo(User::class, 'user_id');
}
In your controller method try this
public function show($id)
{
$film = Film::where('id', $id)
->with([
'account.user:id,name',
'account.user.location:id',city
])->get();
}

Laravel: 2nd level relationship only fetching single row

I am using Laravel 5.6 and I have relation between 3 tables. Cart->cartItem->Images
Here is my controller code:
$cart = Cart::where('created_by_id', Auth::user()->id)->with('cartDetails')->first();
Here is my cart model:
public function CartItem()
{
return $this->hasMany('App\Http\models\CartItem', 'cart_id')->with('images');
}
Here is the model of cartItem:
public function images()
{
return $this->belongsTo('App\Http\models\ProductImage', 'item_id', 'product_id');
}
Now in result I am getting only single image even though I have multiple images in the database. It always picking up the last inserted image.
I want to get all images or at least the first one but not the last one.
Please help.
You should use hasMany() relation instead of belongsTo():
public function images()
{
return $this->hasMany('App\Http\models\ProductImage', 'item_id', 'product_id');
}
if you have multiple images in the database of items then you have to use hasMany() insted of belongsTo().
public function images()
{
return $this->hasMany('App\Http\models\ProductImage', 'item_id', 'product_id');
}
When use belongTo() ?
suppose you have post and comment model. Now you want post of comment . That is inverse of a hasMany relationship.To define the inverse of a hasMany relationship, define a relationship function on the Comment (child) model which calls the belongsTo method
public function post()
{
return $this->belongsTo('App\Post');
}

Laravel belongsTo returning null when using 'with'

I'm just getting started with Laravel so please forgive any noobness.
I have a User and Order model, a user has many orders:
# Inside User model
public function orders()
{
$this->hasMany('Order');
}
# Inside Order
public function user()
{
return $this->belongsTo('User');
}
// Not sure if this is upsetting anything (also in Order)
public function products()
{
return $this->belongsToMany('Product');
}
So I think I have the above right.
But when I do this:
$users = User::with('orders')->find(1);
return $users;
I get Call to a member function addEagerConstraints() on null.
However, if I do it the other way around, it works great:
$orders = Order::with('User')->get();
return $orders;
What am I doing wrong / what don't I understand?! Or is my problem bigger than I think?
Database:
The problem is you don't have return for your orders relationship. It should be:
public function orders(){
return $this->hasMany('Order');
}
You should also use your relationships case sensitive. you showed:
$orders = Order::with('User')->get();
is working, but you should rather use
$orders = Order::with('user')->get();
to avoid extra queries to your database in future
For anyone else that runs across this, I was having the same issue, but my problem was that I had the foreign/local keys swapped. Example:
// This is correct for hasX relationships
public function user() {
return $this->hasOne('App\Models\User', 'user_id', 'local_key_user_id');
}
// This is correct for belongsTo relationships
public function user() {
return $this->belongsTo('App\Models\User', 'local_key_user_id', 'user_id');
}
Notice that for hasX relationships, the foreign key is the second parameter, and the local key is the third. However, for belongsTo relationships, these two are swapped.
Probably doesn't answer this particular question but it relates to the title. I had the same issue here is the wrong query
$offer = Offer::with([
'images:name,id,offer_id',
'offer_options:offer_option,value,id,offer_id',
'user:id,name,avatar'])
->select(['id', 'views', 'type', 'status'])
->where('id', $id)->get();
the model look like this
class Offer extends Model {
function user(): BelongsTo {
return $this->belongsTo(User::class);
}
}
The User
class User extends ..... {
function offer(): HasMany {
return $this->hasMany(Offer::class);
}
}
The issue with the query is I was not selecting user_id, i.e in my select function user_id column was not included and that is why I was getting null for user
according to Laravel docs
When using this feature, you should always include the id column and
any relevant foreign key columns in the list of columns you wish to
retrieve.
So the correct query is
$offer = Offer::with([
'images:name,id,offer_id',
'offer_options:offer_option,value,id,offer_id',
'user:id,name,avatar'])
->select(['id', 'views', 'type', 'status','user_id'])
->where('id', $id)->get();

Categories