diff --git a/app/Models/Building.php b/app/Models/Building.php index 2d705c1..d19da3b 100644 --- a/app/Models/Building.php +++ b/app/Models/Building.php @@ -19,4 +19,20 @@ class Building extends Model 'car_parking' => 'boolean', 'moto_parking' => 'boolean', ]; + + /** + * Get the categories for the building. + */ + public function categories() + { + return $this->hasMany(Category::class); + } + + /** + * Get the listings for the building. + */ + public function listings() + { + return $this->hasManyThrough(Listing::class, Category::class); + } } diff --git a/app/Models/Category.php b/app/Models/Category.php new file mode 100644 index 0000000..effed98 --- /dev/null +++ b/app/Models/Category.php @@ -0,0 +1,27 @@ +belongsTo(Building::class); + } + + /** + * Get the listing associated with the category. + */ + public function listing() + { + return $this->hasOne(Listing::class); + } +} diff --git a/app/Models/Listing.php b/app/Models/Listing.php new file mode 100644 index 0000000..a661350 --- /dev/null +++ b/app/Models/Listing.php @@ -0,0 +1,27 @@ +belongsTo(Category::class); + } + + /** + * Get the building that owns the listing. + */ + public function building() + { + return $this->hasOneThrough(Building::class, Category::class, 'id', 'id', 'category_id', 'building_id'); + } +} diff --git a/app/Nova/Building.php b/app/Nova/Building.php index e576444..5b69c88 100644 --- a/app/Nova/Building.php +++ b/app/Nova/Building.php @@ -5,6 +5,8 @@ use App\Nova\Actions\CreateProject; use Illuminate\Http\Request; use Laravel\Nova\Fields\Boolean; +use Laravel\Nova\Fields\HasMany; +use Laravel\Nova\Fields\HasManyThrough; use Laravel\Nova\Fields\ID; use Laravel\Nova\Http\Requests\NovaRequest; @@ -46,6 +48,8 @@ public function fields(NovaRequest $request) Boolean::make('Active'), Boolean::make('Car parking'), Boolean::make('Moto parking'), + HasManyThrough::make('Listings'), + HasMany::make('Categories'), ]; } diff --git a/app/Nova/Category.php b/app/Nova/Category.php new file mode 100644 index 0000000..0d7c6d7 --- /dev/null +++ b/app/Nova/Category.php @@ -0,0 +1,102 @@ + + */ + public static $model = \App\Models\Category::class; + + /** + * The single value that should be used to represent the resource when being displayed. + * + * @var string + */ + public static $title = 'id'; + + /** + * The columns that should be searched. + * + * @var array + */ + public static $search = [ + 'id', + ]; + + /** + * Get the fields displayed by the resource. + * + * @param \Laravel\Nova\Http\Requests\NovaRequest $request + * @return array + */ + public function fields(NovaRequest $request) + { + return [ + ID::make()->sortable(), + + BelongsTo::make('Building') + ->showWhenPeeking() + ->sortable() + ->filterable(), + + HasOne::make('Listing') + ->hideWhenCreating() + ->hideWhenUpdating() + ->sortable(), + ]; + } + + /** + * Get the cards available for the request. + * + * @param \Laravel\Nova\Http\Requests\NovaRequest $request + * @return array + */ + public function cards(NovaRequest $request) + { + return []; + } + + /** + * Get the filters available for the resource. + * + * @param \Laravel\Nova\Http\Requests\NovaRequest $request + * @return array + */ + public function filters(NovaRequest $request) + { + return []; + } + + /** + * Get the lenses available for the resource. + * + * @param \Laravel\Nova\Http\Requests\NovaRequest $request + * @return array + */ + public function lenses(NovaRequest $request) + { + return []; + } + + /** + * Get the actions available for the resource. + * + * @param \Laravel\Nova\Http\Requests\NovaRequest $request + * @return array + */ + public function actions(NovaRequest $request) + { + return []; + } +} diff --git a/app/Nova/Listing.php b/app/Nova/Listing.php new file mode 100644 index 0000000..04475e9 --- /dev/null +++ b/app/Nova/Listing.php @@ -0,0 +1,101 @@ + + */ + public static $model = \App\Models\Listing::class; + + /** + * The single value that should be used to represent the resource when being displayed. + * + * @var string + */ + public static $title = 'id'; + + /** + * The columns that should be searched. + * + * @var array + */ + public static $search = [ + 'id', + ]; + + /** + * Get the fields displayed by the resource. + * + * @param \Laravel\Nova\Http\Requests\NovaRequest $request + * @return array + */ + public function fields(NovaRequest $request) + { + return [ + ID::make()->sortable(), + + BelongsTo::make('Category') + ->showWhenPeeking() + ->sortable() + ->filterable(), + + HasOneThrough::make('Building') + ->sortable() + ->exceptOnForms(), + ]; + } + + /** + * Get the cards available for the request. + * + * @param \Laravel\Nova\Http\Requests\NovaRequest $request + * @return array + */ + public function cards(NovaRequest $request) + { + return []; + } + + /** + * Get the filters available for the resource. + * + * @param \Laravel\Nova\Http\Requests\NovaRequest $request + * @return array + */ + public function filters(NovaRequest $request) + { + return []; + } + + /** + * Get the lenses available for the resource. + * + * @param \Laravel\Nova\Http\Requests\NovaRequest $request + * @return array + */ + public function lenses(NovaRequest $request) + { + return []; + } + + /** + * Get the actions available for the resource. + * + * @param \Laravel\Nova\Http\Requests\NovaRequest $request + * @return array + */ + public function actions(NovaRequest $request) + { + return []; + } +} diff --git a/database/factories/CategoryFactory.php b/database/factories/CategoryFactory.php new file mode 100644 index 0000000..edcb281 --- /dev/null +++ b/database/factories/CategoryFactory.php @@ -0,0 +1,23 @@ + + */ +class CategoryFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition() + { + return [ + // + ]; + } +} diff --git a/database/factories/ListingFactory.php b/database/factories/ListingFactory.php new file mode 100644 index 0000000..181d0cc --- /dev/null +++ b/database/factories/ListingFactory.php @@ -0,0 +1,23 @@ + + */ +class ListingFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition() + { + return [ + // + ]; + } +} diff --git a/database/migrations/2023_01_17_100744_create_categories_table.php b/database/migrations/2023_01_17_100744_create_categories_table.php new file mode 100644 index 0000000..47b2e5c --- /dev/null +++ b/database/migrations/2023_01_17_100744_create_categories_table.php @@ -0,0 +1,32 @@ +id(); + $table->foreignId('building_id')->constrained(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('categories'); + } +}; diff --git a/database/migrations/2023_01_17_100754_create_listings_table.php b/database/migrations/2023_01_17_100754_create_listings_table.php new file mode 100644 index 0000000..d9f23a0 --- /dev/null +++ b/database/migrations/2023_01_17_100754_create_listings_table.php @@ -0,0 +1,32 @@ +id(); + $table->foreignId('category_id')->constrained(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('listings'); + } +};