【发布时间】:2021-04-05 17:09:45
【问题描述】:
假设我有 2 个表:
用户:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->integer('age')
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
手机:
Schema::create('phones', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('phone');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')
->onDelete('cascade');
});
用户模型:
public function phone()
{
return $this->hasOne('App\Phone');
}
手机型号:
public function user()
{
return $this->belongsTo('App\User');
}
当我执行以下查询时:
$result = User::select('name AS full_name', 'email AS user_email', DB::raw("'Student' as
profile"))->where('age', '>', 10)->with('phone')->get();
我将手机设为 null。
当只是表演时:
$result = User::where('age', '>', 10)->with('phone')->get();
我可以接电话。
【问题讨论】:
-
你为什么使用
DB::raw("'Student' as profile")?
标签: mysql sql laravel eloquent