亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

如何在 Laravel 中實現組合的交叉和內連接?

如何在 Laravel 中實現組合的交叉和內連接?

PHP
蝴蝶不菲 2022-07-29 10:46:33
我在 Laravel 中定義了 3 個表,如下所示:Schema::create('locales', function (Blueprint $table) {    $table->string('id', 2);    $table->string('name', 5000);    $table->timestamps();    $table->primary('id');});Schema::create('i18n_keys', function (Blueprint $table) {    $table->string('id', 255);    $table->timestamps();    $table->primary('id');});Schema::create('i18ns', function (Blueprint $table) {    $table->bigIncrements('id');    $table->string('key', 255);    $table->string('locale', 2);    $table->string('translation', 5000)->nullable();    $table->timestamps();                $table->foreign('key')->references('id')->on('i18n_keys')->onDelete('cascade')->onUpdate('cascade');    $table->foreign('locale')->references('id')->on('locales')->onDelete('cascade')->onUpdate('cascade');    $table->unique(array('key', 'locale'));});現在的問題是如何SELECT以編程方式在 Laravel 中實現以下語句。我的意思是不直接運行 SQL 語句。SELECT `il`.`key`, `il`.`locale`, `in`.`translation` FROM (SELECT `ik`.`id` AS `key`, `lo`.`id` AS `locale` FROM `i18n_keys` as `ik` CROSS JOIN `locales` as `lo`) AS `il` left join `i18ns` as `in` ON `in`.`key` = `il`.`key` and `in`.`locale` = `il`.`locale`;目的是提取他們還沒有翻譯的鍵。但我喜歡使用查詢生成器或 eloquent 或類似的東西來執行此操作,而不是直接傳遞查詢。有什么辦法嗎?
查看完整描述

1 回答

?
慕沐林林

TA貢獻2016條經驗 獲得超9個贊

你可以試試這段代碼:


use App\Models\I18nKey;


$ik = I18nKey::crossJoin('locales as lo')

    ->select('i18n_keys.id AS key', 'lo.id AS locale');


$res = \DB::table(\DB::raw("({$ik->toSql()}) AS il"))

    ->mergeBindings($ik->getQuery())

    ->leftjoin('i18ns as in',function($join){

    $join->on('in.key', '=', 'il.key')

        ->whereColumn('in.locale', 'il.locale');

    })->select('il.key','il.locale','in.translation')->get();


查看完整回答
反對 回復 2022-07-29
  • 1 回答
  • 0 關注
  • 121 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號