Laravel Eloquent 用于向用户列出过滤器名称和 link
Laravel Eloquent for listing filters name and link to user
我正在使用来自 url 的过滤器来过滤学校,我需要向用户显示这些过滤器的链接。好吧,实际上我只需要向用户展示使用后的过滤器而不是 return 空结果。我尝试使用范围和关系,但出现此错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'specialties.school_id' in 'where clause' (SQL: select `id`, `name` from `specialties` where ((select count(*) from `schools` where `specialties`.`school_id` = `schools`.`id` and `specialties` like %""%) >= 1))
在我的例子中,我需要按专业、地区、直辖市、城市等过滤学校。这是我的示例学校模型:
<?php
class School extends Eloquent {
protected $table = 'schools';
public function scopeWhereSpecialties($query, $specialties)
{
if( !is_array($specialties) )
{
$specialties = [$specialties];
}
return $query->where(function($q) use ($specialties)
{
foreach($specialties as $specialty){
$q->where('specialties', 'like', '%"'.$specialty.'"%');
}
});
}
// I just delete other scopes to shorten the code
public function listSchoolsEndUser($filters)
{
$schools_data = School::query();
foreach($filters as $filter => $value)
{
call_user_func(array($schools_data, 'where' . studly_case($filter)), $value);
}
return $schools_data->paginate(12);
}
public function listFilters($filters)
{
$specialties_filters = Specialty::select('id', 'name')->whereFilterAvailable($filters)->get()->toArray();
return $specialties_filters;
}
}
还有我的示例专业模型:
<?php
class Specialty extends Eloquent {
protected $table = 'specialties';
public function scopeWhereFilterAvailable($query, $filters)
{
$specialty = $this->id;
return $query->where(function($q) use ($specialty, $filters)
{
$q->whereHas('school', function($q) use ($specialty, $filters) {
$q->where('specialties', 'like', '%"'.$specialty.'"%');
});
});
}
public function school(){
return $this->belongsTo('School');
}
}
Table学校结构类似于:
____________________________________
| id | name | specialties |
|____|_________|___________________|
| 1 | example | ["1","2","3","4"] |
|____|_________|___________________|
而table专业结构类似于:
________________
| id | name |
|____|_________|
| 1 | example |
|____|_________|
问题在于,使用 select('id', 'name')
时,您将查询限制为仅那些属性,因此 school_id
不可用。您应该包括您(和您的关系)需要的所有列:
$specialties_filters = Specialty::select('id', 'name', 'school_id')->whereFilterAvailable($filters)->get()->toArray();
也许 lists()
也是一个选项。它构造一个数组,其中一个属性作为键,另一个作为值:
$specialties_filters = Specialty::whereFilterAvailable($filters)->lists('name', 'id');
我正在使用来自 url 的过滤器来过滤学校,我需要向用户显示这些过滤器的链接。好吧,实际上我只需要向用户展示使用后的过滤器而不是 return 空结果。我尝试使用范围和关系,但出现此错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'specialties.school_id' in 'where clause' (SQL: select `id`, `name` from `specialties` where ((select count(*) from `schools` where `specialties`.`school_id` = `schools`.`id` and `specialties` like %""%) >= 1))
在我的例子中,我需要按专业、地区、直辖市、城市等过滤学校。这是我的示例学校模型:
<?php
class School extends Eloquent {
protected $table = 'schools';
public function scopeWhereSpecialties($query, $specialties)
{
if( !is_array($specialties) )
{
$specialties = [$specialties];
}
return $query->where(function($q) use ($specialties)
{
foreach($specialties as $specialty){
$q->where('specialties', 'like', '%"'.$specialty.'"%');
}
});
}
// I just delete other scopes to shorten the code
public function listSchoolsEndUser($filters)
{
$schools_data = School::query();
foreach($filters as $filter => $value)
{
call_user_func(array($schools_data, 'where' . studly_case($filter)), $value);
}
return $schools_data->paginate(12);
}
public function listFilters($filters)
{
$specialties_filters = Specialty::select('id', 'name')->whereFilterAvailable($filters)->get()->toArray();
return $specialties_filters;
}
}
还有我的示例专业模型:
<?php
class Specialty extends Eloquent {
protected $table = 'specialties';
public function scopeWhereFilterAvailable($query, $filters)
{
$specialty = $this->id;
return $query->where(function($q) use ($specialty, $filters)
{
$q->whereHas('school', function($q) use ($specialty, $filters) {
$q->where('specialties', 'like', '%"'.$specialty.'"%');
});
});
}
public function school(){
return $this->belongsTo('School');
}
}
Table学校结构类似于:
____________________________________
| id | name | specialties |
|____|_________|___________________|
| 1 | example | ["1","2","3","4"] |
|____|_________|___________________|
而table专业结构类似于:
________________
| id | name |
|____|_________|
| 1 | example |
|____|_________|
问题在于,使用 select('id', 'name')
时,您将查询限制为仅那些属性,因此 school_id
不可用。您应该包括您(和您的关系)需要的所有列:
$specialties_filters = Specialty::select('id', 'name', 'school_id')->whereFilterAvailable($filters)->get()->toArray();
也许 lists()
也是一个选项。它构造一个数组,其中一个属性作为键,另一个作为值:
$specialties_filters = Specialty::whereFilterAvailable($filters)->lists('name', 'id');