【发布时间】:2022-01-09 10:10:35
【问题描述】:
我正在尝试测试记录何时为空。不知道为什么这不起作用。 user_id 是我的外键。当没有记录时,我喜欢它显示它是空的,并且当它被添加以显示它被添加时。我正在手动添加删除记录来测试它。
迁移
Schema::create('business_dashboards', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->onDelete('cascade');
$table->string('business_name');
$table->string('website');
$table->timestamps();
});
型号
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class BusinessDashboard extends Model
{
use HasFactory;
protected $fillable = [
'business_name',
'website',
];
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function userprofile()
{
return $this->belongsTo(User::class);
}
}
控制器
$businessDashboardUserId = BusinessDashboard::where('user_id', null)->first();
if ($businessDashboardUserId) {
dd('Is Null');
} else {
dd('Not Null');
}
【问题讨论】:
-
不工作到底如何?这段代码的结果是什么?还有一件事当
user_id为空时,您正试图获得BusinessDashboard,这很确定它不会返回任何内容。 -
你实际期待什么结果,因为这看起来很奇怪
标签: php laravel database laravel-8