42 lines
948 B
PHP
42 lines
948 B
PHP
<?php
|
||
|
||
namespace App\Models;
|
||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||
use Illuminate\Database\Eloquent\Model;
|
||
|
||
class PcBuild extends Model
|
||
{
|
||
use HasFactory;
|
||
|
||
protected $fillable = [
|
||
|
||
'name',
|
||
'description',
|
||
'user_id',
|
||
|
||
|
||
];
|
||
|
||
// 👇 Связь "многие-ко-многим" с компонентами
|
||
public function components()
|
||
{
|
||
return $this->belongsToMany(Component::class, 'pc_build_components');
|
||
}
|
||
|
||
// Обратная связь
|
||
public function user()
|
||
{
|
||
return $this->belongsTo(User::class);
|
||
}
|
||
|
||
// Опционально: защита от ошибок, если сборка без пользователя
|
||
protected static function booted()
|
||
{
|
||
static::addGlobalScope('user', function ($query) {
|
||
if (auth()->check()) {
|
||
$query->where('user_id', auth()->id());
|
||
}
|
||
});
|
||
}
|
||
} |