Action buttons

Action Buttons

Action buttons are displayed when browsing a BREAD next to each row
You can add your own buttons very easily. First we will create an Action-class which extends Voyagers AbstractAction in app/Actions/MyAction.php
1
<?php
2
3
namespace App\Actions;
4
5
use TCG\Voyager\Actions\AbstractAction;
6
7
class MyAction extends AbstractAction
8
{
9
public function getTitle()
10
{
11
return 'My Action';
12
}
13
14
public function getIcon()
15
{
16
return 'voyager-eye';
17
}
18
19
public function getPolicy()
20
{
21
return 'read';
22
}
23
24
public function getAttributes()
25
{
26
return [
27
'class' => 'btn btn-sm btn-primary pull-right',
28
];
29
}
30
31
public function getDefaultRoute()
32
{
33
return route('my.route');
34
}
35
}
Copied!
Next we need to tell Voyager that we want to use this action. For this open your app/Providers/AppServiceProvider.php and search for the boot() method
1
<?php
2
3
namespace App\Providers;
4
5
use Illuminate\Support\ServiceProvider;
6
use Illuminate\Events\Dispatcher;
7
use TCG\Voyager\Facades\Voyager;
8
9
class AppServiceProvider extends ServiceProvider
10
{
11
public function boot()
12
{
13
Voyager::addAction(\App\Actions\MyAction::class);
14
}
15
}
Copied!
After that you will see your new button when browsing any BREAD

Showing/hiding actions

If you only want to show your action for special datatypes you can implement a function shouldActionDisplayOnDataType() in your action:
1
<?php
2
3
public function shouldActionDisplayOnDataType()
4
{
5
return $this->dataType->slug == 'posts';
6
}
Copied!
If you want to show your action-button on a per-row-base, simply implement a method shouldActionDisplayOnRow($row) and add your condition(s)
1
<?php
2
3
public function shouldActionDisplayOnRow($row)
4
{
5
return $row->id > 10;
6
}
Copied!

Mass Actions

Mass actions are called for multiple instances of a model. If you want your action to be a mass action, just implement the following method:
1
<?php
2
3
public function massAction($ids, $comingFrom)
4
{
5
// Do something with the IDs
6
return redirect($comingFrom);
7
}
Copied!
Last modified 1mo ago