If I have properties table, and 2 other tables:
*property_characteristics
- property_id (i.e. 1)
- characteristic_id (i.e. 5 - join with default_characteristics)
- value (i.e. 3 - aka 3 rooms)
*default_characteristics
- id (i.e. 5)
- name (i.e. rooms)
In the Property.php model I have:
public function characteristics()
{
return $this->belongsToMany('Proactiv\DefaultCharacteristic', 'property_characteristics', 'property_id', 'characteristic_id');
}
How can I get the number of rooms (value from property_characteristics) for a property starting from:
$property = Properties::find(1);
I would need something like this in view:
$property->characteristics->rooms // should return 3 which is the value columns on property_characteristics table
Since the value is on your pivot table, you need to tell Laravel about this extra field. Add to your belongsToMany line to make:
return $this->belongsToMany('Proactiv\DefaultCharacteristic', 'property_characteristics', 'property_id', 'characteristic_id')
->withPivot('value');
Then select characteristics with the name you want, rooms, and get the value:
echo $property->characteristics()->with('name', 'rooms')->first()->pivot->value;
Alternatively, add a getter to your Property model which does this for you (you'll still need to add that withPivot part to the relationship):
public function getRoomsAttribute()
{
return $this->characteristics()
->where('name', 'rooms')
->first()
->pivot
->value;
}
Then you can get the number of rooms in a similar way to how you originally wanted to, with $property->rooms.
Or you could generalize this to get any characteristic:
public function getCharacteristic($name)
{
return $this->characteristics()
->where('name', $name)
->first()
->pivot
->value;
}
And then get the number of rooms with $property->getCharacteristic('rooms').
First, you have to tell your relationship to make your additional field available. You do this using the withPivot() method:
public function characteristics() {
return $this->belongsToMany('Proactiv\DefaultCharacteristic', 'property_characteristics', 'property_id', 'characteristic_id')
->withPivot('value');
}
Now you can access your value on the pivot table. You do this like so:
$property = Properties::find(1);
foreach ($property->characteristics as $characteristic) {
echo $characteristic->pivot->value;
}
You can read more about this in the documentation here, under the Retrieving Intermediate Table Columns heading.
Related
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
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',);
}
I have 3 tables (items, category_questions_mapping and category_questions) in mysql database in which I have the following columns:
items table has following columns:
item_id, uuid, radius, category_id
category_questions_mapping has following columns:
category_id, category_question_id
category_questions has following columns:
category_question_id, data
I have created the model for the items table which is Items.php in which I have created the following method:
public function category_questions() {
return $this->hasOne('App\CategoryQuestionsMapping','category_id','category_id');
}
The above method is basically a relationship between category_id of the items table with the category_id of category_question_mappings table
Problem Statement:
I am wondering what changes I should make in the above method so that I am able to pull data column value from category_questions table. Is there any we can make a relationship in the above method so that it can pull the data column ?
The above method will be used in the controller for display.
To acess the category_question table columns you must define the relationship on the category_question_mapping model too, like:
public function category_questions() {
return $this->hasOne('App\CategoryQuestions','category_question_id','category_question_id');
}
then acess the field you want like $items->category_questions_mapping->category_questions->data.
By the way, I'd recommend naming the first relationship you showed like category_questions_mapping() as it is linking with the category_questions_mapping table.
You can't change only the category_questions method to solve the problem. But you can make 2 relations in 2 models and query data through 2 relations.
// App\Item
public function category_questions_mapping() {
return $this->hasOne(CategoryQuestionsMapping::class, 'category_id', 'category_id');
}
// App\CategoryQuestionsMapping
public function category_question() {
return $this->hasOne(CategoryQuestion::class, 'category_question_id', 'category_question_id');
}
// Somewhere else
// Get data:
$data = $item->category_questions_mapping->category_question->data;
// Find by data:
$items = \App\Item
::whereHas('category_questions_mapping.category_question', function ($query) {
$query->where('data', 'foo');
})
->get();
Your relationship use like this :
public function category_questions() {
return $this->belongsToMany('App\CategoryQuestions','category_questions_mapping','category_id', 'category_question_id);
}
I'm trying to retrieve data from many to many relationship.I have two tables :
companies: [cid,name,origin]
vehicle_types: [id, type]
their pivot table: companies_vehicle_types: companies_id,vehicle_types_id
Relationship defined:
In Companies:
public function vehicle_types(){
return $this->belongsToMany('App\vehicle_types');
}
In vehicle_types
public function companies(){
return $this->belongsToMany('App\companies')->withTimestamps();
}
I want to retrieve companies where vehicle_types = specific type. How can i do that?
I tried doing following in my controller:
$vehicle_types=vehicle_types::all()->whereLoose('type','Bike');
foreach ($vehicle_types->companies as $vehicle_types) {
$company[]=$vehicle_types->pivot->name;
}
return $company;
But it doesn't seem to be working. It throws error of Undefined property: Illuminate\Database\Eloquent\Collection::$companies
Override the second, third and fouth argument in the relation if you are not following laravel's convention. See the documentation Many to Many Relation
Second argument determines the table name of the relationship's
joining table. 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:
public function vehicle_types(){
return $this->belongsToMany('App\vehicle_types', 'companies_vehicle_types', 'companies_id', 'vehicle_types_id');
}
Also, One $vehicle_type can have many companies, so:
$vehicle_types=vehicle_types::all()->whereLoose('type','Bike');
foreach ($vehicle_types as $vehicle_type) {
foreach($vehicle_type->companies as $company)
{
$company[]=$company->pivot->name;
}
endforeach
}
Again, I don't see name field in pivot table for this line to work: $company[]=$company->pivot->name;
I have 3 tables : hotels, hotels_data, hotels_types
Table hotels have id, type, stars, etc... type field is set as foreign key referencing type_id in hotels_types. I'm managing to get the correct data from hotels_data but have an empty result on getting hotels_types title and I don't understand why.
The code is the following :
class Hotel extends Eloquent {
public function getList() {
$data = Hotel::select('id','stars')->with('HotelData', 'HotelType')->paginate(10);
return View::make('hotels.index')->with('hotels', $data);
}
public function HotelData()
{
return $this->hasOne('HotelsData')->select('id','hotel_id','title');
}
public function HotelType()
{
return $this->hasOne('HotelType','type_id', 'type')->select('id','type_id','title');
}
}
You're using the wrong relationship for HotelType()
Your Hotel model should use the inverse of hasOne, which is belongsTo, because it contains the foreign key to HotelType (when a table contains a foreign key, it always belongs to the table being pointed to).
The following should work:
public function hotelType() {
return $this->belongsTo('HotelType','type', 'type_id')->select('id','type_id','title');
}
I've obtained the desired result with the following:
Hotel::select('hotels.*','hotels_types.title as type')
->join('hotels_types', 'hotels.type', '=', 'hotels_types.type_id')
->with('HotelData');