I have two tables with this schmea:
mysql> show columns from table_1;
+-------------+------------------+------+-----+---------------------+-----------
| Field | Type | Null | Key | Default | Extra
+-------------+------------------+------+-----+---------------------+-----------
| id | int(10) unsigned | NO | PRI | NULL | auto_incre
| id_world | int(11) | NO | | NULL |
| key | varchar(255) | NO | | NULL |
| name | varchar(255) | NO | | NULL |
| description | varchar(255) | NO | | NULL |
| level | int(11) | NO | | 0 |
| created_at | timestamp | NO | | 0000-00-00 00:00:00 |
| updated_at | timestamp | NO | | 0000-00-00 00:00:00 |
+-------------+------------------+------+-----+---------------------+-----------
8 rows in set (0.00 sec)
and
mysql> show columns from table_2;
+--------------+------------------+------+-----+---------------------+----------
------+
| Field | Type | Null | Key | Default | Extra
|
+--------------+------------------+------+-----+---------------------+----------
| id | int(10) unsigned | NO | PRI | NULL | auto_incr
| key | varchar(255) | NO | | NULL |
| level | int(11) | NO | | NULL |
| name | varchar(255) | NO | | NULL |
| description | varchar(255) | NO | | NULL |
| price | int(11) | NO | | NULL |
| amount | int(11) | NO | | NULL |
| created_at | timestamp | NO | | 0000-00-00 00:00:00 |
| updated_at | timestamp | NO | | 0000-00-00 00:00:00 |
+--------------+------------------+------+-----+---------------------+----------
30 rows in set (0.00 sec)
I want to get all fields "from table_2 where table_2.key = table_1.key AND table_2.level = 10" is this the right way with the hasMany option in my model?
my normal query looks like this:
SELECT A.key AS p_key,
A.name AS p_key,
A.description AS p_desc,
A.level AS p_level,
B.key AS r_key,
B.level AS r_level,
B.name AS r_name,
B.description AS r_desc
FROM
table_1 AS A,
table_2 AS B
WHERE
B.key = A.key AND
B.level = '1'
To build hasMany
relationship using those tables, you need to create two models first, for example:
class TableOne extends Eloquent {
protected $table = 'table_1';
public function tableTwoItems()
{
return $this->hasMany('TableTwo', 'table_2.key', 'table_1.key')
->where('table_2.level', 1);
}
}
class TableTwo extends Eloquent {
protected $table = 'table_2';
}
Once you created these models in your app/models
directory then you may use something like this:
$result = TableOne::with('tableTwoItems')->get();
To select the items/fields, you may use something like this:
$result = TableOne::with(array('tableTwoItems' => function($query){
$query->select('table_2.key as k2', 'table_2.name as name2', 'more...');
}))->select('table_1.key as k1', 'table_1.name as name1', 'more...')->get();
You may access them like:
$result->first()->tableTwoItems->first();
Or you may loop $result
and also you may loop the related items using nested loop. For example:
foreach($result as $tableOneItem)
echo $tableOneItem->name;
foreach($tableOneItem->tableTwoItems as $tabletwoItem)
echo $tabletwoItem->name;
endforeach;
endforeach;
Try to use different field names in both table's key
and make them unique as well. Read the Eloquent Relation documentation for more.