Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

php - laravel json select customer object select perticular data using hasMany

how to select customer object select perticular data

model user has connected to customer hasMnay

 public function customer()
    {
        return $this->hasMany('AppModelsCustomer','employe_id','id');
    }

and user controller

$collection = User::with('customer')->where(['director_id'=> 16])->select('id','name',)->get();
        return $collection;

and response

[
    {
        "id": 25,
        "name": "emp1",
        "customer": [
            {
                "id": 9,
                "shop_owner_name": "afs",
                "email": "[email protected]",
                "address": "a",
                "lat_lng": "a",
                "shop_name": "a",
                "shop_license": "a",
                "contact_number": "1234567890",
                "shop_image": "image/rU54qz67G9pJ7xEc8JSMkZAzxgUE5pb98zQPelMt.png",
                "customer_image": "image/0ylk6OfMGwOqsdcUXpVZapOpPL73oO0gtOk7PXYV.png",
                "director_id": 0,
                "employe_id": 25
            },
            
        ]
    },
]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Controller

$customers = [];
foreach($collection as $item) {

    $customers[] = $item->customer;
}

return $customers;

View

@foreach ($collection as $item)
    @foreach($item->customer as row)
        {{ $row->email }}
    @endforeach
@endforeach

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...