How can save a record in a second table?
Hey guys, i need save a register in a second table using the same model, in this case i have this code
protected static function booted(): void
{
self::addGlobalScope('for_logged_company', function (Builder $builder): void {
if (auth()->guard('company')->check()) {
$user = auth()->user();
$companies = $user->companies;
if ($companies->count() > 0) {
$company = $companies->first();
$companyId = $company->id;
}
}
});
self::saving(function (self $company): void {
if (auth()->guard('company')->check()) {
$user = auth()->user();
$companies = $user->companies;
if ($companies->count() > 0) {
$company = $companies->first();
$company->company_id = $company->id;
}
}
// Obtener el último registro creado en BoilerConsumption
$boilerConsumption = BoilerConsumption::latest()->first();
// Obtener el boiler_id del modelo BoilerConsumption
$boiler_id = $boilerConsumption->boiler_id;
// Crear el registro en la tabla boiler_boiler_consumption
\DB::table('boiler_boiler_consumption')->insert([
'boiler_id' => $boiler_id,
'boiler_consumption_id' => $boilerConsumption->id,
]);
});
}protected static function booted(): void
{
self::addGlobalScope('for_logged_company', function (Builder $builder): void {
if (auth()->guard('company')->check()) {
$user = auth()->user();
$companies = $user->companies;
if ($companies->count() > 0) {
$company = $companies->first();
$companyId = $company->id;
}
}
});
self::saving(function (self $company): void {
if (auth()->guard('company')->check()) {
$user = auth()->user();
$companies = $user->companies;
if ($companies->count() > 0) {
$company = $companies->first();
$company->company_id = $company->id;
}
}
// Obtener el último registro creado en BoilerConsumption
$boilerConsumption = BoilerConsumption::latest()->first();
// Obtener el boiler_id del modelo BoilerConsumption
$boiler_id = $boilerConsumption->boiler_id;
// Crear el registro en la tabla boiler_boiler_consumption
\DB::table('boiler_boiler_consumption')->insert([
'boiler_id' => $boiler_id,
'boiler_consumption_id' => $boilerConsumption->id,
]);
});
}