[英]Querying one-to-one in laravel
I have two models city and business.I have to perform below queries 我有城市和商业两个模型。我必须执行以下查询
Here is the models 这是模特
Business 商业
class Business extends \Eloquent
{
protected $fillable = [
'business_type',
'first_name',
'last_name',
'email',
'password',
'designation_id',
'name',
'description',
'portfolio_id',
'image',
'city_id',
'package_id',
'group_tag_id'
];
public function city()
{
return $this->belongsTo('City');
}
}
City 市
class City extends \Eloquent
{
protected $fillable = ['name', 'district_id'];
public function business()
{
return $this->hasMany('Business');
}
}
So How can i do this? 那我该怎么做呢?
Hi You can fetch this by this ways : 嗨,您可以通过以下方式获取此信息:
Find Top 10 cities which have the maximum business: 查找营业额最高的十大城市:
$city = City::with('business')->get()->sortBy(function($query) {
return $query->business->count();
}, SORT_REGULAR, true)
->take(10);
Find Business by city name. 按城市名称查找商家。
Business::whereHas('city', function ($q) {
$q->where('name', 'like', 'search_string');//name is the city_name as per your attributes name
})->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.