laravel how to get related data from another table with no relation - php

I am working on laravel and i have the following table structure
elments table
+----+--------------+------------+--------+
| id | element | category | cities |
+----+--------------+------------+--------+
| 1 | element 1 | 1 | 1,2,3 |
| 2 | element 2 | 2 | 2,3 |
+----+-----------+---------------+--------+
cities table
+----+-----------+
| id | city |
+----+-----------+
| 1 | city 1 |
| 2 | city 2 |
| 3 | city 3 |
+----+-----------+
categories table
+----+-------------+
| id | category |
+----+-------------+
| 1 | category 1 |
| 2 | category 2 |
| 3 | category 3 |
+----+-------------+
I use the eloquent query with relations as following
$elements =Element::with('categories')->get();
but how to get cities names for each elements from the cities table in the same query

Related

Laravel belongs to many by two intermediate tables?

Hi guys I have to create many to many relationship but through a another table. but laravel BelongsToMany give only 1 intermediate table relation
here table structure:
features Table:
+----+-----------+
| id | text |
+----+-----------+
| 1 | feature 1 |
+----+-----------+
| 2 | feature 2 |
+----+-----------+
feature_values table:
+----+-----------+-------+
| id | feature_id | text |
+----+-----------+-------+
| 1 | 1 | val 1 |
+----+-----------+-------+
| 2 | 1 | val 2 |
+----+-----------+-------+
| 3 | 2 | val 3 |
+----+-----------+-------+
products table:
+----+-------+
| id | name |
+----+-------+
| 1 | tv |
+----+-------+
| 2 | phone |
+----+-------+
product_features table:
+----+------------+------------+
| id | product_id | feature_id |
+----+------------+------------+
| 1 | 1 | 1 |
+----+------------+------------+
| 2 | 2 | 1 |
+----+------------+------------+
| 3 | 2 | 2 |
+----+------------+------------+
product_feature_values table:
+----+--------------------+------------------+
| id | product_feature_id | feature_value_id |
+----+--------------------+------------------+
| 1 | 1 | 1 |
+----+--------------------+------------------+
| 2 | 2 | 1 |
+----+--------------------+------------------+
| 3 | 2 | 2 |
+----+--------------------+------------------+
I'm trying to create a relationship between feature_values and and products through product_feature_values and product_features for get product by feature_values.
I know data can obtain by joining tables, but I looking for a solution that using by laravel model relationships
thank you.
I tried like this
class FeatureValue extends Model
{
public function products()
{
return $this->belongsToMany(Product::class,'product_feature_values')->withTimestamps();
}
}
I'm afraid, that Laravel didn't offer that out-of the box. But there's a package called staudenmeir/belongs-to-through that does.

How to get id's of those with empty values as well as filled values using inner join in mysql

$products = Product::select('products.*','c.name as category_name','b.name as brand_name')
->join('categories as c','products.category_id','c.id')
->join('brands as b','products.brand_id','b.id')
->whereNull('products.deleted_at')
->orderBy('products.created_at','desc')
->get()
->toArray();
The above is the code I am using in laravel to get product details from product table. With the above code I am getting all the products with having brand_id, but I want both products having brand_id as well as without brand_id (shown in table below) using join.
Below is the product table:-
---------------------------------------------------------------------------
| id | category_id | sub_category_id | brand_id | title |
---------------------------------------------------------------------------
| 1 | 3 | 1 | 1 | abc |
---------------------------------------------------------------------------
| 2 | 3 | 3 | 2 | Shirts |
---------------------------------------------------------------------------
| 3 | 3 | 3 | | jeans |
---------------------------------------------------------------------------
| 4 | 1 | 1 | | efg |
---------------------------------------------------------------------------
Below is the brand table:-
--------------------------------------------------------------
| id | category_id | sub_category_id | name |
--------------------------------------------------------------
| 1 | 3 | 1 | abc |
--------------------------------------------------------------
| 2 | 3 | 3 | efg |
--------------------------------------------------------------
Below is the Category Table:-
-------------------
| id | name |
-------------------
| 1 | men |
-------------------
| 2 | women |
-------------------
| 3 | kids |
-------------------

How can I divide table data into multiple tables and keep the relations?

I have four tables: (three o them are empty)
-- all_locations
+----+----------------+---------------+-----------+
| id | name | location_type | parent_id |
+----+----------------+---------------+-----------+
| 1 | Aruba | 0 | 0 |
| 2 | Afghanistan | 0 | 0 |
| 3 | – | 1 | 1 |
| 4 | Balkh | 1 | 2 |
| 5 | Herat | 1 | 2 |
| 6 | Kabol | 1 | 2 |
| 7 | South Hill | 2 | 3 |
| 8 | The Valley | 2 | 3 |
| 9 | Mazar-e-Sharif | 2 | 4 |
| 10 | Herat | 2 | 5 |
+----+----------------+---------------+-----------+
-- country
+----+---------+
| id | name |
+----+---------+
-- province
+----+---------+-------------+
| id | name | country_id |
+----+---------+-------------+
-- city
+----+---------+-------------+
| id | name | province_id |
+----+---------+-------------+
Now I want to divide data of the first table between other tables (based on the location_type valye: 0 means country, 1 means province, 2 means city)
and then drop the first table.
So this is the expected result:
-- country
+----+-------------+
| id | name |
+----+-------------+
| 1 | Aruba |
| 2 | Afghanistan |
+----+-------------+
-- province
+----+---------+------------+
| id | name | country_id |
+----+---------+------------+
| 1 | – | 1 |
| 2 | Balkh | 2 |
| 3 | Herat | 2 |
| 6 | Kabol | 2 |
+----+---------+------------+
-- city
+----+----------------+-------------+
| id | name | province_id |
+----+----------------+-------------+
| 1 | South Hill | 1 |
| 2 | The Valley | 1 |
| 3 | Mazar-e-Sharif | 2 |
| 4 | Herat | 3 |
+----+----------------+-------------+
I can do that by a loop in PHP. Something like this:
foreach( $fetched_rows_from_all_locations as $location ){
switch( $location['location_type'] ){
case '0':
-- INSERT INTO country (name) VALUES ($location['name'])
break;
case '1':
-- INSERT INTO province(name) VALUES ($location['name'])
break;
case '2':
-- INSERT INTO city(name) VALUES ($location['name'])
break;
}
}
But my code doesn't handle the relations. As you know, the values into parent_id column aren't valid anymore. So I have to make the new ids. How can I do that?
My solution would be:
Inserting the countries to the new table - which is the easiest task.
Then inserting the province. Now we have to grab the new IDs of the countries (as the ID column is auto-increment).
First we do a self-JOIN on all_location, to get the parents name.
Then we do a join to the previous new filled table countries on the column name, grab the ID and finally insert the values to the new province table.
Same procedure like above again for the city table.
INSERT INTO country (name)
SELECT name
FROM all_locations
WHERE location_type = 0
INSERT INTO province(name, country_id)
SELECT child.name
,country.id
FROM all_locations child
INNER JOIN all_locations parent
ON child.parent_id = parent.id
INNER JOIN country
ON country.name = parent.name
WHERE child.location_type = 1
INSERT INTO city(name, province_id)
SELECT child.name
,province.id
FROM all_locations child
INNER JOIN all_locations parent
ON child.parent_id = parent.id
INNER JOIN province
ON province.name = parent.name
WHERE child.location_type = 2
DROP TABLE all_locations
If you want to reproduce my solution, you can use this link:
http://rextester.com/XQOZ12449

How to query a closure table to get descendants

I have two tables category and hierarchy
table schema for category:
+----+---------+
| id | name |
+----+---------+
| 1 | Shoes |
| 2 | Sandals |
| 3 | floaters|
| 4 | men |
| 5 | women |
+----+---------+
table schema for hierarchy
+----------+------------+
| ancestor | descendant |
+----------+------------+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 4 |
| 2 | 5 |
+----------+------------+
How to query to get the data in below order
-Shoes
- Sandals
-men
-women
- floaters
I have tried but am new to such complex queries.

Manipulating Foreign Keys output in MYSQL

I have a table connected to another table through foreign key;
First Table is the brand; it has a brand_id and brand_name fields
Second Table is the Product Line; it has a Line_id and line_name fields
Third Table is the Lines Offered; it has a id, Brand (which is a foreign key from the first table), and Line_name (which is a foreign key from the second table.
If i will look into the third table on mysql the fields contains the id numbers of the foreign keys.
My question is this, is it possible that the stored value will be the name itself and not the ID? or Is it possible to add a field on my third table named it as Brand_name which is a VARCHAR that will display the exact brand name from the first table. Example the values of my third table would be '1','3','The Brand Name of brand with id no. 3','25'; - If yes i dont have any idea how to do this.
Nothing is preventing you from doing this. You would simply add a brand_name field to the "third table"... The question begging to be asked is: why do you want to do this?
brands
_________________________
| brand_id | brand_name |
| 1 | brand1 |
| 2 | brand2 |
| 3 | brand3 |
| 4 | brand4 |
| 5 | brand5 |
-------------------------
lines
_________________________
| line_id | line_name |
| 1 | line1 |
| 2 | line2 |
| 3 | line3 |
| 4 | line4 |
| 5 | line5 |
-------------------------
linked
_________________________________________________
| id | line_id | brand_id | brand_name |
| 1 | 5 | 1 | brand1 |
| 2 | 5 | 2 | brand2 |
| 3 | 4 | 2 | brand2 |
| 4 | 4 | 3 | brand3 |
| 5 | 4 | 3 | brand3 |
| 6 | 3 | 4 | brand4 |
| 7 | 3 | 4 | brand4 |
| 8 | 2 | 4 | brand4 |
| 9 | 2 | 5 | brand5 |
| 10 | 1 | 5 | brand5 |
------------------------------------------------
That's your proposed setup. Now if we:
SELECT brand_id, brand_name FROM linked WHERE line_id = 4;
We would get:
_________________________
| brand_id | brand_name |
| 2 | brand2 |
| 3 | brand3 |
| 3 | brand3 |
-------------------------
But the same could be achieved without duplicate data (in large databases it's pretty unreasonable to have duplicate data like that), and without needing to update BOTH the linked and brand tables every time the brand name changes by using:
SELECT linked.brand_id, brands.brand_name
FROM brands, linked
WHERE linked.line_id = 4 AND brands.brand_id = linked.brand_id;
_________________________
| brand_id | brand_name |
| 2 | brand2 |
| 3 | brand3 |
| 3 | brand3 |
-------------------------
That answer didn't need to be that long. I was having fun making tables.

Categories