Skip to content

Commit

Permalink
Add dynamic menu and site settings
Browse files Browse the repository at this point in the history
  • Loading branch information
curtisdelicata committed Jul 29, 2024
1 parent 48c07b2 commit 9fa830b
Show file tree
Hide file tree
Showing 12 changed files with 277 additions and 0 deletions.
11 changes: 11 additions & 0 deletions app/Filament/Admin/Resources/MenuResource/Pages/CreateMenu.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Filament\Admin\Resources\MenuResource\Pages;

use Filament\Resources\Pages\CreateRecord;
use App\Filament\Admin\Resources\MenuResource;

class CreateMenu extends CreateRecord
{
protected static string $resource = MenuResource::class;
}
11 changes: 11 additions & 0 deletions app/Filament/Admin/Resources/MenuResource/Pages/EditMenu.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Filament\Admin\Resources\MenuResource\Pages;

use Filament\Resources\Pages\EditRecord;
use App\Filament\Admin\Resources\MenuResource;

class EditMenu extends EditRecord
{
protected static string $resource = MenuResource::class;
}
11 changes: 11 additions & 0 deletions app/Filament/Admin/Resources/MenuResource/Pages/ListMenus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Filament\Admin\Resources\MenuResource\Pages;

use Filament\Resources\Pages\ListRecords;
use App\Filament\Admin\Resources\MenuResource;

class ListMenus extends ListRecords
{
protected static string $resource = MenuResource::class;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Filament\Admin\Resources\SiteSettingsResource\Pages;

use App\Filament\Admin\Resources\SiteSettingsResource;
use Filament\Resources\Pages\CreateRecord;

class CreateSiteSettings extends CreateRecord
{
protected static string $resource = SiteSettingsResource::class;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Filament\Admin\Resources\SiteSettingsResource\Pages;

use App\Filament\Admin\Resources\SiteSettingsResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;

class EditSiteSettings extends EditRecord
{
protected static string $resource = SiteSettingsResource::class;

protected function getHeaderActions(): array
{
return [
Actions\DeleteAction::make(),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Filament\Admin\Resources\SiteSettingsResource\Pages;

use App\Filament\Admin\Resources\SiteSettingsResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;

class ListSiteSettings extends ListRecords
{
protected static string $resource = SiteSettingsResource::class;

protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}
28 changes: 28 additions & 0 deletions app/Models/Menu.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Menu extends Model
{
use HasFactory, SoftDeletes;

protected $fillable = [
'name',
'url',
'parent_id',
'order',
];

public function parent()
{
return $this->belongsTo(Menu::class, 'parent_id');
}

public function children()
{
return $this->hasMany(Menu::class, 'parent_id');
}
}
20 changes: 20 additions & 0 deletions database/factories/MenuFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Database\Factories;

use App\Models\Menu;
use Illuminate\Database\Eloquent\Factories\Factory;

class MenuFactory extends Factory
{
protected $model = Menu::class;

public function definition()
{
return [
'name' => $this->faker->word,
'url' => $this->faker->url,
'order' => $this->faker->numberBetween(1, 10),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

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

return new class extends Migration
{
public function up()
{
Schema::create('site_settings', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('currency');
$table->string('default_language');
$table->text('address');
$table->string('country');
$table->string('email');
$table->timestamps();
});
}

public function down()
{
Schema::dropIfExists('site_settings');
}
};
28 changes: 28 additions & 0 deletions database/migrations/2024_07_24_080000_create_menus_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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('menus', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('url');
$table->unsignedBigInteger('parent_id')->nullable();
$table->integer('order')->default(0);
$table->timestamps();
$table->softDeletes();

$table->foreign('parent_id')->references('id')->on('menus')->onDelete('cascade');
});
}

public function down(): void
{
Schema::dropIfExists('menus');
}
};
71 changes: 71 additions & 0 deletions database/seeders/MenuSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Menu;

class MenuSeeder extends Seeder
{
public function run()
{
$menus = [
[
'name' => 'Home',
'url' => '/',
'order' => 1
],
[
'name' => 'Properties',
'url' => '/properties',
'order' => 2,
'children' => [
['name' => 'For Sale', 'url' => '/properties/for-sale', 'order' => 1],
['name' => 'For Rent', 'url' => '/properties/for-rent', 'order' => 2],
]
],
[
'name' => 'Services',
'url' => '/services',
'order' => 3,
'children' => [
['name' => 'Buying', 'url' => '/services/buying', 'order' => 1],
['name' => 'Selling', 'url' => '/services/selling', 'order' => 2],
['name' => 'Renting', 'url' => '/services/renting', 'order' => 3],
]
],
[
'name' => 'About',
'url' => '/about',
'order' => 4
],
[
'name' => 'Contact',
'url' => '/contact',
'order' => 5
],
[
'name' => 'Calculators',
'url' => '/calculators',
'order' => 6
],
];

foreach ($menus as $menuData) {
$this->createMenu($menuData);
}
}

private function createMenu($menuData, $parentId = null)
{
$children = $menuData['children'] ?? [];
unset($menuData['children']);

$menuData['parent_id'] = $parentId;
$menu = Menu::create($menuData);

foreach ($children as $childData) {
$this->createMenu($childData, $menu->id);
}
}
}
21 changes: 21 additions & 0 deletions database/seeders/SiteSettingsSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\SiteSettings;

class SiteSettingsSeeder extends Seeder
{
public function run()
{
SiteSettings::create([
'name' => config('app.name', 'Liberu Real Estate'),
'currency' => '£',
'default_language' => 'en',
'address' => '123 Real Estate St, London, UK',
'country' => 'United Kingdom',
'email' => '[email protected]',
]);
}
}

0 comments on commit 9fa830b

Please sign in to comment.