Quiero realizar un desplegable Único dependiendo de otro desplegable
tengo 3 tablas
areas
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | UNI | NULL | |
| code | int(11) | NO | UNI | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+------------+------------------+------+-----+---------+----------------+
categories
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | UNI | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+------------+------------------+------+-----+---------+----------------+
categyareas
+--------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| id_category | int(10) unsigned | YES | MUL | NULL | |
| id_area | int(10) unsigned | YES | MUL | NULL | |
| ans_cat | int(11) | YES | | NULL | |
| duration_cat | int(11) | YES | | NULL | |
| description | varchar(255) | YES | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+--------------+------------------+------+-----+---------+----------------+
Lo que quiero es que al seleccionar categoría(1) y el area(1), en siguiente, al seleccionar categoría(1) no muestre el area(1), ya que ya existe esa relacion
Investigue y realice este consulta
SELECT name
FROM areas
where id NOT IN (
SELECT DISTINCT id_area
FROM categoryareas WHERE
id_area IN (
SELECT id_area FROM categoryareas WHERE id_category = 1
)
)
la cual ejecuto y me da el resultado que quiero Link
En esta imagen si observo en la tabla principal ya existe una relacion con Categoría 2 y Area 5, al ingresar un nuevo registro me sigue apareciendo area 5 con categoría 2, el cual areas 5 ya no debería aparecer
en mi controlador tengo
<?php
namespace App\Http\Livewire\Categoryarea;
use Livewire\Component;
use App\Models\area\Area;
use App\Models\category\Category;
use App\Models\categoryarea\categoryarea;
class CreateCategoryArea extends Component
{
public $selecteArea = null, $selecteCategory = null;
public $areas, $categories, $ans_cat = null;
public $CategoryAreaStore = false, $results, $categoriaSel;
public function render()
{
return view('livewire.categoryarea.create-category-area');
}
public function mount()
{
$this->areas = Area::all();
$this->categories = Category::all();
}
function rules() {
return [
'selecteArea' => 'required',
'selecteCategory' => 'required',
'ans_cat' => '',
];
}
public function updated($propertyName)
{
$this->validateOnly($propertyName);
}
protected $messages = [
'selecteArea.required' => 'Selecione una Area',
'selecteCategory.required' => 'Selecione una Categoria',
];
public function save(){
$this->validate();
categoryarea::create([
'id_area'=> intVal($this->selecteArea),
'id_category'=> intVal($this->selecteCategory),
'ans_cat' => $this->ans_cat,
]);
$this->emit('CategoryAreaStore');
$this->resetErrorBag();
$this->resetValidation();
$this->reset(['selecteArea', 'selecteCategory', 'ans_cat']);
$this->emitTO( 'categoryarea.live-category-area-table','render');
$this->emit('alert', 'Relacion categorya por area creado sastifactoriamente');
}
public function cerrar(){
$this->reset(['selecteArea', 'selecteCategory','ans_cat']);
$this->emit('CategoryAreaStore');
$this->resetErrorBag();
$this->resetValidation();
}
}
y mi codigo esta esto
<div class="col-lg">
<div class="input-icon">
<span class="input-icon-addon">
<i class="fas fa-clone"></i>
</span>
<select class="form-control" wire:model="selecteCategory">
<option value="">{{ __('Select Category') }} *</option>
@foreach ($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
@endforeach
</select>
</div>
<x-jet-input-error for="selecteCategory" />
</div>
</div>
<br>
<div class="row row-sm">
<div class="col-lg">
<div class="input-icon">
<span class="input-icon-addon">
<i class="fas fa-clone"></i>
</span>
<select class="form-control" wire:model="selecteArea">
<option value="">{{ __('Select Area') }} *</option>
@foreach ($areas as $area)
<option value="{{ $area->id }}">{{ $area->name }}</option>
@endforeach
</select>
</div>
<x-jet-input-error for="selecteArea" />
</div>
</div>
Este es mi modelo categoryarea
<?php
namespace App\Models\categoryarea;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class categoryArea extends Model
{
use HasFactory;
protected $guarded = [];
public function area()
{
return $this->belongsTo('App\Models\area\Area' , 'id_area', 'id');
}
public function category()
{
return $this->belongsTo('App\Models\category\Category' , 'id_category', 'id');
}
}
Modelo area
<?php
namespace App\Models\area;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Area extends Model
{
use HasFactory;
protected $guarded = [];
public function functionaries()
{
return $this->hasMany('App\Models\functionary\Functionary' , 'area_id', 'id');
}
public function categoryareas()
{
return $this->hasMany('App\Models\categoryarea\categoryarea' , 'id_area', 'id');
}
public function activitycategoryareas()
{
return $this->hasMany('App\Models\activitycategoryarea\activitycategoryarea', 'id_area', 'id');
}
}
Modelo category
<?php
namespace App\Models\category;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
use HasFactory;
protected $guarded = [];
public function categoryarea()
{
return $this->hasMany('App\Models\categoryarea\Categoryarea' , 'id_category', 'id');
}
public function activitycategoryarea()
{
return $this->hasMany('App\Models\activitycategoryarea\activitycategoryarea', 'id_category', 'id');
}
}
Lo cual no se como realizar ese proceso.