F
Filament12mo ago
gon.exe

Keeping GET parameters through livewire ajax executions

I know this needed was already discussed in different moments because I saw the responses from that but I am not able to do this suggestions on my code. I need to keep a GET parameters through livewire ajax executions to invoke different functions on form function. At first I get the value by "request()->query('purchase_requisition_id');" but then I lose it. The only way I found to do that is: app\Filament\Resources\PurchaseOrderResource.php ->
public static function form(Form $form) : Form
{

// Purchase Order data
$array_form = [
... code for few fields ...
];

// Get purchase_requisition_id
$url = $_SERVER["HTTP_REFERER"] ?? $_SERVER["REQUEST_URI"] ?? null;
$purchase_requisition_id = explode('?purchase_requisition_id=', $url)[1] ?? null;

if ($purchase_requisition_id) {
$array_form = array_merge($array_form, self::createTablePurchaseOrder($purchase_requisition_id));
}
else{
$array_form = array_merge($array_form, self::createTableRepeater());
}

// Return form
return $form
->schema($array_form)
->columns(4);
//
}
public static function form(Form $form) : Form
{

// Purchase Order data
$array_form = [
... code for few fields ...
];

// Get purchase_requisition_id
$url = $_SERVER["HTTP_REFERER"] ?? $_SERVER["REQUEST_URI"] ?? null;
$purchase_requisition_id = explode('?purchase_requisition_id=', $url)[1] ?? null;

if ($purchase_requisition_id) {
$array_form = array_merge($array_form, self::createTablePurchaseOrder($purchase_requisition_id));
}
else{
$array_form = array_merge($array_form, self::createTableRepeater());
}

// Return form
return $form
->schema($array_form)
->columns(4);
//
}
Where I have to add the public property and mount function to replace the way I am getting and keeping the GET parameter? Thanks in advance!
Solution:
If you want to keep the mount() approach though, I think you can use a callback in your form schema(): ```php ->schema(function ($livewire) { if ($livewire->purchase_requisition_id) { return [...
Jump to solution
9 Replies
Patrick Boivin
Patrick Boivin12mo ago
You can't do this reliably in the static methods on your resource. I mean, you can find a hack but it's not designed for that. Instead, use the mount() method on the Page component directly (e.g. EditPosts) Here's a quick example: https://discord.com/channels/883083792112300104/1132764689995145448/1132771508587077712
gon.exe
gon.exe12mo ago
Thanks @pboivin for your response. I added the public property and the mount function to my CreatePage but how to add fields to the form in base of the value of this public property ? app\Filament\Resources\PurchaseOrderResource\Pages\CreatePurchaseOrder.php:
class CreatePurchaseOrder extends CreateRecord
{
protected static string $resource = PurchaseOrderResource::class;

public ?int $purchase_requisition_id = null;

public function mount(): void
{
$this->purchase_requisition_id = request()->query('purchase_requisition_id') ?: null;
parent::mount();
}
}
class CreatePurchaseOrder extends CreateRecord
{
protected static string $resource = PurchaseOrderResource::class;

public ?int $purchase_requisition_id = null;

public function mount(): void
{
$this->purchase_requisition_id = request()->query('purchase_requisition_id') ?: null;
parent::mount();
}
}
Patrick Boivin
Patrick Boivin12mo ago
Not sure exactly how you plan on using this information but maybe something like this:
TextInput::make('my_input')
->default(fn ($livewire) => $livewire->purchase_requisition_id ?: ''),
TextInput::make('my_input')
->default(fn ($livewire) => $livewire->purchase_requisition_id ?: ''),
When the form is initialized, use the property as default value if possible.
gon.exe
gon.exe12mo ago
Actually I need to create different fields based on the value of the variable. So in the $form I have added an IF to add fields to the schema based on value. So I'm thinking that what I need is outside the scope of the framework. I solved this situation as follows:
public static function form(Form $form) : Form
{
// Purchase Order data
$array_form = [
... some fields ...
];
//

// Get purchase_requisition_id
$purchase_requisition_id = self::getPurchaseRequisitionId();

// If purchase_requisition_id is set, then create table with assets from purchase requisition
if ($purchase_requisition_id) {
$array_form = array_merge($array_form, self::createTablePurchaseOrder($purchase_requisition_id));
}
// If purchase_requisition_id is not set, then create table with assets from asset types
else{
$array_form = array_merge($array_form, self::createTableRepeater());
}
//

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

public static function getPurchaseRequisitionId() {

// Get purchase_requisition_id
$url = $_SERVER["HTTP_REFERER"] ?? null;
if (strpos($url,'purchase_requisition_id')) {
$purchase_requisition_id = explode('purchase_requisition_id=', $url)[1] ?? null;
$purchase_requisition_id = explode('&', $purchase_requisition_id)[0] ?? null;
}
else{
$url = $_SERVER["REQUEST_URI"];
$purchase_requisition_id = explode('purchase_requisition_id=', $url)[1] ?? null;
$purchase_requisition_id = explode('&', $purchase_requisition_id)[0] ?? null;
}
//

return $purchase_requisition_id;
}
public static function form(Form $form) : Form
{
// Purchase Order data
$array_form = [
... some fields ...
];
//

// Get purchase_requisition_id
$purchase_requisition_id = self::getPurchaseRequisitionId();

// If purchase_requisition_id is set, then create table with assets from purchase requisition
if ($purchase_requisition_id) {
$array_form = array_merge($array_form, self::createTablePurchaseOrder($purchase_requisition_id));
}
// If purchase_requisition_id is not set, then create table with assets from asset types
else{
$array_form = array_merge($array_form, self::createTableRepeater());
}
//

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

public static function getPurchaseRequisitionId() {

// Get purchase_requisition_id
$url = $_SERVER["HTTP_REFERER"] ?? null;
if (strpos($url,'purchase_requisition_id')) {
$purchase_requisition_id = explode('purchase_requisition_id=', $url)[1] ?? null;
$purchase_requisition_id = explode('&', $purchase_requisition_id)[0] ?? null;
}
else{
$url = $_SERVER["REQUEST_URI"];
$purchase_requisition_id = explode('purchase_requisition_id=', $url)[1] ?? null;
$purchase_requisition_id = explode('&', $purchase_requisition_id)[0] ?? null;
}
//

return $purchase_requisition_id;
}
Patrick Boivin
Patrick Boivin12mo ago
If this works for you then it's all that matters really 😄 It's an interesting use-case
Solution
Patrick Boivin
Patrick Boivin12mo ago
If you want to keep the mount() approach though, I think you can use a callback in your form schema():
->schema(function ($livewire) {
if ($livewire->purchase_requisition_id) {
return [
// Some fields
];
}

return [
// Some other fields
];
})
->schema(function ($livewire) {
if ($livewire->purchase_requisition_id) {
return [
// Some fields
];
}

return [
// Some other fields
];
})
awcodes
awcodes12mo ago
Why not just load each set of fields in its own group then use the injected $livewire to show or hide the group in visible(). Might be easier than worrying about ifs, etc.
awcodes
awcodes12mo ago
Also looks like the handling of query strings changed in v3. https://livewire.laravel.com/docs/upgrading#url-query-string
Laravel
Upgrade Guide | Laravel
A full-stack framework for Laravel that takes the pain out of building dynamic UIs.
gon.exe
gon.exe12mo ago
Thanks both! I will try with Patrick suggestions first @pboivin your suggestion works fine! I will use it to keep the mount function and not use custom function to get the GET parameter. Thanks a lot! @awcodes thank you very much too!
Want results from more Discord servers?
Add your server
More Posts