rework components

This commit is contained in:
TimmensOne 2023-03-29 22:32:49 +02:00
parent 7a8e277887
commit 656056a3b3
17 changed files with 217 additions and 33 deletions

View File

@ -35,3 +35,6 @@ sail exec laravel.test php artisan migrate:refresh --seed
## ChatGPT ## ChatGPT
see ChatGPT folder see ChatGPT folder
#### Challenges
Convention, due to given database. Could be easier

View File

@ -7,11 +7,19 @@
use App\Models\PurchasingInformation; use App\Models\PurchasingInformation;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
class Device extends Model class Device extends Model
{ {
use HasFactory; use HasFactory;
/**
* The table associated with the model.
* @var string
*/
protected $table = 'devices';
/** /**
* The primary key associated with the devices table. * The primary key associated with the devices table.
* @var string * @var string
@ -25,18 +33,20 @@ class Device extends Model
*/ */
protected $fillable = ['device_id', 'title', 'device_type', 'description', 'accessories', 'rz_username_buyer', 'serial_number', 'image_url']; protected $fillable = ['device_id', 'title', 'device_type', 'description', 'accessories', 'rz_username_buyer', 'serial_number', 'image_url'];
//The data type of the auto-incrementing ID.
protected $keyType = 'string';
//Timestamps are disabled. //Timestamps are disabled.
public $timestamps = false; public $timestamps = false;
public function owners() { public function owners(): HasMany {
return $this->hasMany(OwnerTransaction::class, 'device_id'); return $this->hasMany(OwnerTransaction::class, 'device_id', 'device_id');
} }
public function locations() { public function locations(): HasMany {
return $this->hasMany(LocationTransaction::class, 'device_id'); return $this->hasMany(LocationTransaction::class, 'device_id', 'device_id');
} }
public function purchasing() { public function purchasing(): HasOne {
return $this->belongsTo(PurchasingInformation::class, 'device_id'); return $this->hasOne(PurchasingInformation::class, 'device_id', 'device_id');
} }
} }

View File

@ -5,11 +5,18 @@
use App\Models\Device; use App\Models\Device;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class LocationTransaction extends Model class LocationTransaction extends Model
{ {
use HasFactory; use HasFactory;
/**
* The table associated with the model.
* @var string
*/
protected $table = 'location_transactions';
/** /**
* The primary key associated with the devices table. * The primary key associated with the devices table.
* @var string * @var string
@ -23,7 +30,10 @@ class LocationTransaction extends Model
*/ */
protected $fillable = ['location_transaction_id', 'room_code', 'timestamp_located_since', 'device_id']; protected $fillable = ['location_transaction_id', 'room_code', 'timestamp_located_since', 'device_id'];
public function device() { public function device(): BelongsTo {
return $this->belongsTo(Device::class, 'device_id'); return $this->belongsTo(Device::class, 'device_id', 'device_id');
} }
//Timestamps are disabled.
public $timestamps = false;
} }

View File

@ -5,11 +5,18 @@
use App\Models\Device; use App\Models\Device;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class OwnerTransaction extends Model class OwnerTransaction extends Model
{ {
use HasFactory; use HasFactory;
/**
* The table associated with the model.
* @var string
*/
protected $table = 'owner_transactions';
/** /**
* The primary key associated with the devices table. * The primary key associated with the devices table.
* @var string * @var string
@ -23,8 +30,10 @@ class OwnerTransaction extends Model
*/ */
protected $fillable = ['owner_transaction_id', 'rz_username', 'timestamp_owner_since', 'device_id']; protected $fillable = ['owner_transaction_id', 'rz_username', 'timestamp_owner_since', 'device_id'];
public function device() { public function device(): BelongsTo {
return $this->belongsTo(Device::class, 'device_id'); return $this->belongsTo(Device::class, 'device_id', 'device_id');
} }
//Timestamps are disabled.
public $timestamps = false;
} }

View File

@ -4,11 +4,18 @@
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class PurchasingInformation extends Model class PurchasingInformation extends Model
{ {
use HasFactory; use HasFactory;
/**
* The table associated with the model.
* @var string
*/
protected $table = 'purchasing_information';
/** /**
* The primary key associated with the devices table. * The primary key associated with the devices table.
* @var string * @var string
@ -22,7 +29,10 @@ class PurchasingInformation extends Model
*/ */
protected $fillable = ['purchasing_information_id', 'price', 'timestamp_warranty_end', 'timestamp_purchase', 'cost_centre', 'seller', 'device_id']; protected $fillable = ['purchasing_information_id', 'price', 'timestamp_warranty_end', 'timestamp_purchase', 'cost_centre', 'seller', 'device_id'];
public function device() { public function device(): BelongsTo {
return $this->belongsTo(Device::class, 'device_id'); return $this->belongsTo(Device::class, 'device_id', 'device_id');
} }
//Timestamps are disabled.
public $timestamps = false;
} }

View File

@ -12,6 +12,12 @@ class User extends Authenticatable
{ {
use HasApiTokens, HasFactory, Notifiable; use HasApiTokens, HasFactory, Notifiable;
/**
* The table associated with the model.
* @var string
*/
protected $table = 'users';
/** /**
* The primary key associated with the users table. * The primary key associated with the users table.
* @var string * @var string
@ -31,6 +37,8 @@ class User extends Authenticatable
'hashed_password' 'hashed_password'
]; ];
//ID is not auto-incrementing.
public $incrementing = false;
//Timestamps are disabled. //Timestamps are disabled.
public $timestamps = false; public $timestamps = false;

View File

@ -0,0 +1,29 @@
<?php
namespace App\View\Components;
use App\Models\LocationTransaction as ModelsLocationTransaction;
use Closure;
use Illuminate\View\Component;
use Illuminate\Contracts\View\View;
class LocationTransaction extends Component
{
/**
* Create a new component instance.
*/
public function __construct(
public ModelsLocationTransaction $location
)
{
//
}
/**
* Get the view / contents that represent the component.
*/
public function render(): View|Closure|string
{
return view('components.location-transaction');
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace App\View\Components;
use App\Models\OwnerTransaction as ModelsOwnerTransaction;
use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;
class OwnerTransaction extends Component
{
/**
* Create a new component instance.
*/
public function __construct(
public ModelsOwnerTransaction $owner
)
{
//
}
/**
* Get the view / contents that represent the component.
*/
public function render(): View|Closure|string
{
return view('components.owner-transaction');
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace App\View\Components;
use App\Models\Device;
use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;
class PurchasingInformation extends Component
{
/**
* Create a new component instance.
*/
public function __construct(
public Device $device
)
{}
/**
* Get the view / contents that represent the component.
*/
public function render(): View|Closure|string
{
return view('components.purchasing-information');
}
}

View File

@ -7,6 +7,9 @@
use App\Models\Device; use App\Models\Device;
use App\Models\User; use App\Models\User;
use Illuminate\Database\Seeder; use Illuminate\Database\Seeder;
use App\Models\OwnerTransaction;
use App\Models\LocationTransaction;
use App\Models\PurchasingInformation;
class DatabaseSeeder extends Seeder class DatabaseSeeder extends Seeder
{ {
@ -22,7 +25,9 @@ public function run(): void
// 'email' => 'test@example.com', // 'email' => 'test@example.com',
// ]); // ]);
Device::factory(2)->create(); Device::factory(1)->create([
'device_id' => '1'
]);
User::create([ User::create([
@ -40,5 +45,29 @@ public function run(): void
'has_admin_privileges' => false, 'has_admin_privileges' => false,
'hashed_password' => bcrypt('test123') 'hashed_password' => bcrypt('test123')
]); ]);
PurchasingInformation::create([
'purchasing_information_id' => '1',
'price' => '1',
'timestamp_warranty_end' => '1',
'timestamp_purchase' => '1',
'cost_centre' => '1',
'seller' => '1',
'device_id' => '1',
]);
LocationTransaction::create([
'location_transaction_id' => '1',
'room_code' => '1',
'timestamp_located_since' => '1',
'device_id' => '1',
]);
OwnerTransaction::create([
'owner_transaction_id' => '1',
'rz_username' => '1',
'timestamp_owner_since' => '1',
'device_id' => '1',
]);
} }
} }

View File

@ -11,4 +11,15 @@
<li>{{ $device['serial_number'] }}</li> <li>{{ $device['serial_number'] }}</li>
<li>{{ $device['image_url'] }}</li> <li>{{ $device['image_url'] }}</li>
</ul> </ul>
<x-purchasing-information :device="$device" />
@php
$locationTransactions = $device->locations;
$ownerTransactins = $device->owners;
@endphp
@foreach ($locationTransactions as $location)
<x-location-transaction :location="$location" />
@endforeach
@foreach ($ownerTransactins as $owner)
<x-owner-transaction :owner="$owner" />
@endforeach
</div> </div>

View File

@ -1,5 +0,0 @@
@props(['locationTransaction'])
<div>
<p>Owner: {{ $locationTransaction->room_code }}</p>
<p>since: {{ $locationTransaction->timestamp_located_since }}</p>
</div>

View File

@ -1,5 +0,0 @@
@props(['ownerTransaction'])
<div>
<p>Owner: {{ $ownerTransaction->rz_username }}</p>
<p>since: {{ $ownerTransaction->timestamp_owner_since }}</p>
</div>

View File

@ -1,8 +0,0 @@
@props(['purchasingInformation'])
<div>
<p>price: {{ $purchasingInformation->price }}</p>
<p>timestamp_warranty_end: {{ $purchasingInformation->timestamp_warranty_end }}</p>
<p>timestamp_purchase: {{ $purchasingInformation->timestamp_purchase }}</p>
<p>cost_centre: {{ $purchasingInformation->cost_centre }}</p>
<p>seller: {{ $purchasingInformation->seller }}</p>
</div>

View File

@ -0,0 +1,7 @@
<div>
<h3>Location Transactions</h3>
<ul>
<li>room_code: {{ $location->room_code }}</li>
<li>timestamp_located_since: {{ $location->timestamp_located_since }}</li>
</ul>
</div>

View File

@ -0,0 +1,7 @@
<div>
<h3>Owner Transactions</h3>
<ul>
<li>rz_username: {{ $owner->rz_username }}</li>
<li>timestamp_owner_since: {{ $owner->timestamp_owner_since }}</li>
</ul>
</div>

View File

@ -0,0 +1,13 @@
<div>
@php
$purchasing = $device->purchasing;
@endphp
<h3>Purchasing Information</h3>
<ul>
<li>price: {{ $purchasing->price }}</li>
<li>timestamp_warranty_end: {{ $purchasing->timestamp_warranty_end }}</li>
<li>timestamp_purchase: {{ $purchasing->timestamp_purchase }}</li>
<li>cost_centre: {{ $purchasing->cost_centre }}</li>
<li>seller: {{ $purchasing->seller }}</li>
</ul>
</div>