History:
DB: Tables clients and titles
A Client can have many titles but a title can only have one client. So i will use a simple one-to-many relationship as illustrated below:
Client Model
class Client extends Eloquent {
public function titles(){
return $this->hasMany('titles');
}
}
Then i simply use the following to get the requested data for the selected title
$clientTitles = Client::find(1)->titles;
All in all this should list all client associated titles.
My question really comes to this as i am looking to split my data within my titles table into smaller tables as i also use some aspects of the titles data somewhere within the system and do not need to get all of the title details every time.
So i would have another three tables related to the titles data table
Titles, Title_Artwork, Title_Details, Titles_List
Now if i use the same as above i will get all the data within the titles table, but not the other three. So how can i then update my relationship to then scrape the other three title tables so when i need to, i can get all the data, rather.
Or is there another way to do this or NO keep to what i have done an just limit the call to the fields i require?
From what I understood, And lets assume that your Titles model is some what like-
class Titles extends Eloquent{
public function titleartwork(){
return ..
}
public function titledetails(){
return ..
}
public function titlelist(){
return ..
}
}
And your client model is-
class Client extends Eloquent {
public function titles(){
return $this->hasMany('titles');
}
}
So in order to get the data of any of these tables Title_Artwork, Title_Details, Titles_List
you can do this
$clientTitles = Client::with('Titles','Titles.titleartwork','Titles.titlelist')->where('id','=',1)->get();
In terms of Laravel it is known as Eager Loading, to know more you can check the doc
It will also work with multiple nesting, I mean if your Title_Artwork also contains relationship like-
class TitleArtwork extends Eloquent{
public function sometable(){
return ..;
}
}
Then you can get the data of sometable using
$clientTitles = Client::with('Titles','Titles.titleartwork.sometable','Titles.titlelist')->where('id','=',1)->get();
Related
A contract has a representative (which is a user).
Currently I get this relationship with an belongsTo directly like this in my Contract model:
class Contract extends Model
{
public function representative()
{
return $this->belongsTo(User::class);
}
I would like to be able to retrieve this relationship through the organization_user table and retrieve the "position" attribute.
I think I should use the hasOneThrough relationship but I can't get what I'm looking for.
class Contract extends Model
{
public function representative()
{
return $this->hasOneThrough(User::class, OrganizationUser::class);
}
Not sure if you're in a position to change your DB structure, but it seems to me that your table contracts and organization_user are duplicating each other, storing the same data.
Would it make sense to leave only one table organization_user as a pivot table with the position?
Then you wouldn't need to make the big loop over few relationship to get the needed data.
I have Order model with another relation OrderPhoto:
public function OrderPhoto()
{
return $this->hasMany('App\OrderPhoto');
}
In turn OrderPhoto model has relation:
public function Photo()
{
return $this->belongsToMany('App\Photo');
}
So, how to get data from OrderModel with related data from third model Photo?
I guess this:
Order::with("OrderPhoto.Photo")->get();
to retrieve only data from Photo model for each Order
So, each Order has some OrderPhotos. Relationship is one to many.
But one item from OrderPhotos is related with primary key from table Photos. It is one to one relation.
My result query should be:
select `photos`.*, `ordersphoto`.`Orders_Id` from `photos` inner join `ordersphoto` on `ordersphoto`.`Photos_Id` = `photos`.`Id` where `ordersphoto`.`Orders_Id` in (1);
How to use hasManyThrough for this query?
Just having a quick look at your relationships it looks like you could create a hasManyThrough relationship on the order Model.
public function Photo {
return $this->hasManyThrough('App\OrderPhoto', 'App\Photo')
}
You may need to add the table keys to make it work
This will allow you to do:
Order::with("Photo")->get();
You can see more details here https://laravel.com/docs/5.5/eloquent-relationships#has-many-through
Update
Try this
public function Photo {
return $this->hasManyThrough('App\Photo', 'App\OrderPhoto', 'Order_id', 'Photos_id', 'id', 'id')
}
It is a little hard to get my head around your DB structure with this info but you should hopefully be able to work it out. This may also help
https://laravel.com/api/5.7/Illuminate/Database/Eloquent/Concerns/HasRelationships.html#method_hasManyThrough
I have four tables in database: groups, specialties, lessons, group_lesson. It's structures:
groups
id
specialty_id
name
specialties
id
name
lessons
id
specialty_id
group_lesson (UNIQUE INDEX lesson_id, group_id)
lesson_id
group_id
date
My models look like that for now:
class Group extends Eloquent {
public function specialty() {
return $this->belongsTo('Specialty');
}
}
class Lesson extends Eloquent {
public function specialty() {
return $this->belongsTo('Specialty');
}
}
class Specialty extends Eloquent {
public function lessons() {
return $this->hasMany('Lesson');
}
public function groups() {
return $this->hasMany('Group');
}
}
I need get additional fields in Group model look like that
Group - Eloquent model
name - string
lessons - collection of Lesson models of Group Specialty
date - date from group_lesson table
I've tried different relationships and combinations, but it's doesn't work. Please help me to write correct relationships.
You can use eager-loading to access relational data through relationships, and can even chain relationships further. As a rule of thumb, if you can draw a path to from 1 model to another through a relationship, you can eagerload all the relevant and relational data for that with chained eager-loads.
Laravel Eager Loading
As an example
$speciality_group = Speciality::with('group','lessons')->find($id);
Even though you are only getting a single instance of the speciality model, the related data is hasMany, meaning multiple records. You need to loop through these records using a foreach loop to access the relevant data for them, or alternitavely add additional closures in your initial query to load only a single related model.
foreach($speciality_group->group as $group)
{
echo $group->name;
}
You will need to do this for both instances where you want to display related information.
I am trying to get all attributes in one JSON.
Eg:
Consider I have a table "place" with "id","name","description" as attributes.
Now,
I have another table "region" with "id","name","place_id","description" as attributes(or) schema.
There is one to many relationship between place and region.
And now,I define another table "street" with "id","name","region_id" as its schema.
There is again One to Many relationship between "region" and "street".
I want to get all details which comes in one place.If there is some place "1,banglore,garden city" and there are two regions with that place_id as
"{"1,r.t.nagar,1,region in banglore"},{"2,ashok nagar,1,region in banglore"}".Here "1" is foreign key referring id of "banglore's id(1)".
Now,similarly if each region has 3 streets in street table.I want to get all the values in one JSON. I know that we can get this with foreach .But,i want an efficient solution to get all nested relationship to my Place model from Region,Street in JSON.
Thanks.
You first need to define one to many relations like:
class Places extends Eloquent{
....
public function regions()
{
return $this->hasMany('App\Region');
}
...
}
And the same to streets:
class Regions extends Eloquent{
....
public function streets()
{
return $this->hasMany('App\Street');
}
....
}
Note that I have assumed that your models stay within App folder and have corresponding namespaces.
After that you can get you model like:
Place::with('regions.streets')->get();
Which will automatically nest all relations and cast to JSON if you send a json response.
I'm sure this is very simple - I just need two columns from a country database to populate a <select>
Looking at this URL I came up with the code below, but this seems to return an object for each record
http://laravel.com/api/class-Illuminate.Database.Eloquent.Model.html#_all
class countries extends Eloquent
{
}
public static function getCountrySelect()
{
return countries::all(array("name","iso_3166_2"));
}
If you want to populate custom field from database use this:->
countries::lists('name','iso_3166_2');
i.e
countries::lists('coulmnname1','coulmnname2');
and if you want to get suppose first 10 entries for that model use this:
countries::take(10)->get();