与Eloquent Laravel建立了很多关系

Evo*_*tio 2 php mysql model laravel eloquent

这个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)
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

我希望得到所有字段"从table_2 where table_2.key = table_1.key AND table_2.level = 10"这是在我的模型中使用hasMany选项的正确方法吗?

我的正常查询如下所示:

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'
Run Code Online (Sandbox Code Playgroud)

The*_*pha 5

hasMany使用这些表建立关系,首先需要创建两个模型,例如:

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';
}
Run Code Online (Sandbox Code Playgroud)

app/models目录中创建这些模型后,您可以使用以下内容:

$result = TableOne::with('tableTwoItems')->get();
Run Code Online (Sandbox Code Playgroud)

要选择项目/字段,您可以使用以下内容:

$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();
Run Code Online (Sandbox Code Playgroud)

您可以访问它们,如:

$result->first()->tableTwoItems->first();
Run Code Online (Sandbox Code Playgroud)

或者您可以循环$result,也可以使用嵌套循环循环相关项.例如:

foreach($result as $tableOneItem)

    echo $tableOneItem->name;

    foreach($tableOneItem->tableTwoItems as $tabletwoItem)

        echo $tabletwoItem->name;

    endforeach;

endforeach;
Run Code Online (Sandbox Code Playgroud)

尝试在两个表中使用不同的字段名称key,并使它们也是唯一的.阅读Eloquent Relation文档了解更多信息.