简体   繁体   中英

Laravel Eloquent Master Detail result

I'm using Laravel 5.1 Just want to ask how to display ORM master detail when child return no data the the master data is not showing

This is My Master Model

<?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');
        }
    }

Child Model

<?php

namespace SpekWeb;

use Illuminate\Database\Eloquent\Model;

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

And this is My Controller

<?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;
    }
}

I want the master record not showing when I filtered the code within 'with' area, using ->has('telcoProduct) still not working for me. The master record still showing on Blade Views. Is there any trick to solve this problems ?

Add a whereHas function before your with . This is essentially like saying "if you have any, give me all records matching these criteria".

Your query should look like this (not tested).

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;
}

Thanx @Tim

But when I using like you said with this code :

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

I've got this result :

"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
    },
]

Please check 2nd record of telco_product, it doesn't match the condition of query

$tpHas->whereMemType(1);

Its display all the record inside telcoProduct join wd_productprice

I've try this code then :

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

And I've got the result as I expected

"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"
    }
]

Is My technique correct ? Should I write the condition twice on the query ?

Thx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM