Laravel Relaton Return null - php

I have two table
one is products another is offers
products
---------
id | offer_id
offers
---------
id | product_id
Now I want to get all offers against a product
In my product model I wrote
public function getOfferDetails()
{
return $this->belongsTo('App\Offer');
}
but it return null.

You want to get all offers that belong to a product, right? Have you tried this:
public function getOfferDetails()
{
return $this->hasMany('App\Offer');
}
In your Product model?

Need to define how your relationships are. Based on what you have
A Product can have 0 - many offers and an Offer belongs to 1 Product.
You will need some foreign key to match the models. OTB Laravel will attempt to use the method name appended with _id as the foreign key. Since your method name is different, you need to pass the foreign key as the second parameter in your relationship method.
Products Model should have
public function getOfferDetails()
{
return $this->hasMany('App\Offer', 'product_id');
}
Offer Model should have
public function getProduct()
{
return $this->belongsTo('App\Product', 'product_id');
}
Laravel Documentation that might help out as well.

This is what you're looking for I think:
public function getOfferDetails()
{
return $this->hasMany('App\Offer', 'id', 'offer_id');
// as ceejayoz mentioned in comments, Laravel should be able to detect this themselves. The below would work the exact same as above
// return $this->hasMany('App\Offer', 'id', 'offer_id')
}
$product->getOfferDetails()->get();

Related

In Laravel Eloquent how to define relationship through secondary table? (Always returning 0 relations)

I feel like this should work. I have a list of products and categories (types).
Tables:
Products
- id
- name
- etc
Types
- id
- name
- etc
ProductTypes
- product_id
- type_id
Now, I feel like in the Type model in Laravel, I should be able to define this relationship:
public function products()
{
return $this->hasManyThrough(Product::class, ProductType::class, 'type_id', 'id');
}
I've tried other variations with the secondary ids in the additional parameters but no luck, always an empty list. Is ProductTypes a pivot table and therefore should be dealt with differently?
Edit: What's weird is that for the final 2 parameters ($localKey = null, $secondLocalKey = null) even if I enter complete garbage no error is thrown but these 2 parameters $firstKey = null, $secondKey = null have to be correct).
You are using the wrong relationship. Based on your database structure, a product can belong to many type. Therefore, it should be a BelongsToMany instead of a HasManyThrough.
You can achieve what you want with the following method, by passing the table name of your ProductTypes as the second parameter:
public function products()
{
return $this->belongsToMany(Product::class, 'product_types');
}
If your ProductType model extends Illuminate\Database\Eloquent\Relations\Pivot, you can do:
public function products()
{
return $this->belongsToMany(Product::class, 'product_types')
->using(ProductType::class);
}
For more information about Many to Many relationships: https://laravel.com/docs/6.x/eloquent-relationships#many-to-many

How to create relationship between 3 models in laravel?

SQL scheme:
bulletins
id increment
deals
id increment
seller_id
buyer_id
deals_items - items = bulletins
id increment
title
desc
bulletin_id
deal_id
How can I get deal row by bulletin id? In raw SQL it looks like:
select `deals`.* from `deals` inner join `deals_items` on `deals_items`.`deal_id` = `deals`.`id` where `deals_items`.`bulletin_id` = 10572
I tried:
public function deals()
{
return $this->hasManyThrough(DealItem::class,Deal::class, 'bulletin_id','dealid','id');
}
But it seems a wrong way. Can't find right way in laravel doc about relation.
#HCK shows right way.
but when I doing $bulletin->deals() in blade template I got empty collection of deals.
When just $bulletin->deal - all is fine, we have collection of deals.
I using protected $with = ['deals'] in my bulletin model, but what is different call method or property? Why with method empty result?
#Amarnasan was close, but the order of the foreign keys was wrong. Try this:
Deal.php
public function bulletins()
{
return $this
->belongsToMany(Bulletin::class, 'deals_items', 'deal_id', 'bulletin_id')
->withPivot('title','desc');
}
Bulletin.php
public function deals()
{
return $this
->belongsToMany(Deal::class, 'deals_items', 'bulletin_id', 'deal_id')
->withPivot('title','desc');
}
From the docs:
As mentioned previously, to determine the table name of the
relationship's joining table, Eloquent will join the two related model
names in alphabetical order. However, you are free to override this
convention. You may do so by passing a second argument to the
belongsToMany method:
return $this->belongsToMany('App\Role', 'role_user');
In addition to customizing the name of the joining table, you may also
customize the column names of the keys on the table by passing
additional arguments to the belongsToMany method. The third argument
is the foreign key name of the model on which you are defining the
relationship, while the fourth argument is the foreign key name of the
model that you are joining to:
return $this->belongsToMany('App\Role', 'role_user', 'user_id', 'role_id');
Update
When you access the relationship as a method: $bulletin->deals() you are accessing the relationship itself. This will return an instance of \Illuminate\Database\Eloquent\Relations\BelongsToMany (in your case). Here the query is not executed yet, so you could keep adding constrains to your query, for example:
$bulletin
->deals()
->where('seller_id', 45) // <---
->skip(5) // <---
-> ... (And so on)
When you access it as a dynamic property, you are already executing the query, so this will return a Collection instance. Is the same as calling the relationship as a method and then attach the ->get() at the end, so this two are equivalent:
$bulletin->deals()->get()
// equals to:
$bulletin->deals
Check this other answer, it answers your question.
DealClass:
public function bulletins()
return $this->belongsToMany('App\Bulletin', 'deals_items ', 'bulletin_id', 'deal_id')->withPivot('title','desc');
}
BulletinClass:
public function deals()
return $this->belongsToMany('App\Deal', 'deals_items ', 'deal_id', 'bulletin_id')->withPivot('title','desc');
}
deals model -
public function bulletins()
return $this->belongsToMany(Bulletin::class, 'deals_items ', 'bulletin_id', 'deal_id');
}
bulletin model:-
public function deals()
{
return $this
->belongsToMany(Deal::class, 'deals_items', 'deal_id', 'bulletin_id',);
}

Laravel - Making a HasManyThrough Relationship provide a unique collection

I am having an issue getting a hasManyThrough to work:
public function deliveryContainers() : HasManyThrough
{
return $this->hasManyThrough(
DeliveryContainer::class, // Final
StockMovement::class, // Intermediate
'product_id', // Foreign key on Intermediate
'id', // Foreign key on Final
'product_id', // Local key on Current
'location_id' // Local key on Intermediate
)->where('delivery_id', $this->delivery_id);
}
Because the stockMovements table returns multiple results, my resulting delivery containers collection contains duplicate entries. If I could somehow put a group/unique on the intermediate table query then this would be resolved.
I can get a collection with the correct deliveryContainers eager loaded using the following:
public function deliveryContainers()
{
return $this->hasMany(StockMovement::class, 'entity_id', 'delivery_id')
->with('deliveryContainer')
->where('product_id', $this->product_id)
->get()
->unique('location_id');
}
However, to access the deliveryContainer I now have the following:
foreach($this->deliveryContainers() as $row){
$row->deliveryContainer->id;
}
And what I would like to have...
foreach($this->deliveryContainers() as $row){
$row->id;
}
Is there any way to push the eager loaded relationship up a level (if that can be used to describe it), or even better add some kind of unique filter to the hasManyThrough relationship?
Table Structure
delivery_exceptions (where this relationship originates)
product_id
delivery_id
delivery_containers
id
delivery_id
stock_movements
entity_id (linked to delivery id)
product_id
Relationships
A delivery exception belongsTo a product
A product hasMany stock_movements
A stock movement belongsTo a delivery container
A delivery exception hasMany delivery containers... (indirectly, through a combination of the product and the stock movements)
You've got a really tough setup there and I'm not entirely sure that I got the full idea behind it (also because of you using entity_id at some place instead of delivery_id). But nonetheless, I gave it a shot.
The hasManyThrough relationship you defined looks actually not too bad, but in my opinion there is a better way to get to the result. But first let's have a look at your relationships:
3
+-------------------------------------+
4 v |
+-------------> Delivery <----------+ |
| | 1 |
+ + +
DeliveryException +---> Product <---+ StockMovement +---> DeliveryContainer
+ ^
+---------------------------------------------------------+
2
As a StockMovement already belongs to a DeliveryContainer, which in return belongs to a Delivery, the relation from StockMovement to Delivery (marked as 1) seems obsolete to me. Anyway, to get relation 2 on your model, you can use the paths 3 and 4 to your advantage:
class DeliveryException
{
public function deliveryContainers(): HasMany
{
return $this->hasMany(DeliveryContainer::class, 'delivery_id', 'delivery_id');
}
}
Obviously, this will give you all the DeliveryContainers, unfiltered by the Product. Therefore I suggest adding a second function:
public function deliveryContainersByProduct(): HasMany
{
return $this->deliveryContainers()
->whereHas('stockMovements', function ($query) {
$query->where('product_id', $this->product_id);
});
}
The accepted answer is far more elegant, but this is another way to do this too:
public function deliveryContainers1()
{
return $this->hasManyThrough(
DeliveryContainer::class, // Final
StockMovement::class, // Intermediate
'product_id', // Foreign key on Intermediate
'id', // Foreign key on Final
'product_id', // Local key on Current
'location_id' // Local key on Intermediate
)
->where('delivery_id', $this->delivery_id)
->distinct();
}

Laravel model relationships Class not found

In Laravel I just started with models and I have this database structure:
users
id | username | password | group | //(group is the id of the group)
groups
id | groupname |
group_pages
group_id | page_id | create | read | update | delete
pages
id | pagename
I am trying to check if the user can create/read/update/delete on the page he's on.
So I have 4 Models for this at the moment: Users, Pages,Group_pages and Groups. So in the models, I define the relationships like so:
User model:
public function group()
{
return $this->belongsTo('group', 'group', 'id');
}
Group Model:
public function users()
{
return $this->hasMany('users', 'group', 'id');
}
public function group_pages()
{
return $this->hasMany('group_pages', 'group_id', 'id');
}
I am using this in my controller like this:
$group_id = User::find(Session::get('user_id'));
$crud = Group::find($group_id->group)->group_pages()->first();
As described in the documentation.
but this is giving me the error:
Class group_pages not found
What is going wrong here?
I'm not sure about assigning the keys in the relationships.
I know this:
One to One Inverse:
return $this->belongsTo('class', 'local_key', 'parent_key');
One to Many:
return $this->hasMany('class', 'foreign_key', 'local_key');
I dont know about the One to Many Inverse. I know it's: return $this->belongsTo('table');, but I dont know about the keys.
Group_pages model:
class Group_pages extends Eloquent {
public function pages()
{
return $this->belongsTo('pages', 'id', 'group_id');
}
public function group()
{
return $this->belongsTo('group', 'id', 'group_id');
}
}
Model files should be named singularly and in camel-case, i.e. User, Page, Group. A model representing the join between users and groups isn’t necessary.
Then when it comes to defining the relationships, the first parameter is the class name of the model:
class User {
public function group()
{
return $this->belongsTo('Group', 'local_key', 'parent_key');
}
}
You’re making life difficult for yourself by going against Laravel’s conventions.
If you name your columns as per Laravel’s conventions, you then don’t need to specify them in your relationship definitions either. So your users table should have a column named group_id that’s a foreign key referencing the id column in your groups table. Your relationship can then be expressed like this:
class User {
public function group()
{
return $this->belongsTo('Group');
}
}
A lot more succinct and easier to read, and you don’t have to remember which way around the local and foreign column names go.
You can read more about the conventions Laravel uses for model and relation names in the official documentation: http://laravel.com/docs/master/eloquent#relationships
You defined your relationship with a model-class that does not exists.
To solve this, create a group_page-model (or even better GroupPage) and change the corresponding relationship (return $this->hasMany('GroupPage', 'group_id', 'id'); within your Group-model.
Then fix the relationship in your User-model:
public function group() // typo! not groep..
{
return $this->belongsTo('group', 'group'); // remove id, you do not need it
}
Then there is a problem with your controller code which might be fixable like that:
$group_id = User::find(Session::get('user_id'))->group()->id;
$crud = Group::find($group_id)->group_pages()->first();
I always like to recommend Laracasts to peopel who are new to Laravel (i hope you do not know this yet). The basic screencasts are all free (laravel 4 from scratch and laravel 5 fundamendals) and you will lern very fast in no time! Specifically, have a look at the episode on Eloquent Relationsships.
I also strongly recommend sticking to conventions
use the column-name group_id on the users-table for the group-foreign-key).
Classnames should be PascalCase -> Group, not group, and when commiting them as parametes, stick to it (belongsTo('Group'))...
This makes life much easier!
Finally
Be aware that there might be packages for what you are trying to achieve. One that comes to my mind is Entrust.
You're making your life hard with this code and thus you can't make it work.
Check this out first:
// This is User model, NOT group_id
$group_id = User::find(Session::get('user_id'));
Next:
public function group() // I suppose groep was typo, right?
{
// having relation with same name as the column
// makes Eloquent unable to use the relation in fact
return $this->belongsTo('group', 'group', 'id');
}
So, here's what you need:
// User model
public function group()
{
return $this->belongsTo('Group', 'group_id'); // rename column to group_id
}
// Group model
public function pages()
{
return $this->belongsToMany('Page', 'group_pages')
->withPivot(['create', 'read', 'update', 'delete']);
}
Then:
$user = User::find(Session::get('user_id')); // or use Auth for this
$page = $user->group->pages()->find($currentPageId);
// now you can access pivot fields:
$page->pivot->create;
$page->pivot->update;
... and so on

Laravel 4 Eloquent/Model Relationships

I am setting up several Models an want to know the correct approach to table structure and Model relationships.
Let's assume we have a shop containing products, each with properties size and color.
Table products
id
size_id
color_id
price
Table sizes
id
name
Table colors
id
name
Models
class Product extends Eloquent {
public function size() {
return $this->hasOne('Size', 'id');
}
public function color() {
return $this->hasOne('Color', 'id');
}
}
class Size extends Eloquent {
public function products() {
return $this->belongsTo('Product', 'size_id');
}
}
class Color extends Eloquent {
public function products() {
return $this->belongsTo('Product', 'color_id');
}
}
This way I can easily echo the color/size of a product using {{ Product->size['name'] }}. Also, I want to pass Eloquent the size's foreign key size.id like Product::where('size_id', '5') rather than its name size.name.
Problem: Doing $products = Product::has('size', '=', '5')->get() does not give me any results, yet doing $products = Product::where('size_id', '5')->get() does.
I am pretty confused, what went wrong?
I think that the problem is that your ::has() method is looking for products with exactly 5 different sizes on each specific product, which would assume that you would be using $this->hasMany('Size') in your Product model. Where as the ::where() method is returning results where the size of the product is 5.
In the documentation they use an example of comments. A post will have a list of comments. You can find posts that have at least one comment (ie. Post::has('comments')->get()) or you can find posts that have more than 3 comments (ie. Post::has('comments', '>=', '3')->get()).
http://laravel.com/docs/eloquent#querying-relations

Categories