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,
]);
});
}
4 Replies
TranceCode
TranceCode3mo ago
and i have this code in my form
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Select::make('boiler_id')
->label('N° medidor')
->relationship('boilers', 'nummedidor'),
Forms\Components\TextInput::make('month')->type('month'),
Forms\Components\TextInput::make('previousreading'),
Forms\Components\TextInput::make('currentreading'),
]);
}
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Select::make('boiler_id')
->label('N° medidor')
->relationship('boilers', 'nummedidor'),
Forms\Components\TextInput::make('month')->type('month'),
Forms\Components\TextInput::make('previousreading'),
Forms\Components\TextInput::make('currentreading'),
]);
}
so, i need save the
boiler_id
boiler_id
and the
boiler_consumption_id
boiler_consumption_id
but the problem is that is not detecting the boiler_id from the form and i need save the register BoilerConsumption and use this id for save it in
boiler_consumption_id
boiler_consumption_id
So how can fix this in my code?
krekas
krekas3mo ago
where are you trying to get that boiler_id? you are doing saving event on one model but from the form i guess it's on another table?
lazydog
lazydog3mo ago
In your Pages, Create file, override this
protected function handleRecordCreation(array $data)
protected function handleRecordCreation(array $data)
TranceCode
TranceCode3mo ago
Don't worry bro, i fix it using replace saving for saved in booted method; like this!
self::saved(function ($boilerConsumption): void {
// Create register in boiler_boiler_consumption
\DB::table('boiler_boiler_consumption')->insert([
'boiler_id' => $boilerConsumption->boiler_id,
'boiler_consumption_id' => $boilerConsumption->id,

]);
});
self::saved(function ($boilerConsumption): void {
// Create register in boiler_boiler_consumption
\DB::table('boiler_boiler_consumption')->insert([
'boiler_id' => $boilerConsumption->boiler_id,
'boiler_consumption_id' => $boilerConsumption->id,

]);
});