Skip to content

Commit

Permalink
Categories and Listings
Browse files Browse the repository at this point in the history
  • Loading branch information
chimit committed Jan 17, 2023
1 parent 7871354 commit 83e6a49
Show file tree
Hide file tree
Showing 10 changed files with 387 additions and 0 deletions.
16 changes: 16 additions & 0 deletions app/Models/Building.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
27 changes: 27 additions & 0 deletions app/Models/Category.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Models;

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

class Category extends Model
{
use HasFactory;

/**
* Get the building that owns the category.
*/
public function building()
{
return $this->belongsTo(Building::class);
}

/**
* Get the listing associated with the category.
*/
public function listing()
{
return $this->hasOne(Listing::class);
}
}
27 changes: 27 additions & 0 deletions app/Models/Listing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Models;

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

class Listing extends Model
{
use HasFactory;

/**
* Get the category for the listing.
*/
public function category()
{
return $this->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');
}
}
4 changes: 4 additions & 0 deletions app/Nova/Building.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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'),
];
}

Expand Down
102 changes: 102 additions & 0 deletions app/Nova/Category.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace App\Nova;

use Illuminate\Http\Request;
use Laravel\Nova\Fields\BelongsTo;
use Laravel\Nova\Fields\HasOne;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Http\Requests\NovaRequest;

class Category extends Resource
{
/**
* The model the resource corresponds to.
*
* @var class-string<\App\Models\Category>
*/
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 [];
}
}
101 changes: 101 additions & 0 deletions app/Nova/Listing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace App\Nova;

use Illuminate\Http\Request;
use Laravel\Nova\Fields\BelongsTo;
use Laravel\Nova\Fields\HasOneThrough;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Http\Requests\NovaRequest;

class Listing extends Resource
{
/**
* The model the resource corresponds to.
*
* @var class-string<\App\Models\Listing>
*/
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 [];
}
}
23 changes: 23 additions & 0 deletions database/factories/CategoryFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Category>
*/
class CategoryFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
//
];
}
}
23 changes: 23 additions & 0 deletions database/factories/ListingFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Listing>
*/
class ListingFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
//
];
}
}
32 changes: 32 additions & 0 deletions database/migrations/2023_01_17_100744_create_categories_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

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

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->foreignId('building_id')->constrained();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('categories');
}
};
Loading

0 comments on commit 83e6a49

Please sign in to comment.