如何在whereBetween条件下用列名编写数据透视表?

时间:2019-02-07 09:42:38

标签: laravel

我有关系包含数据透视表,当我运行代码时出现此问题 未找到列:1054“ where子句”中的未知列“ guest_show.show_date”(SQL:从guests中选择*,其中guest_fname为空,guest_showshow_date在05之间/ 20/2018和05/20/2018)。 有什么解决办法? 这是我的模型和功能

class Guest extends Model
{
protected $primaryKey = 'guest_id';
protected $table = 'guests';
protected $fillable = 
['guest_fname','guest_lname','profession','mobile','work_phone',
        'current_job','previous_job','work_address','DOB','DD'];

public function programs()
{
    return $this->belongsToMany(Program::class, 'guest_show', 'guest_id', 
'program_id')
        ->withPivot('subject', 'show_date');
}

public function specialties()
{
    return $this->belongsToMany(Specialization::class, 'guest_speciality', 
'guest_id', 'speciality_id');
}




 public static function filter()
 {
    // Search for a guest based on their name.
    $guests=Guest::with('programs', 'specialties');

    if (request()->has('guest_fname')) {
        $guests->where('guest_fname', request()->input('guest_fname'));
    }

    // Search for a guest based on their programs.
    if (request()->has('program_name')) {
        $guests->where('program_name', request()->input('program_name'));
    }

    // Search for a guest based on their specialties.
    if (request()->has('specialty_name')) {
        $guests->where('specialty_name', request()- 
  >input('specialty_name'));
    }

    // Search for a guest based on their date.
    $datefrom=request()->input('from');
    $dateto = request()->input('to');
    if (request()->has('from') || request()->has('to')) {
       $guests->whereBetween('guest_show.show_date', [$datefrom, $dateto]);
    }
    return $guests->get();
}

1 个答案:

答案 0 :(得分:0)

尝试关闭。

 $guests=Guest::with('programs' => function($query) use ($datefrom,$dateto){
   $query->whereBetween('guest_show.show_date', [$datefrom, $dateto]);    })
, 'specialties');

P.S:我只修改了方法之间的位置。