![](/img/trans.png)
[英]Php Mysql - Get results that do not exist in table depend another table
[英]Get all items that do not exist in another table
我正在使用Laravel Framework 6.16.0
並且我有兩個表格、 logos
和companies
:
標志:
Schema::create('logos', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('companies_id')->default('999999');
$table->string('logo_path')->nullable($value = true); // path to the logo image
$table->timestamps();
});
公司:
Schema::create('companies', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('symbol'); // AAPL
$table->string('name'); // company name such as Apple Inc.
$table->timestamps();
});
楷模:
class Logo extends Model
{
protected $table = 'logos';
protected $guarded = ['id'];
public function company()
{
return $this->belongsTo(Company::class, 'companies_id');
}
}
class Company extends Model
{
protected $table = 'companies';
protected $guarded = ['id'];
}
我想在logo-table
中獲取所有沒有 logo 並且 logo 已經90 days
沒有更新的符號。
我嘗試了以下方法:
$symbolsWithoutLogos = DB::table('companies')
>leftJoin('logos', 'companies.id', '=', 'logos.companies_id')
->whereDate('logos.updated_at', Carbon::now()->subDays(90))
->whereNull('companies.logo_image')
->orderBy('name', 'ASC')
->get('companies.*');
此查詢導致:
select `companies`.* from `companies` left join `logos` on `companies`.`id` = `logos`.`companies_id` where date(`logos`.`updated_at`) = ? order by `name` asc
但是,即使我的公司表有足夠的記錄,我在運行此查詢時也沒有得到結果:
有什么建議我做錯了嗎?
感謝您的回復!
->whereDate('logos.updated_at' whereDate 僅比較 dateTime 列的日期部分...嘗試使用來自 carbon 的 toDateString()...
$symbolsWithoutLogos = DB::table('companies')
->leftJoin('logos', 'companies.id', '=', 'logos.companies_id')
->whereDate('logos.updated_at', Carbon::now()->subDays(90)->toDateString())
->whereNull('companies.logo_image')
->orderBy('name', 'ASC')->select('companies.*')
->get();
您可以使用 Eloquent 而不是使用左連接和 DB 外觀來實現此目的。
我猜 Company 和 Logo 之間的關系是 1:1 或 1:n。 如果我們假設它是 1:1,那么您的查詢將如下所示:
Company::whereHas('logo', function($query) {
return $query->where('date', '<', Carbon::now()->subDays(90));
})->orWhereDoesntHave('logo')->...
您應該檢查: 查詢關系缺失
只要我們遵循 Laravel 命名約定,我們不需要指定表名,Laravel 會從 model 名稱中找出來。
徽標 model 應該是這樣的:
class Logo extends Model
{
// protected $table = 'logos';
// not necessary, Laravel already knows to use logos table
protected $guarded = ['id'];
public function company()
{
// return $this->belongsTo(Company::class, 'companies_id');
// no need to specify foreign key if we follow naming convention
// foreign key should be {foreign_model_singular}_id
// in this case it is company_id
return $this->belongsTo(Company::class);
}
}
公司 model 也不需要指定表名,它已經知道應該是公司。
公司 model:
class Company extends Model
{
// protected $table = 'companies';
protected $guarded = ['id'];
public function logo()
{
return $this->hasOne(Logo::class);
}
}
請檢查 $guarded 和 $fillable 屬性是如何工作的。 如果只有id設置為受保護,那么所有其他屬性都是不受保護的,不確定這是否是您的意圖。
完整的查詢應該是:
Company::whereHas('logo', function($query) {
return $query->where('updated_at', '<', Carbon::now()->subDays(90));
})->orWhereDoesntHave('logo')
->orderBy('name', 'ASC')
->get();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.