@giwrgos Use joins. And format your code.
How to make one array of results using 3 tables
Hello everyone, I'm looking for a help if anyone can help me. Basically I'm having 3 tables in my database, the one is called category the second property and the third one is used to relate the category with property and is called property_relationship. The table property_relationship has the following fields. id, category_id, sub_category_id,property_id.
From here I'm collecting all the available properties_ids of the given category_id using the following lines: $propertiesid_Array = Properties_relation::where('category_id', '=', $category->category_id)->lists('property_id');
and then I'm doing the following to collect all the properties $properties = $this->property->whereIn('property_id', $propertiesid_Array)->whereNull("deleted_at")->orderBy('updated_at', 'DESC')->paginate(20);
This is working fine. What I'm trying now to to is to add an extra field inside the $properties instance that will contain the name of the sub_category and then using the same loop that I have into the view to print this field.
for example we have the following data
category: ["id"=>1,"category_title"=>"Category A","type"=>"parent"], ["id"=>2,"category_title"=>"Sub Category A","type"=>"sub"]
property: ["id"=>1,"property_title"=>"Property A"],
property_relationship: ["id"=>1,"category_id"=>1,"sub_category_id"=>2,"property_id"=>1]
So the $properties variable I want to be a list with all the available property_relationship having the following attributes ["id"=>1,"category_title"=>"Sub Category A","property_title"=>"Property A" ]
Does anyone knows how I can do it?
Thank you
Ok i found the solution after long time.
inside the model of the property i made the following methods
public function category_title($id) {
return Category::where("category_id","=",$id)->select("category_title")->first();
}
public function category()
{
return $this->hasOne('Properties_relation');
}
inside the Category model this
public function relationships() {
return $this->hasMany('Properties_relation');
}
and it work. Thanks for the help
Please or to participate in this conversation.