[英]Get reported users with Eloquent
在我的 laravel 應用程序中,我想獲取其他用戶報告的用戶,並且對於每個用戶,我想檢索附加到他們的其他用戶的這些報告,但我不知道如何。
報告 model:
public function reporter()
{
return $this->belongsTo('App\Models\User', 'reporter_id');
}
public function reported()
{
return $this->belongsTo('App\Models\User', 'reported_id');
}
用戶 model:
public function reports()
{
return $this->hasMany('App\Models\Report','reporter_id');
}
public function reported()
{
return $this->hasMany('App\Models\Report','reported_id');
}
我如何創建報告:
public function create($request)
{
$validatedData = $request->validate(ReportValidator::$create);
$report = new Report();
$report->reportcategory_id = $request['reportcategory_id'];
$report->reporter_id = $request['reporter_id'];
$report->reported_id = $request['reported_id'];
$report->body = $request['body'];
$report->save();
$report = $report->fresh([ 'category' ]);
return $report;
}
報告遷移:
Schema::create('reports', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('reporter_id')->index();
$table->foreign('reporter_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->unsignedInteger('reported_id')->index();
$table->foreign('reported_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->unsignedInteger('reportcategory_id')->index();
$table->foreign('reportcategory_id')->references('id')->on('reportcategories')->onDelete('cascade')->onUpdate('cascade');
$table->text('body')->nullable();
$table->timestamps();
});
您需要獲取用戶並加載這樣的關系
$user = User::has('reported')->with('reported')->get();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.