Search Laravel雄辩:根据模型自身的属性、关系属性和远关系属性过滤模型
我有一个简单的,我认为经常发生的情况,但我无法在整个互联网上找到任何解决方案 有四个相关模型:地点、位置、省份和国家。其中每个都有几个属性(如名称) 国家: 在国家/地区模型中,我希望找到名称与给定搜索字符串匹配的所有国家/地区,这非常简单:Search Laravel雄辩:根据模型自身的属性、关系属性和远关系属性过滤模型,search,laravel-4,orm,model,eloquent,Search,Laravel 4,Orm,Model,Eloquent,我有一个简单的,我认为经常发生的情况,但我无法在整个互联网上找到任何解决方案 有四个相关模型:地点、位置、省份和国家。其中每个都有几个属性(如名称) 国家: 在国家/地区模型中,我希望找到名称与给定搜索字符串匹配的所有国家/地区,这非常简单: class Country extends \Eloquent { public function provinces() { return $this->hasMany('Province'); }
class Country extends \Eloquent
{
public function provinces()
{
return $this->hasMany('Province');
}
[...]
public function scopeSearch($query, $string)
{
if (isset($string) and !is_null($string)) {
$terms = explode(' ', $string); // Array of searchstrings
foreach ($terms as $term) {
$query = $query->orWhere('name', 'like', '%' . $term . '%');
}
}
return $query;
}
}
省:
在省模型中,我想查找名称与给定搜索字符串匹配的所有省(如在国家模型中),但也要查找国家名称与此搜索字符串匹配的省。这也不是很困难:
class Province extends \Eloquent
{
public function country()
{
return $this->belongsTo('Country');
}
public function locations()
{
return $this->hasMany('Location');
}
[...]
public function scopeSearch($query, $string)
{
if (isset($string) and !is_null($string)) {
$terms = explode(' ', $string); // Array of searchstrings
foreach ($terms as $term) {
$query = $query->orWhereHas('country', function ($query) use ($term) {
$query->where('name', 'like', '%' . $term . '%');
});
$query = $query->orWhere('name', 'like', '%' . $term . '%');
}
}
return $query;
}
}
位置:
现在它变得棘手了:在位置模型中,我想找到名称与给定搜索字符串匹配的所有位置,但也要找到省,省名称与此搜索字符串匹配,这是棘手的部分,国家名称与给定搜索字符串匹配。目前我不知道如何才能做到这一点
class Location extends \Eloquent
{
public function province()
{
return $this->belongsTo('Province');
}
public function places()
{
return $this->hasMany('Place');
}
[...]
public function scopeSearch($query, $string)
{
if (isset($string) and !is_null($string)) {
$terms = explode(' ', $string); // Array of searchstrings
foreach ($terms as $term) {
$query = $query->orWhereHas('province', function ($query) use ($term) {
$query->where('name', 'like', '%' . $term . '%');
});
$query = $query->orWhere('name', 'like', '%' . $term . '%');
}
}
return $query;
}
}
class Place extends \Eloquent
{
public function location()
{
return $this->belongsTo('Location');
}
[...]
public function scopeSearch($query, $string)
{
if (isset($string) and !is_null($string)) {
$terms = explode(' ', $string); // Array of searchstrings
foreach ($terms as $term) {
$query = $query->orWhereHas('location', function ($query) use ($term) {
$query->where('name', 'like', '%' . $term . '%');
});
$query = $query->orWhere('name', 'like', '%' . $term . '%');
}
}
return $query;
}
}
地点:
这里也是一样:在Place模型中,我想查找名称与给定搜索字符串匹配的所有地方,但也要查找位置,其中位置名称与此搜索字符串匹配,省名称与给定搜索字符串匹配,当然还有国家名称与搜索字符串匹配。还有:目前我不知道如何才能做到这一点
class Location extends \Eloquent
{
public function province()
{
return $this->belongsTo('Province');
}
public function places()
{
return $this->hasMany('Place');
}
[...]
public function scopeSearch($query, $string)
{
if (isset($string) and !is_null($string)) {
$terms = explode(' ', $string); // Array of searchstrings
foreach ($terms as $term) {
$query = $query->orWhereHas('province', function ($query) use ($term) {
$query->where('name', 'like', '%' . $term . '%');
});
$query = $query->orWhere('name', 'like', '%' . $term . '%');
}
}
return $query;
}
}
class Place extends \Eloquent
{
public function location()
{
return $this->belongsTo('Location');
}
[...]
public function scopeSearch($query, $string)
{
if (isset($string) and !is_null($string)) {
$terms = explode(' ', $string); // Array of searchstrings
foreach ($terms as $term) {
$query = $query->orWhereHas('location', function ($query) use ($term) {
$query->where('name', 'like', '%' . $term . '%');
});
$query = $query->orWhere('name', 'like', '%' . $term . '%');
}
}
return $query;
}
}
那么,有人知道我如何解决这个问题吗?有(简单的)方法吗?有谁知道Laravel4插件可以帮我解决这个问题吗?还是我完全走错了路,不得不在控制器上做这些事情
谢谢 数据库中的位置和位置是否存储在各自的表中?是的,每个模型都有自己的表。