Я храню значения адресов в таблице адресов, Модель адреса. Столбцы приведены ниже;
Код: Выделить всё
addressesСуществует также еще одна модель, называемая Type, она хранит тип все в проекте. Например, он хранит типы авторизации (Суперпользователь, Пользователь, Администратор и т. д.), красноречивые типы моделей, которые указаны в пути Models\ (App\Models\User, App\Models\Driver и т. д.), платежи. типы и т. д...
Думаю, вы поняли, о чем я вас спрошу, но давайте продолжим. Если разобраться, тип адресов я не задавал.
Код: Выделить всё
addressable_typeКод: Выделить всё
idКод: Выделить всё
addressable_type | 10 // this refers to `types`.`id`
addressable_id | 1
country | 225
state | 2175
city | 108155
address | NULL
status | 1
created_at | 2024-08-31 14:50:13
updated_at | 2024-08-31 14:50:13
id | 10
type | App\Models\Convoy // so this is record in the types table
label | 2
created_at | 2024-08-31 14:50:12
updated_at | 2024-08-31 14:50:12
Код: Выделить всё
class Convoy extends Model
{
use HasFactory;
protected $guarded = [];
public function address() : MorphOne
{
return $this->morphOne(Address::class, 'addressable');
}
}
Код: Выделить всё
class Address extends Model
{
use HasFactory;
protected $guarded = [];
public function addressable() : MorphTo
{
return $this->morphTo();
}
public function status() : BelongsTo
{
return $this->belongsTo(Status::class);
}
}
Код: Выделить всё
public function convoys() : View
{
$test = Convoy::with('address')->find(6);
dd($test);
return view('convoys', compact('convoys'));
}
Код: Выделить всё
App\Models\Convoy {#1228 ▼ // app/Http/Controllers/HomeController.php:50
...
#relations: array:1 [▼
"address" => null
]
...
}
Подробнее здесь: https://stackoverflow.com/questions/789 ... a-type-wit
Мобильная версия