Add Tenant model, controller, and migration; update Producto model to reference tenant_codigo
This commit is contained in:
@@ -51,7 +51,7 @@ class ProductController extends Controller
|
|||||||
protected function rules(?int $productoId = null): array
|
protected function rules(?int $productoId = null): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'tenant_id' => ['required', 'integer'],
|
'tenant_codigo' => ['required', 'string', 'exists:tenants,codigo'],
|
||||||
'categoria_id' => ['required', 'integer'],
|
'categoria_id' => ['required', 'integer'],
|
||||||
'slug' => [
|
'slug' => [
|
||||||
'required',
|
'required',
|
||||||
|
|||||||
64
app/Http/Controllers/TenantController.php
Normal file
64
app/Http/Controllers/TenantController.php
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Models\Tenant;
|
||||||
|
use Illuminate\Http\JsonResponse;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Http\Response;
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
|
||||||
|
class TenantController extends Controller
|
||||||
|
{
|
||||||
|
public function index(): JsonResponse
|
||||||
|
{
|
||||||
|
return response()->json(Tenant::query()->latest()->get());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function store(Request $request): JsonResponse
|
||||||
|
{
|
||||||
|
$validated = $request->validate($this->rules());
|
||||||
|
|
||||||
|
$tenant = Tenant::create($validated);
|
||||||
|
|
||||||
|
return response()->json($tenant, 201);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function show(Tenant $tenant): JsonResponse
|
||||||
|
{
|
||||||
|
return response()->json($tenant);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function update(Request $request, Tenant $tenant): JsonResponse
|
||||||
|
{
|
||||||
|
$validated = $request->validate($this->rules($tenant->id));
|
||||||
|
|
||||||
|
$tenant->update($validated);
|
||||||
|
|
||||||
|
return response()->json($tenant);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function destroy(Tenant $tenant): Response
|
||||||
|
{
|
||||||
|
$tenant->delete();
|
||||||
|
|
||||||
|
return response()->noContent();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
protected function rules(?int $tenantId = null): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'codigo' => [
|
||||||
|
'required',
|
||||||
|
'string',
|
||||||
|
'max:255',
|
||||||
|
Rule::unique('tenants', 'codigo')->ignore($tenantId),
|
||||||
|
],
|
||||||
|
'nombre' => ['required', 'string', 'max:255'],
|
||||||
|
'dominio' => ['nullable', 'string', 'max:255'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,9 +5,10 @@ namespace App\Models;
|
|||||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||||
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;
|
||||||
|
|
||||||
#[Fillable([
|
#[Fillable([
|
||||||
'tenant_id',
|
'tenant_codigo',
|
||||||
'categoria_id',
|
'categoria_id',
|
||||||
'slug',
|
'slug',
|
||||||
'nombre',
|
'nombre',
|
||||||
@@ -23,9 +24,16 @@ class Producto extends Model
|
|||||||
protected function casts(): array
|
protected function casts(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'tenant_id' => 'integer',
|
|
||||||
'categoria_id' => 'integer',
|
'categoria_id' => 'integer',
|
||||||
'precio' => 'decimal:2',
|
'precio' => 'decimal:2',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return BelongsTo<Tenant, $this>
|
||||||
|
*/
|
||||||
|
public function tenant(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Tenant::class, 'tenant_codigo', 'codigo');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
26
app/Models/Tenant.php
Normal file
26
app/Models/Tenant.php
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
|
|
||||||
|
#[Fillable([
|
||||||
|
'codigo',
|
||||||
|
'nombre',
|
||||||
|
'dominio',
|
||||||
|
])]
|
||||||
|
class Tenant extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return HasMany<Producto, $this>
|
||||||
|
*/
|
||||||
|
public function productos(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(Producto::class, 'tenant_codigo', 'codigo');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('tenants', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->string('codigo')->unique();
|
||||||
|
$table->string('nombre');
|
||||||
|
$table->string('dominio')->nullable();
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('tenants');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::table('productos', function (Blueprint $table) {
|
||||||
|
$table->dropColumn('tenant_id');
|
||||||
|
});
|
||||||
|
|
||||||
|
Schema::table('productos', function (Blueprint $table) {
|
||||||
|
$table->string('tenant_codigo')->after('id');
|
||||||
|
$table->foreign('tenant_codigo')->references('codigo')->on('tenants')->cascadeOnUpdate()->restrictOnDelete();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::table('productos', function (Blueprint $table) {
|
||||||
|
$table->dropForeign(['tenant_codigo']);
|
||||||
|
$table->dropColumn('tenant_codigo');
|
||||||
|
});
|
||||||
|
|
||||||
|
Schema::table('productos', function (Blueprint $table) {
|
||||||
|
$table->unsignedBigInteger('tenant_id')->after('id');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use App\Http\Controllers\ProductController;
|
use App\Http\Controllers\ProductController;
|
||||||
|
use App\Http\Controllers\TenantController;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
|
|
||||||
@@ -9,3 +10,4 @@ Route::get('/user', function (Request $request) {
|
|||||||
})->middleware('auth:sanctum');
|
})->middleware('auth:sanctum');
|
||||||
|
|
||||||
Route::apiResource('productos', ProductController::class);
|
Route::apiResource('productos', ProductController::class);
|
||||||
|
Route::apiResource('tenants', TenantController::class);
|
||||||
|
|||||||
Reference in New Issue
Block a user