gpt4 book ai didi

php - 获取 hasMany 与 Eloquent Laravel 的关系

转载 作者:行者123 更新时间:2023-11-29 01:28:46 24 4
gpt4 key购买 nike

我有两个带有这个 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)

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)

我想获取“来自 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'

最佳答案

要使用这些表建立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';
}

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

$result = TableOne::with('tableTwoItems')->get();

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

$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();

您可以像这样访问它们:

$result->first()->tableTwoItems->first();

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

foreach($result as $tableOneItem)

echo $tableOneItem->name;

foreach($tableOneItem->tableTwoItems as $tabletwoItem)

echo $tabletwoItem->name;

endforeach;

endforeach;

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

关于php - 获取 hasMany 与 Eloquent Laravel 的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25672069/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com