SQLSTATE 42S22找不到列

时间:2019-07-15 12:40:04

标签: laravel-5

我知道phpmyadmin中没有该表 但问题是我不知道该怎么做

代码

    public function DriverRefer($id)
{
    $driver = Driver::find($id);
    $query = ReferralDiscount::where([['referral_driver_id', '=', $id], ['referral_sender_id', '!=', 0]])->latest();
    $referral_details = $query->paginate(25);
    return view('merchant.driver.driver_refer', compact('referral_details', 'driver'));
}
  

Illuminate \ Database \ QueryException(42S22)   SQLSTATE [42S22]:找不到列:1054“ where子句”中的未知列“ referral_driver_id”(SQL:从referral_discounts中选择count(*)作为聚合,其中(referral_driver_id = 4130和{{1} }!= 0))   以前的例外   SQLSTATE [42S22]:找不到列:1054“ where子句”中的未知列“ referral_driver_id”(42S22)

1 个答案:

答案 0 :(得分:0)

我在App / Models中添加了一个名为DriverReferralDiscount的模型:

<?php

class DriverReferralDiscount extends Model
{
    protected $guarded = [];

    public function Driver()
    {
        return $this->belongsTo(Driver::class, 'referral_driver_id');
    }

    public function SenderDriver()
    {
        return $this->belongsTo(Driver::class, 'referral_sender_id');
    }

    public function getSenderDetails($driver_id)
    {
        $refer = ReferralDiscount::where([['referral_driver_id', '=', $driver_id], ['referral_sender_id', '!=', 0]])->get();
        return $refer;
    }

    public function getSenderCount($driver_id)
    {
        $refer = ReferralDiscount::where([['referral_driver_id', '=', $driver_id], ['referral_sender_id', '!=', 0]])->count();
        return $refer;
    }}

并将代码更改为

public function DriverRefer($id){
$driver = Driver::find($id);
$query = DriverReferralDiscount::where([['referral_driver_id', '=', $id], ['referral_sender_id', '!=', 0]])->latest();
$referral_details = $query->paginate(25);
return view('merchant.driver.driver_refer', compact('referral_details', 'driver'));}

然后我在phpmyadmin中添加了一个名为driver_referral_discounts的表,其列为:

id  
referral_driver_id  
referral_sender_id  
referral_offer  
referral_offer_value    
referral_available  
created_at  
updated_at

很抱歉,如果不清楚,但是我尽了最大努力,以防其他人得到帮助。