Laravel same model relationships - laravel

I have a table where I store data IDs from same parent table.
Table 1: id, name ,etc
Table 2: first_id, secound_id
Both first_id and secound_id are linked to tabel 1 > id.
public function first() {
$this->belongsTo('First');
}
public function secound() {
$this->belongsTo('First');
}
When I do $someobject->first it returns model First. But when i do $someobject->secound it restuns null. Any idea why?

Related

hasOneThrough condition on intermediate model

i have 3 tables
student
id
guardian_relation_id
student_guardian
id
student_id
guardian_id
guardian_type
Guardian
id
name
i have a relationship function in student model :
public function guardian(){
return $this->hasOneThrough(Guardian::class,StudentGuardian::class,'student_id','id','id','guardian_id');
}
and its working.
but now i want to get get data where guardian_relation_id = guradian_type
how do i achieve this ?

How to fetch data from database if one model has relation with two models in Laravel

I'm trying to fetch data from database using Laravel eloquent but it returns no data. Here is the database structure
Region
id
name
District
id
name
region_id
Ward
id
name
region_id
So the ward doesn't relate with district it relates with Region. How can I get ward(data)? This is how I fetch data
Area::with('region.district.ward')->get();
Models
Region.php
public function district()
{
return $this->hasMany(District::class);
}
public function ward()
{
return $this->hasMany(Ward::class);
}
District.php
public function region()
{
return $this->belongsTo(Region::class);
}
Ward.php
public function regions()
{
return $this->belongsTo(Region::class);
}
You can eager load the descendant tables by using dot notation, when you have data like a chaining structure, like table2 => table2 => table 3
Model1::with('model2.model3')->get();
But you data is not a chaining structure, so you can achieve it by :
Region::with('district')->with('ward')->get();
Or,
Region::with(['district', 'ward'])->get();

Laravel Eloquent relationships with 3 Table

Laravel 3 Table
table_user
id
name
table_barang
id
iduser
name
statusbarang_id
table_statusbarang
id
name
My code UserModels:
public function BarangModels()
{
return $this->hasMany(BarangModels::class, 'iduser');
}
My code BarangModel :
public function UserModels()
{
return $this->belongsTo(UserModels::class);
}
public function StatusBarangModels()
{
return $this->hasOne(StatusBarangModels::class, 'idstatusbarang');
}
My Code StatusBarangModels :
public function BarangModels()
{
return $this->belongsTo(BarangModels::class);
}
My Code Usercontroller
public function showdetail($id)
{
$value = UserModels::find($id);
return view('user/detailuser', compact('value'));
}
And, I want to select barangmodels (id, name) statusbarangmodels (name)
thank you
First you need to add primary key of BarangModel as foreign key in StatusBarangModels for the relationship that you define in models. After adding key use the following code.
UserModels::with(['BarangModels'=>function($item){
$item->with(['StatusBarangModels'=>function($query){
$query->select('statusbarang_id','name');
}])->select('id','name');
}])->get();
You need to select foreign key in nested relationships so that eloquent can match primary and foreign key in tables.
This will work.
Thank you

Laravel eloquent use id from one table to search junction table?

So I have 3 tables
-Business
- id
-Address
- id
-business_address
- business_id
- address_id
And now at the moment when I go into a view business page I pass business->id from business table as $id:
public function displayBusiness($id) {
$business = Business::find($id);
$address = Address::find($id);
Which works absolutely fine at this moment but what if address has a different id?
so:
-Business
- id = 1
-Address
- id = 2
-business_address
- business_id = 1
- address_id = 2
So how can I modify that so that when id in business table = 1 it goes into business_address and find matching address id and bring back records that match it
What you should have is a relationship. So in your Business model you'd add the following.
public function addresses() {
return $this->belongsToMany(Address::class, 'business_address', 'address_id', 'business_id');
}
Then in your Address model you'd have the following.
public function addresses() {
return $this->belongsToMany(Business::class, 'business_address', 'business_id', 'address_id');
}
With this, you can now do the following.
public function displayBusiness($id) {
$business = Business::with('addresses')->find($id);
}
Then you access addresses by doing $business->addresses.
This is all based on what you currently have, so I'm assuming that one Address can belong to several Business. If this should not be the case, you'll need to refactor your relationship and database, as the pivot table isn't needed.

Laravel Eloquent - many to many with where for the pivot table

I'm trying to see what the best way to do the following.
I have 3 tables: users, items and item_user.
users and items table are pretty generic, id and a few columns to hold whatever data.
item_user table has the following structure
id
item_id
user_id
user_type [ 1 - Owner | 2 - Follower | 3 - Something else ]
Relationships:
Each Item has 1 Owner (user type)
Each Item has many followers
Each User can own many Items
Each User can follow many Items
I would like to have the Owner and Followers be the Users table so I don't need to replicate user data. I created a pivot table of item_id, user_id and user_type to hold these relationships.
So the question is how to I do this in Laravel Eloquent?
Item Model looks like:
public function user() {
return $this->belongsToMany('Users');
// This isn't actually correct since it belongs to only one User but not sure how to specify a where user_type = 1;
// return $this->belongsTo('User');
}
User Model looks like:
public function item() {
return $this->hasMany('Item');
}
Thanks!
You can just append the condition to your belongsToMany declaration:
public function user() {
return $this->belongsToMany('Users')->where('user_type', 1);
}
This will return only the User entries that have user_type = 1 in your pivot table. And just to make it more clear you could name the method owner() instead of user() to reflect the added condition.

Resources