第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

獲取另一個(gè)表中不存在的所有項(xiàng)目

獲取另一個(gè)表中不存在的所有項(xiàng)目

PHP
郎朗坤 2023-10-15 16:35:23
我正在使用Laravel Framework 6.16.0并且有兩個(gè)表,logos并且companies:標(biāo)志:        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'];}我想獲取 中沒有徽標(biāo)logo-table且徽標(biāo)尚未更新的所有符號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.*');該查詢的結(jié)果是:select `companies`.* from `companies` left join `logos` on `companies`.`id` = `logos`.`companies_id` where date(`logos`.`updated_at`) = ? order by `name` asc但是,即使我的公司表有足夠的記錄,運(yùn)行此查詢時(shí)我也得不到結(jié)果:有什么建議我做錯了什么嗎?我很感謝你的回復(fù)!
查看完整描述

2 回答

?
森欄

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超5個(gè)贊

您可以使用 Eloquent 來實(shí)現(xiàn)此目的,而不是使用左連接和數(shù)據(jù)庫外觀。


我猜公司和Logo之間的關(guān)系是1:1或1:n。如果我們假設(shè)它是 1:1 那么你的查詢將如下所示:


Company::whereHas('logo', function($query) {

? ? return $query->where('date', '<', Carbon::now()->subDays(90));

})->orWhereDoesntHave('logo')->...

您應(yīng)該檢查:查詢關(guān)系缺失

只要我們遵循 Laravel 命名約定,我們就不需要指定表名,Laravel 會從模型名稱中找出它。

標(biāo)志模型應(yīng)該是這樣的:

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);

? ? }

}

Company 模型也不需要指定表名,它已經(jīng)知道它應(yīng)該是Companies。


公司模式:


class Company extends Model

{

? ? // protected $table = 'companies';


? ? protected $guarded = ['id'];


? ? public function logo()

? ? {

? ? ? ? return $this->hasOne(Logo::class);

? ? }

}

請檢查 $guarded 和 $fillable 屬性如何工作。如果只有id設(shè)置為受保護(hù),則所有其他屬性均不受保護(hù),不確定這是否是您的意圖。


完整的查詢應(yīng)該是:


Company::whereHas('logo', function($query) {

? ? return $query->where('updated_at', '<', Carbon::now()->subDays(90));

})->orWhereDoesntHave('logo')

->orderBy('name', 'ASC')

->get();


查看完整回答
反對 回復(fù) 2023-10-15
?
德瑪西亞99

TA貢獻(xiàn)1770條經(jīng)驗(yàn) 獲得超3個(gè)贊

->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();


查看完整回答
反對 回復(fù) 2023-10-15
  • 2 回答
  • 0 關(guān)注
  • 166 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號