dongman5539 2014-09-04 17:56
浏览 33
已采纳

与Eloquent Laravel建立了很多关系

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'
  • 写回答

1条回答 默认 最新

  • douwei9759 2014-09-04 19:25
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探