Table bulk action with a variable in __construct
romain-lgr opened this issue · 4 comments
- Laravel Version: 10.0
- PHP Version: 8.1
- Splade JS Version (npm): 1.3.0
- Splade Breeze PHP Version (composer): 2.5
- Dev environment (OS, Sail/Valet/etc): Mac, Valet
I want to display a splade table of Products (Product model) belonging to an Event (Event model), so I created a Table view and I pass the $event variable. My code looks like this:
Controller
use App\Tables\EventProducts;
/**
* Display the event products.
*/
public function products(Event $event)
{
return view('events.products.index', [
'event' => $event,
'eventProducts' => new EventProducts($event),
]);
}
Table
<?php
namespace App\Tables;
use App\Models\Tenant\Product;
use Illuminate\Http\Request;
use ProtoneMedia\Splade\AbstractTable;
use ProtoneMedia\Splade\Facades\Toast;
use ProtoneMedia\Splade\SpladeTable;
class EventProducts extends AbstractTable
{
private $event;
/**
* Create a new instance.
*
* @return void
*/
public function __construct($event)
{
$this->event = $event;
}
/**
* Determine if the user is authorized to perform bulk actions and exports.
*
* @return bool
*/
public function authorize(Request $request)
{
return true;
}
/**
* The resource or query builder.
*
* @return mixed
*/
function for () {
return $this->event->products();
}
/**
* Configure the given SpladeTable.
*
* @param \ProtoneMedia\Splade\SpladeTable $table
* @return void
*/
public function configure(SpladeTable $table)
{
$table
->column('name')
->column('created_at')
->column('price', 'Price')
->column('actions', '')
->bulkAction(
label:'Remove',
each: function (Product $product) {
dd($product);
},
after:fn() => Toast::info('Products Removed!')
)
->paginate(30);
}
}
It works well but when I try the bulk action, I get the following error: Unresolvable dependency resolving [Parameter #0 [ $event ]] in class App\Tables\EventProducts. So it is asking me for the $event variable, how can I pass it into my bulk action method? Basically I want to be able to bulk detach and attach products from/to an event.
Thank you!
In addition, exports do not seem to work when using a relationship. I get an export.html file instead of an export.csv file when exporting.
Is it that we can simply not use bulk actions and export for a relationship table at the moment? Or am I missing something?
Passing constructor parameters is not supported for api requests, you could use query string parameters to retrieve the model in your SpladeTable as a workaround.
First inject request instance to your SpladeTable
<?php
namespace App\Tables;
use App\Models\Tenant\Product;
use Illuminate\Http\Request;
use ProtoneMedia\Splade\AbstractTable;
use ProtoneMedia\Splade\Facades\Toast;
use ProtoneMedia\Splade\SpladeTable;
class EventProducts extends AbstractTable
{
private Event|null $event;
/**
* Create a new instance.
*
* @return void
*/
public function __construct(Request $request)
{
$this->event = Event::find($request->get('event'));
}
If you include the 'event' query string parameter in your initial request it will be passed to API calls too and your SpladeTable will be able to load the event.
For instance if your url is "https://example-app.com/eventproducts" -> "https://example-app.com/eventproducts?event=1"
This may not be optimal but this will allow you to achieve what you want
Thanks a lot, I passed my event id in the URL as a query like you wrote and it works fine. Indeed it would be nicer to have cleaner URLs and a better solution for the future but for the moment it will do. I appreciate the support.
I started out making my tables without bulk actions and now I'm suddenly at a roadblock because of this. Are there plans to find a solution for tables that take constructor parameters?