F
Filament12mo ago
gon.exe

Create multiple records at once

Hi, I need create multiple records at once. I am thinking on use handleRecordCreation function on my createPage but I do not know how to do this requirement. At the moment of the creation I have the following data:
array:7 [// app\Filament\Resources\PurchaseOrderResource\Pages\CreatePurchaseOrder.php:113
"number" => "PO-0009-2023"
"date" => "2023-08-08 17:48:32"
"required_date" => "2023-08-25"
"amount" => 22610
"order_status_id" => 1
"business_partner_id" => array:2 [
0 => "2248"
1 => "2250"
]
"PurchaseOrderAssets" => array:2 [
0 => array:6 [
"enable" => true
"asset_id" => 10012
"price" => "2231.00"
"quantity" => "10.00"
"amount" => "22310.00"
"business_partner_id" => 2248
]
1 => array:6 [
"enable" => true
"asset_id" => 10038
"price" => "300.00"
"quantity" => "1.00"
"amount" => "300.00"
"business_partner_id" => 2250
]
]
]
array:7 [// app\Filament\Resources\PurchaseOrderResource\Pages\CreatePurchaseOrder.php:113
"number" => "PO-0009-2023"
"date" => "2023-08-08 17:48:32"
"required_date" => "2023-08-25"
"amount" => 22610
"order_status_id" => 1
"business_partner_id" => array:2 [
0 => "2248"
1 => "2250"
]
"PurchaseOrderAssets" => array:2 [
0 => array:6 [
"enable" => true
"asset_id" => 10012
"price" => "2231.00"
"quantity" => "10.00"
"amount" => "22310.00"
"business_partner_id" => 2248
]
1 => array:6 [
"enable" => true
"asset_id" => 10038
"price" => "300.00"
"quantity" => "1.00"
"amount" => "300.00"
"business_partner_id" => 2250
]
]
]
I need create 2 records on PurchaseOrder table and each one with the PurchaseOrderAssets respective. Thanks in advance!
11 Replies
Patrick Boivin
Patrick Boivin12mo ago
Is this a TableRepeater under Activos?
gon.exe
gon.exe12mo ago
Yes!
Patrick Boivin
Patrick Boivin12mo ago
Is it setup with ->relationship()? Actually, can you share this part of your code?
gon.exe
gon.exe12mo ago
Sure. Let me prepare that The form is a little complex because I am using it in differents situations. I share below part of the code and attach the function which is creating the Table Repeater
public static function form(Form $form) : Form
{

... some fields ...

// Purchase Order Assets
$array_form = array_merge($array_form, self::createFields());

// Return form
return $form
->schema($array_form)
->columns(4);
//
}

// Function to create assets table
public static function createFields() : array
{

return [
Section::make(__('Assets'))
->schema(function ($livewire, $record) {

// Get purchase_requisition_id from GET parameter
$purchase_requisition_id = $livewire->purchase_requisition_id ?? null;

// Get purchase_order_id from GET parameter
$purchase_order_id = $record->id ?? null;

// Get repeaterFunction
$repeaterFunction = $livewire->repeaterFunction ?? null;

// If purchase_requisition_id or purchase_order_id is set, then create table with assets from purchase_requisition_id
if ( ( ($purchase_requisition_id) || ($purchase_order_id) ) && ((!$repeaterFunction) or ($repeaterFunction == 'createTablePurchaseOrder') )){
$livewire->repeaterFunction = 'createTablePurchaseOrder';
!!Enter Here----> $tableRepeater = self::createTablePurchaseOrder($purchase_requisition_id, $purchase_order_id);
}
// Else create table with all assets
else{
$livewire->repeaterFunction = 'createTableRepeater';
$tableRepeater = self::createTableRepeater();
}

return $tableRepeater;

})
->columns(4)
->compact()
->collapsible(),
];

}
public static function form(Form $form) : Form
{

... some fields ...

// Purchase Order Assets
$array_form = array_merge($array_form, self::createFields());

// Return form
return $form
->schema($array_form)
->columns(4);
//
}

// Function to create assets table
public static function createFields() : array
{

return [
Section::make(__('Assets'))
->schema(function ($livewire, $record) {

// Get purchase_requisition_id from GET parameter
$purchase_requisition_id = $livewire->purchase_requisition_id ?? null;

// Get purchase_order_id from GET parameter
$purchase_order_id = $record->id ?? null;

// Get repeaterFunction
$repeaterFunction = $livewire->repeaterFunction ?? null;

// If purchase_requisition_id or purchase_order_id is set, then create table with assets from purchase_requisition_id
if ( ( ($purchase_requisition_id) || ($purchase_order_id) ) && ((!$repeaterFunction) or ($repeaterFunction == 'createTablePurchaseOrder') )){
$livewire->repeaterFunction = 'createTablePurchaseOrder';
!!Enter Here----> $tableRepeater = self::createTablePurchaseOrder($purchase_requisition_id, $purchase_order_id);
}
// Else create table with all assets
else{
$livewire->repeaterFunction = 'createTableRepeater';
$tableRepeater = self::createTableRepeater();
}

return $tableRepeater;

})
->columns(4)
->compact()
->collapsible(),
];

}
@pboivin, thanks for helping me. The form has the logic we discuss on https://discord.com/channels/883083792112300104/1138141715266547752 PurchaseOrder.php
namespace App\Models;

use App\Models\OrderStatus;
use App\Models\BusinessPartner;
use App\Models\PurchaseOrderAsset;
use App\Models\PurchaseRequisition;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class PurchaseOrder extends Model
{
use HasFactory;
use SoftDeletes;

protected $fillable = [
'number',
'date',
'required_date',
'amount',
'business_partner_id',
'order_status_id',
];

public function OrderStatus()
{
return $this->belongsTo(OrderStatus::class);
}

public function PurchaseOrderAssets()
{
return $this->hasMany(PurchaseOrderAsset::class);
}

public function PurchaseRequisition()
{
return $this->belongsTo(PurchaseRequisition::class);
}

public function BusinessPartner()
{
return $this->belongsTo(BusinessPartner::class);
}

}
namespace App\Models;

use App\Models\OrderStatus;
use App\Models\BusinessPartner;
use App\Models\PurchaseOrderAsset;
use App\Models\PurchaseRequisition;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class PurchaseOrder extends Model
{
use HasFactory;
use SoftDeletes;

protected $fillable = [
'number',
'date',
'required_date',
'amount',
'business_partner_id',
'order_status_id',
];

public function OrderStatus()
{
return $this->belongsTo(OrderStatus::class);
}

public function PurchaseOrderAssets()
{
return $this->hasMany(PurchaseOrderAsset::class);
}

public function PurchaseRequisition()
{
return $this->belongsTo(PurchaseRequisition::class);
}

public function BusinessPartner()
{
return $this->belongsTo(BusinessPartner::class);
}

}
Patrick Boivin
Patrick Boivin12mo ago
Ok, it's indeed a bit complex, forgive me for skimming a little bit 😅 It looks like you're using the PurchaseOrderAssets relationship on the repeater Is this setup correctly on your model?
gon.exe
gon.exe12mo ago
Yes, it is ! haha Yes, i just added the model code here But I believe I am not using correctly the form and relationship()
Patrick Boivin
Patrick Boivin12mo ago
Right, thanks for pointing it out! The hasMany() looks of to me... Just a note that ->default() will only run on Create, not on Edit. Is it working in your case, on Create?
gon.exe
gon.exe12mo ago
I am using the PurchaseOrderResource to display the fields I need (number, date, amount, order_status_id, business_partner_id ). But then I need to create as many records as different business_partner_id values in the TableRepeater. Example records: PurchaseOrder (ID, number, date, amount, order_status_id, business_partner_id ): [1; 'PO-1', '2023-08-09', 1000, 1, 8] [2; 'PO-2', '2023-08-09', 2000, 1, 9] PurchaseOrderAsset: (quantity, price, amount, purchase_order_id, asset_id) [1, 500, 500, 1, 1] [2, 250, 500, 1, 2] [1, 1500, 1500, 2, 3] [5, 100, 500, 2, 4] Yes, I am using it to init TableRepeater fields Let me know if I am making understand myself
Patrick Boivin
Patrick Boivin12mo ago
Ok. It looks like the ->relationship() should be good for what you're trying to do, but it's possible I'm missing some detail. An alternative you could explore for saving manually is a lifecycle hook, like afterSave(), on your page component.
gon.exe
gon.exe12mo ago
Thanks @pboivin , I will move to a custom page with a custom form to catch the submit function
Want results from more Discord servers?
Add your server
More Posts