actualmente me encuentro haciendo un CRUD con registros relacionados en Laravel 8 usando Livewire. La relacion es hasMany y no puedo editarlos o agregarlos usando la relacion. Pero cuando en otros modelos con relacion hasOne no tuve ningun problema en hacerlo.
Modelo License:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\Personal;
use App\Models\Renovation;
class License extends Model
{
use HasFactory;
protected $table = 'licenses';
protected $fillable = ['id','personal_id', 'resolution', 'identification', 'date_registration', 'renovation_id'];
protected $primaryKey = 'id';
public function personal(){
return $this->hasOne(Personal::class, 'id', 'personal_id');
}
public function renovations(){
return $this->hasMany(Renovation::class, 'id', 'renovation_id');
}
}
Modelo Renovation:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\License;
class Renovation extends Model
{
use HasFactory;
protected $fillable = ['year_paid', 'iva_condition', 'status'];
protected $primaryKey = 'id';
public function license(){
return $this->belongsTo(License::class, 'id');
}
}
Los metodos estan en el componente de Livewire:
<?php
namespace App\Http\Livewire;
use Livewire\Component;
use Livewire\WithPagination;
use App\Models\License;
use App\Models\Personal;
use App\Models\Renovation;
class TbLicense extends Component
{
use WithPagination;
public $search, $license_id, $personal_id, $resolution, $identification, $date_registration, $renovation_id;
public $year_paid, $iva_condition, $status;
public $action = "store";
public $tabla = "Matricula";
public function render()
{
$items = Personal::all();
return view('livewire.tb-license',[
'licenses' => License::with('renovations','personal')
->where('identification', 'LIKE', "%{$this->search}%")
->orWhere('id', 'LIKE', "%{$this->search}%")
->paginate(5)
])->with(compact('items'));
}
public function store()
{
$this->validate([
'personal_id' => 'required',
'resolution' => 'required',
'identification' => 'required',
'date_registration' => 'required|date',
'year_paid' => 'required|date',
'iva_condition' => 'required',
'status' => 'required',
]);
Renovation::create([
'year_paid' => $this->year_paid,
'iva_condition' => $this->iva_condition,
'status' => $this->status
]);
$data = Renovation::latest('id')->first();
License::create([
'personal_id' => $this->personal_id,
'resolution' => $this->resolution,
'identification' => $this->identification,
'date_registration' => $this->date_registration,
'renovation_id' => $data->id
]);
$this->reset(['search', 'action', 'personal_id', 'resolution', 'identification', 'date_registration', 'renovation_id', 'year_paid', 'iva_condition', 'status']);
}
public function edit(License $License)
{
$this->resolution = $License->resolution ;
$this->identification = $License->identification ;
$this->date_registration = $License->date_registration ;
$this->renovation_id = $License->renovation_id ;
$this->license_id = $License->id ;
$this->year_paid = $License->renovations->year_paid ;
$this->iva_condition = $License->renovations->iva_condition ;
$this->status = $License->renovations->status ;
$this->renovation_id = $License->renovations->id ;
$this->action = "update";
}
public function update()
{
$license = License::find($this->license_id);
$license->update([
'resolution' => $this->resolution ,
'identification' => $this->identification ,
'date_registration' => $this->date_registration,
'renovation_id' => $this->renovation_id
]);
$license->renovations = Renovation::find($this->renovation_id);
$license->renovations->update([
'year_paid' => $this->year_paid ,
'iva_condition' => $this->iva_condition,
'status' => $this->status
]);
$this->reset(['search', 'action', 'personal_id', 'resolution', 'identification', 'date_registration', 'renovation_id', 'year_paid', 'iva_condition', 'status']);
}
public function destroy(License $License)
{
$License->delete();
}
public function default()
{
$this->reset(['search', 'action', 'resolution', 'identification', 'date_registration', 'renovation_id', 'year_paid', 'iva_condition', 'status']);
}
}
La migracion de License es la siguiente:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateLicensesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('licenses', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('personal_id')->nullable();
$table->string('resolution');
$table->bigInteger('identification');
$table->date('date_registration');
$table->unsignedBigInteger('renovation_id')->nullable();
$table->foreign('personal_id')->references('id')->on('personals');
$table->foreign('renovation_id')->references('id')->on('renovations');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('licenses');
}
}
Y la migracion de Renovation es la siguiente:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateRenovationsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('renovations', function (Blueprint $table) {
$table->id();
$table->date('year_paid');
$table->string('iva_condition');
$table->string('status');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('renovations');
}
}
No se en que estoy fallando pero usando la misma forma con dos modelos pero con una relacion hasOne funciona de maravilla. ¿En que estoy fallando? ¿Podrian ayudarme?