簡體   English   中英

Laravel雄辯的主要細節結果

[英]Laravel Eloquent Master Detail result

我使用的是Laravel 5.1,只是想問一問當孩子沒有返回任何數據且主數據未顯示時如何顯示ORM主數據細節

這是我的大師模范

<?php

namespace SpekWeb;

use Illuminate\Database\Eloquent\Model;

    class ProdukGroup extends Model
    {
        public $table = 'product_group';
        public $primaryKey = 'prd_group';

        public function telcoProduct() {
            return $this->hasMany('SpekWeb\TelcoProduct', 'prd_group', 'prd_group');
        }
    }

子模型

<?php

namespace SpekWeb;

use Illuminate\Database\Eloquent\Model;

class TelcoProduct extends Model
{
    public $table = 'telco_product';
    public $primaryKey = 'tel_prd';
}

這是我的控制器

<?php

namespace SpekWeb\Http\Controllers;

use Illuminate\Http\Request;
use SpekWeb\Http\Requests;
use SpekWeb\Http\Controllers\Controller;
use SpekWeb\ProdukGroup;
use SpekWeb\TelcoProduct;

class ProdukController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $memberPrices = ProdukGroup::with(array(
            'telcoProduct' => function($tpJoin) {
                $tpJoin->join('wd_productprice','telco_product.tel_prd','=','wd_productprice.tel_prd');
                $tpJoin->where('charge','>=',0);
                $tpJoin->whereMemType('1'); //variabel
                $tpJoin->where(function($qWhere) {
                    $qWhere->whereKodeKec('ASTAY'); //variabel
                    $qWhere->orWhereNull('cluster_gid');
                });
            },
            )
        )
        ->has('telcoProduct')
        ->get();
        return $memberPrices;
    }
}

當我使用-> has('telcoProduct)仍然對我不起作用時,我不希望在過濾'with'區域中的代碼時顯示主記錄。 主記錄仍顯示在“刀片視圖”上。 有解決這個問題的技巧嗎?

添加whereHas您之前的功能with 本質上,這就像在說“如果有的話,給我所有符合這些條件的記錄”。

您的查詢應如下所示(未經測試)。

public function index()
{
    $memberPrices = ProdukGroup::whereHas('telco_product', function($query) {
            $query->join('wd_productprice','telco_product.tel_prd','=','wd_productprice.tel_prd');
            $query->where('charge','>=',0);
            $query->whereMemType('1'); //variabel
            $query->where(function($qWhere) {
                $qWhere->whereKodeKec('ASTAY'); //variabel
                $qWhere->orWhereNull('cluster_gid');
            });
        },
        )
    )
    ->with(array(
        'telcoProduct' => function($tpJoin) {
            $tpJoin->join('wd_productprice','telco_product.tel_prd','=','wd_productprice.tel_prd');
            $tpJoin->where('charge','>=',0);
            $tpJoin->whereMemType(1); //this value replace by dynamic Input
            $tpJoin->where(function($qWhere) {
                $qWhere->whereKodeKec('ASTAY'); //this value replace by dynamic Input
                $qWhere->orWhereNull('cluster_gid');
            });
        },
    ))
    ->get();
    return $memberPrices;
}

謝謝@Tim

但是當我像你這樣使用時說以下代碼:

    $memberPrices = ProdukGroup::whereHas('telcoProduct', function($tpHas){
            $tpHas->join('wd_productprice','telco_product.tel_prd','=','wd_productprice.tel_prd');
            $tpHas->where('charge','>=',0);
            $tpHas->whereMemType(1); //this value replace by dynamic Input
            $tpHas->where(function($qWhere) {
                $qWhere->whereKodeKec('ASTAY'); //this value replace by dynamic Input
                $qWhere->orWhereNull('cluster_gid');
            });
        }
    )
    ->with(array(
        'telcoProduct' => function($tpJoin) {
            $tpJoin->join('wd_productprice','telco_product.tel_prd','=','wd_productprice.tel_prd');
        },
    ))
    ->has('telcoProduct')
    ->get();
    return $memberPrices;

我有這個結果:

"prd_group": 1,
"grp_name": "Telkomsel",
"topup_code": "777",
"balance_code": "776",
"tel_id": 1,
"product_type": "reguler",
"with_code": 1,
"telco_product": [
    {
        "tel_prd": 4,
        "prd_group": 1,
        "prd_value": 5000,
        "stock": null,
        "keyword": "S5",
        "charge": 0,
        "price": 5000,
        "mem_type": 1,
        "prd_id": 4,
        "cluster_gid": null,
        "kode_kec": null
    },
    {
        "tel_prd": 4,
        "prd_group": 1,
        "prd_value": 5000,
        "stock": null,
        "keyword": "S5",
        "charge": 0,
        "price": 5100,
        "mem_type": 2,
        "prd_id": 4,
        "cluster_gid": null,
        "kode_kec": null
    },
]

請檢查telco_product的第二條記錄,它與查詢條件不匹配

$ tpHas-> whereMemType(1);

它顯示telcoProduct中所有記錄的加入wd_productprice

我已經嘗試過此代碼:

    $memberPrices = ProdukGroup::whereHas('telcoProduct', function($tpHas){
            $tpHas->join('wd_productprice','telco_product.tel_prd','=','wd_productprice.tel_prd');
            $tpHas->where('charge','>=',0);
            $tpHas->whereMemType(1); //this value replace by dynamic Input
            $tpHas->where(function($qWhere) {
                $qWhere->whereKodeKec('ASTAY'); //this value replace by dynamic Input
                $qWhere->orWhereNull('cluster_gid');
            });
        }
    )
    ->with(array(
        'telcoProduct' => function($tpJoin) {
            $tpJoin->join('wd_productprice','telco_product.tel_prd','=','wd_productprice.tel_prd');
            $tpJoin->where('charge','>=',0);
            $tpJoin->whereMemType(1); //this value replace by dynamic Input
            $tpJoin->where(function($qWhere) {
                $qWhere->whereKodeKec('ASTAY'); //this value replace by dynamic Input
                $qWhere->orWhereNull('cluster_gid');
            });
        },
    ))
    ->has('telcoProduct')
    ->get();
    return $memberPrices;

我得到了預期的結果

"prd_group": 1,
"grp_name": "Telkomsel",
"topup_code": "777",
"balance_code": "776",
"tel_id": 1,
"product_type": "reguler",
"with_code": 1,
"telco_product": [
    {
    "tel_prd": 4,
    "prd_group": 1,
    "prd_value": 5000,
    "stock": null,
    "keyword": "S5",
    "charge": 0,
    "price": 5000,
    "mem_type": 1,
    "prd_id": 4,
    "cluster_gid": null,
    "kode_kec": null
    },
    {
    "tel_prd": 4,
    "prd_group": 1,
    "prd_value": 5000,
    "stock": null,
    "keyword": "SS5",
    "charge": 0,
    "price": 5000,
    "mem_type": 1,
    "prd_id": 148,
    "cluster_gid": 3,
    "kode_kec": "ASTAY"
    }
]

我的技術正確嗎? 我應該在查詢中寫兩次條件嗎?

謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM