protonemedia / laravel-splade

💫 The magic of Inertia.js with the simplicity of Blade 💫 - Splade provides a super easy way to build Single Page Applications (SPA) using standard Laravel Blade templates, and sparkle it to make it interactive. All without ever leaving Blade.
https://splade.dev
MIT License
1.47k stars 111 forks source link

Table Class Exports with a Request value #444

Open EpikkuH opened 1 year ago

EpikkuH commented 1 year ago

Description:

I am using the table class because i need to do exports but i am having problems because i need a $id for the query which i get from the request and it is lost by the time i export the table someone haves any idea how i could approach this?

Steps To Reproduce Issue:

on my table class:

    public function __construct(Request $request)
    {
        $this->id = $request->id;
    }

    public function for()
    {
        return MovimientosDeInventario::query()
            ->join('productos','productos.id', '=','product_id')
            ->join('ordenes_de_entradas','ordenes_de_entradas.id', '=','order_id')
            ->select('movimientos_de_inventarios.*', 'productos.nombre','ordenes_de_entradas.order_key')
            ->where('product_id','=',$this->id);
    }

everything works except the export which returns an empty table instead of the same one it originally shows

jamesj2 commented 1 year ago

I suggest trying it without the ->where('product_id','=',$this->id); in your query.

...edit Sorry, I didn't pay attention enough attention to your issue description :(. Have you looked at the request going to the server? I probably not passing the id. And I don't see where that is configurable.

EpikkuH commented 1 year ago

I suggest trying it without the ->where('product_id','=',$this->id); in your query.

...edit Sorry, I didn't pay attention enough attention to your issue description :(. Have you looked at the request going to the server? I probably not passing the id. And I don't see where that is configurable.

It's alright yeah it seems the id gets lost and i don't see any way to make it work at least for now, if it's not possible right know it definitely should be in the future thanks for your time anyways

Adel-Kazem commented 9 months ago

I have fixed this issue by editing the core files of the Splade.

Here is step by step how I have successfully was able to send the ID that is associated with a relationship with the records inside the data table:

Step 1: Define Extra Parameters in BulkAction Class Add a method in the BulkAction class to set extra parameters. // In BulkAction.php public function setExtraParameters(array $extraParameters) { $this->extraParameters = $extraParameters; }

Step 2: Set Extra Parameters in HasBulkActions Trait When defining a bulk action, set the extra parameters. `// In HasBulkActions.php public function bulkAction( // ... other parameters, array $extraParameters = [] ): self { // ... existing code to set up bulk action

$this->bulkActions[$key]->setExtraParameters($extraParameters);

return $this;

} `

Step 3: Pass Product ID as an Extra Parameter In your ProductReviews table configuration, pass the productId. // In ProductReviews.php $table->bulkAction( label: 'Delete Selected', // ... other parameters, extraParameters: ['productId' => $this->productId] );

Step 4: Access Product ID in the Bulk Action Method Retrieve the productId within the deleteSelectedReviews method using the request helper. // In ProductReviews.php protected function deleteSelectedReviews($selectedIds) { $productId = request('wildId'); // ... existing deletion logic }

Step 5: Use Extra Parameters in Blade Template Modify the Blade template to use the extra parameters in the getUrl method. `{{-- In your Blade template --}} @foreach($table->getBulkActions() as $bulkAction) <button v-if="table.hasSelectedItems" @click.prevent="table.performBulkAction( @js($bulkAction->getUrl(['wildId' => $bulkAction->extraParameters])), // ... other parameters )" dusk="action.{{ $bulkAction->getSlug() }}"

{{ $bulkAction->label }} @endforeach `

These steps ensure that the productId is passed as an additional parameter when defining bulk actions and is accessible within the method that performs the bulk deletion. The use of @js in the Blade template ensures the JavaScript code has the correct parameters passed from the server side.