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 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,