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 ?
Related
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();
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.
I have three tables:->
-- Student(PK userid)
-- Teacher(PK teacherid, FK userid ref...)
-- Rating(PK rateid, FK userid ref....., FK teacherid ref....)
I want to insert into rating table by joining with other tables. How do I achieve that in Laravel 5.2? Please help me out guys!
This can be done in Larvel using Polymorphic relationships
To make it simpler, here's a sample db structure:
student
id - integer
user_id - integer
teacher
id - integer
user_id - integer
ratings
id - integer
value - integer
rateable_id - integer
rateable_type - string
The rateable_id corresponds to either the id on teacher or student
The rateable_type is a string of the class name of the model that is rateable. This is added automatically for you, as explained below.
Your models should look like this:
Rating.php
class Rating extends Model
{
public function rateable()
{
return $this->morphTo();
}
}
Teacher.php
class Teacher extends Model
{
public function rating()
{
return $this->morphOne(Rating::class, 'rateable');
}
}
Student.php
class Student extends Model
{
public function rating()
{
return $this->morphOne(Rating::class, 'rateable');
}
}
Now, here's a small example on how to use this.
$student = Student::find(1);
$rating = new Rating();
$rating->value = 10;
$student->rating()->save($rating);
First you find a student. Then you create a new rating and add a value.
When you do $student->rating()->save($rating); the rateable_id and rateable_type are filled in for you, with the correct value for your Student.php model through the relationship. Simple with Laravel.
I am trying to get a feel around the laravel ORM and I have the following models.
I have a:
user table with- id, firstname, lastname
city table with - id, name
usercity table with - user_id, city_id
The usercity table tracks the cities the user has visited.
I added the following in city model:
public function usercity()
{
return $this->hasMany('App\UserCity');
}
And another function in user model
public function usercity()
{
return $this->hasMany('App\UserCity');
}
I also added a model for UserCity and added following function there.
public function city()
{
return $this->belongsTo('App\City');
}
public function user()
{
return $this->belongsTo('App\User');
}
Now, the goal is to retrieve all the cities a user has visited. I used the following function.
$usercities = User::where('id','=',1)->first()->usercity()->get();
This works in the sense that it retrieves the user_id and city_id.
What would i need to do to get all the fields in the city table also?
Current response:
[[{"user_id":"1","city_id":"1"},{"user_id":"1","city_id":"2"},{"user_id":"1","city_id":"3"},{"user_id":"1","city_id":"4"}]]
I might be able to use inner join but I wanted to see if there was another way to retrieve the data which safely populates the data for me.
What you really have is a many-to-many relationship between users and cities, with the usercity table being the pivot table. Laravel uses the BelongsToMany relationship to implement this. You'll need to make a few changes to get this to work.
In your city model:
public function users() {
return $this->belongsToMany('App\User', 'usercity');
}
In your user model:
public function cities() {
return $this->belongsToMany('App\City', 'usercity');
}
You can get rid of the UserCity model. There is usually no reason to need a model for the pivot table.
The usercity table may need to be updated to add an id field as the primary key. I've not tried it without one, however, so it may work as you have it. Also, if you wanted, you could rename the table to city_user to conform to Laravel conventions, and then you wouldn't need to specify the table name in the relationship definitions.
Once your relationships are setup correctly, you can access a user's cities via the cities relationship on the user, and you can access a city's users via the users relationship on the city. For example:
// all of the cities visited by user 1
$user = User::find(1);
$usercities = $user->cities;
// all of the users that have visited city 1
$city = City::find(1);
$cityusers = $city->users;
You can find more information about the relationships in the documentation here.
I have 4 tables that I'm trying to work with in Laravel and I can't figure out how to use eloquent to execute a particular query. I want to update all orders that belong to a user (through product_id) and that have null payout_id.
This raw sql statement works but I'm not sure how to use eloquent for this..perhaps sync?
UPDATE order_items i JOIN products p on (i.product_id = p.id) SET i.payout_id = null where p.user_id = 3
User Model
Product Model
FK: user_id
Order Model
FK: product_id
FK: payout_id
Payout Model
I would really appreciate any help!
First define a function orders in User Model
public function orders()
{
return $this->hasManyThrough('Orders', 'Product','user_id','product_id');
}
User::where('id','=',$userid)->with(array('orders'=>function($query){
$query->where('payout_id','=',0)->update(array('payout_id'=>$updatevalue));
}))->first();
You need to create a model for your tables i and p, see http://laravel.com/docs/eloquent for full information on model creation.
In the model for i you would then create a relationship to p:
public function p()
{
return $this->('p','id','product_id');
}
You can then run your query as follows:
$results = i::with('p')
->where('user_id', '=', 3)
->update(array('payout_id' => null));