Skip to content

Commit

Permalink
Add photos to items and start the relations
Browse files Browse the repository at this point in the history
  • Loading branch information
ysbrandB committed May 31, 2024
1 parent a1aed41 commit b8d0cbd
Show file tree
Hide file tree
Showing 15 changed files with 259 additions and 34 deletions.
20 changes: 19 additions & 1 deletion app/Http/Controllers/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Http\Requests\StoreItemRequest;
use App\Http\Requests\UpdateItemRequest;
use App\Models\Item;
use Illuminate\Support\Facades\Storage;
use Inertia\Inertia;
use Vinkla\Hashids\Facades\Hashids;

Expand All @@ -18,6 +19,7 @@ public function index()
return Inertia::render('Items/Index', [
'items' => Item::all()->each(function ($item) {
$item->hashid = $item->public_id;
$item->photo_url = asset('storage/photos/' . $item->photo);
}),
]);
}
Expand All @@ -35,9 +37,12 @@ public function create()
*/
public function store(StoreItemRequest $request)
{
$photo = $request->file('photo');
$photo->storeAs( 'photos/'.$photo->hashName(), ['disk' => 'public']);
Item::create([
'name' => $request->name,
'description' => $request->description,
'photo' => $photo->hashName(),
]);

return redirect(route('items.index'));
Expand Down Expand Up @@ -72,10 +77,19 @@ public function edit(Int $id)
public function update(StoreItemRequest $request, int $id)
{
$item = Item::findOrFail($id);
$photo = $request->file('photo');
$photo->storeAs( 'photos/'.$photo->hashName(), ['disk' => 'public']);

if($item->photo){
//delete the old photo
Storage::disk('public')->delete('photos/' . $item->photo);
}
$item->update([
'name' => $request->name,
'description' => $request->description,
'photo' => $photo->hashName(),
]);
$item->save();
return redirect(route('items.index'));
}

Expand All @@ -84,6 +98,10 @@ public function update(StoreItemRequest $request, int $id)
*/
public function destroy(Item $item)
{
//
if($item->photo){
//delete the old photo
Storage::disk('public')->delete('photos/' . $item->photo);
}
$item->delete();
}
}
4 changes: 2 additions & 2 deletions app/Http/Requests/StoreItemRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public function rules(): array
{
return [
//name and description are required
'name' => ['required'],
'description' => ['required'],
'name' => ['required', 'string'],
'description' => ['required', 'string'],
];
}
}
18 changes: 18 additions & 0 deletions app/Models/Attribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* @property string name
* @property string description
*/
class Attribute extends Model
{
use HasFactory;
public function items()
{
return $this->belongsToMany(Item::class, 'attribute_items');
}
}
30 changes: 30 additions & 0 deletions app/Models/Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
* @property string title
* @property HasMany item
* @property BelongsToMany attributes
*/
class Controller extends Model
{
use HasFactory;

protected $fillable = ['title'];
public function item(): BelongsTo
{
return $this->belongsTo(HasMany::class, 'item_id');
}

public function attributes(): BelongsToMany
{
return $this->belongsToMany(Attribute::class, 'attribute_controllers');
}
}
19 changes: 14 additions & 5 deletions app/Models/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,29 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
* @property string name
* @property string description
* @property string public_id
* @property boolean is_actuator
* @property string photo
*
*/

class Item extends Model
{
use HasFactory;

/**
* @var \Illuminate\Support\HigherOrderCollectionProxy|mixed
*/
protected $fillable = [
'name',
'description',
'photo',
'is_actuator'
];

public function resolveRouteBinding($value, $field = null)
{
return $this::findOrFail(Hashids::decode($value));
}

public function getPublicIdAttribute(): string
{
return Hashids::encode($this->id);
Expand Down
5 changes: 5 additions & 0 deletions config/filesystems.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@

'disks' => [

'uploads' => [
'driver' => 'local',
'root' => storage_path().'/files/uploads',
],

'local' => [
'driver' => 'local',
'root' => storage_path('app'),
Expand Down
22 changes: 22 additions & 0 deletions database/factories/AttributeFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Database\Factories;

use App\Models\Attribute;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Carbon;

class AttributeFactory extends Factory
{
protected $model = Attribute::class;

public function definition(): array
{
return [
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
'name' => $this->faker->name(),
'description' => $this->faker->text(),
];
}
}
22 changes: 22 additions & 0 deletions database/factories/ControllerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Database\Factories;

use App\Models\Controller;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Carbon;

class ControllerFactory extends Factory
{
protected $model = Controller::class;

public function definition(): array
{
return [
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
'Name' => $this->faker->name(),
'item_id' => $this->faker->randomNumber(),
];
}
}
2 changes: 2 additions & 0 deletions database/migrations/2024_05_26_204123_create_items_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ public function up(): void
Schema::create('items', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('photo');
$table->text('description')->nullable();
$table->boolean('is_actuator')->default(false);
$table->timestamps();
});
}
Expand Down
23 changes: 23 additions & 0 deletions database/migrations/2024_05_29_081709_create_controllers_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
public function up(): void
{
Schema::create('controllers', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->text('description');
$table->foreignId('item_id');
$table->timestamps();
});
}

public function down(): void
{
Schema::dropIfExists('controllers');
}
};
40 changes: 40 additions & 0 deletions database/migrations/2024_05_29_082156_create_attributes_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
public function up(): void
{
Schema::create('attributes', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('description');
$table->timestamps();
});

//add new pivot table called attribute_items
Schema::create('attribute_items', function (Blueprint $table) {
$table->id();
$table->foreignId('attribute_id');
$table->foreignId('item_id');
$table->timestamps();
});

//add a new pivot table called attribute_controllers
Schema::create('attribute_controllers', function (Blueprint $table) {
$table->id();
$table->foreignId('attribute_id');
$table->foreignId('controller_id');
$table->timestamps();
});
}

public function down(): void
{
Schema::dropIfExists('attributes');
Schema::dropIfExists('attribute_items');
Schema::dropIfExists('attribute_controllers');
}
};
7 changes: 7 additions & 0 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Database\Seeders;

use App\Models\Item;
use App\Models\User;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
Expand All @@ -19,5 +20,11 @@ public function run(): void
'name' => 'Test User',
'email' => '[email protected]',
]);

Item::create([
'name' => 'Item 1',
'description' => 'This is item 1',
'is_actuator' => false,
]);
}
}
9 changes: 9 additions & 0 deletions resources/js/Pages/Items/Create.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Head, useForm } from '@inertiajs/vue3';
const form = useForm({
name: '',
description: '',
image: '',
});
const submit = () => {
Expand Down Expand Up @@ -40,6 +41,14 @@ const submit = () => {
<InputError class="mt-2" :message="form.errors.name" />
</div>

<div class="mt-4">
<InputLabel for="image" value="Image" />

<input v-model="form.image" type="image" alt="image">

<InputError class="mt-2" :message="form.errors.image" />
</div>

<div class="mt-4">
<InputLabel for="description" value="Description" />

Expand Down
Loading

0 comments on commit b8d0cbd

Please sign in to comment.