「 ORM 」 一覧
-
モデル周り
123456789101112131415161718192021222324Schema::create('user_mails', function (Blueprint $table) {$table->bigIncrements('user_mail_id');$table->bigInteger('user_id')->unsigned()->comment('ユーザーID');$table->timestamps();$table->softDeletes();$table->foreign('user_id')->references('user_id')->on('users');});protected $fillable = ['user_id',];=Userpublic function userMails(): HasMany{return $this->hasMany('App\Models\UserMail', 'user_id');}=UserMailpublic function user(): BelongsTo{return $this->belongsTo('App\Models\User', 'user_id');}データ抽出
123456789101112131415$query = User::leftJoin('user_mails', function ($join) {$join->on('users.user_id', '=', 'user_mails.user_id')->whereNull('user_mails.deleted_at');});$query->select(['users.email',\DB::raw('(case when user_mails.user_mail_id is not null then 1 else 0 end) as is_mail')]);$query->where('user_mails.user_mail_id', 1);$query->where(function ($subQuery) use ($keyword) {$subQuery->where('users.name', 'LIKE', "%{$keyword}%")->orWhere('users.email', 'LIKE', "%{$keyword}%");});