laravel5.3 一對多表 分頁查詢

// 表結構

mysql> desc modules;
+------------+------------------+------+-----+---------+----------------+
| Field      | Type             | Null | Key | Default | Extra          |
+------------+------------------+------+-----+---------+----------------+
| id         | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| module_dec | int(10) unsigned | NO   |     | NULL    |                |
| module_hex | varchar(10)      | NO   |     | NULL    |                |
| msg        | varchar(255)     | NO   |     | NULL    |                |
| name       | varchar(20)      | NO   |     | NULL    |                |
| status     | tinyint(1)       | YES  |     | 0       |                |
| created_at | timestamp        | YES  |     | NULL    |                |
| updated_at | timestamp        | YES  |     | NULL    |                |
| deleted_at | timestamp        | YES  |     | NULL    |                |
+------------+------------------+------+-----+---------+----------------+
9 rows in set (0.01 sec)

mysql> desc objects;
+------------+------------------+------+-----+---------+----------------+
| Field      | Type             | Null | Key | Default | Extra          |
+------------+------------------+------+-----+---------+----------------+
| id         | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| module_dec | int(10) unsigned | NO   | MUL | NULL    |                |
| object_dec | int(10) unsigned | NO   |     | NULL    |                |
| object_hex | varchar(255)     | NO   |     | NULL    |                |
| name       | varchar(20)      | NO   |     | NULL    |                |
| msg        | varchar(255)     | NO   |     | NULL    |                |
| status     | tinyint(1)       | YES  |     | 0       |                |
| created_at | timestamp        | YES  |     | NULL    |                |
| updated_at | timestamp        | YES  |     | NULL    |                |
| deleted_at | timestamp        | YES  |     | NULL    |                |
+------------+------------------+------+-----+---------+----------------+
10 rows in set (0.01 sec)

mysql> desc errors;
+------------+------------------+------+-----+---------+----------------+
| Field      | Type             | Null | Key | Default | Extra          |
+------------+------------------+------+-----+---------+----------------+
| id         | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| module_dec | int(10) unsigned | NO   | MUL | NULL    |                |
| object_dec | int(10) unsigned | NO   |     | NULL    |                |
| hex        | varchar(20)      | NO   |     | NULL    |                |
| dec        | int(10) unsigned | NO   |     | NULL    |                |
| hex_code   | varchar(10)      | NO   |     | NULL    |                |
| dec_code   | int(10) unsigned | NO   |     | NULL    |                |
| name       | varchar(20)      | NO   |     | NULL    |                |
| msg        | varchar(255)     | NO   |     | NULL    |                |
| status     | tinyint(1)       | YES  |     | 0       |                |
| created_at | timestamp        | YES  |     | NULL    |                |
| updated_at | timestamp        | YES  |     | NULL    |                |
| deleted_at | timestamp        | YES  |     | NULL    |                |
+------------+------------------+------+-----+---------+----------------+
13 rows in set (0.01 sec)

// 關係模型的定義, 逆向的
Module.php
      public function  errors(){
        return $this->hasMany('App\Object','module_dec','module_dec');
    }

Object.php
     /**
     *  對象 & 錯誤碼.
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public  function errors(){
        return $this->hasMany('App\Error','object_dec','object_dec');
    }

Error.php
    /**
     * 錯誤碼 & 模塊
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function  modules(){
        return $this->belongsTo('App\Module','module_dec','module_dec');
    }


    /**
     * 錯誤碼 & 對象.
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function objects(){
        return $this->belongsTo('App\Object','object_dec','object_dec');
    }

// 查詢處理

        $res =\App\Error::with(['objects'=>function($query){
            $query->with('modules');
        }])->paginate(2);
       
         return View('you page name',['res'=>$res]);   # 爲了銜接頁面上的變量.

// 頁面解析過程

 @foreach ($res as $user)
                <tr>
                    <td>{{ $user->name }}</td>
                    <td>{{ $user['modules']['name'] }}</td>
                    <td>{{ $user['modules']['module_dec'] }}</td>
                    <td>{{ $user['objects']['name'] }}</td>
                    <td>{{ $user['objects']['object_dec'] }}</td>
                    <td>{{ $user->dec }}</td>
                    <td>{{ $user->hex }}</td>
                    <td>{{ $user->hex_code }}</td>
                    <td>{{ $user->dec_code }}</td>
                    <td>{{ $user->msg }}</td>
                    <td>@if( !$user->status ) 啓用 @else 禁用 @endif</td>
                    <td>
                        <a href="{{ URL::route('showErrors',[$user->id]) }}">編輯</a>
                        <a href="{{ URL::route('deleteError',[$user->id]) }}">刪除</a>
                    </td>
                </tr>
            @endforeach
        </table>
    </div>
    {{ $error->links() }}

###############################
1  寫在最後,主要是頁面上解析的時候,用的是數組的方式表達的,
2  若是模型中使用了軟刪除的特性, 那麼在該查詢的過程當中是會過濾掉被軟刪除的,請注意,
3  若是要添加新的條件能夠本身參考手冊寫,
4  $res = $this->model->with('objects','modules')->paginate(2);  這樣的也是能夠的,
5  若是查詢的過程當中使用了groupBy,出現mysql(1055)錯誤,請參考連接
   https://github.com/laravel/framework/issues/14997
相關文章
相關標籤/搜索