0

I am using Latest version of laravel-backpack (V5.X).

I list operation i have very small requirement. just want to add a query string dynamically to get it on create page. i have tried many buttons api but didn't worked.

Below is the code snippet. What's wrong here ?

/*
        |--------------------------------------------------------------------------
        | LIST OPERATION
        |--------------------------------------------------------------------------
        */
        $this->crud->operation('list', function () {
            $category = isset($this->crud->getRequest()->category) ? $this->crud->getRequest()->category : 'latest-news';

            $this->crud->addColumn('created_at');
            $this->crud->addColumn('category');
            $this->crud->addColumn('title');
            $this->crud->addColumn('status');
            
            $this->crud->addClause('where', 'category', '=' , trim($category));
            // $this->crud->removeButton('create');
            $this->crud->modifyButton('create', ['href' => config('backpack.base.route_prefix', 'admin').'/news?category='.$category]);
        });
Ravi Roshan
  • 570
  • 3
  • 14

2 Answers2

0

The top button is a "view button". You can't directly configure it's attributes.

It means you need to create a new button blade file and then do the modifications.

You can either: a) Remove the button with $this->crud->removeButton('create') and add your own afterwards (from a view, model function whatever rocks your boat).

https://backpackforlaravel.com/docs/5.x/crud-buttons

b) Create the previous mentioned file, and change the current button view:

$this->crud->modifyButton('create', ['content' => 'your_new_button_view']);

Cheers

Pedro X
  • 849
  • 5
  • 13
  • $this->crud->modifyButton('create', ['content' => ' Add Latest News']); i added this but not working – Ravi Roshan May 22 '23 at 14:23
  • If you want to do it like that, you also need to change the type. Probably the best to do in your scenario is to remove the button as I showed before, and then add a button from a model function if you don't want to create a button view: https://backpackforlaravel.com/docs/5.x/crud-buttons#adding-a-custom-button-without-a-blade-file-1 – Pedro X May 22 '23 at 18:09
0

but if you still need it, you can try this one

//Inside your setupListOperation, add this line
CRUD::removeButton('create');
CRUD::addButtonFromModelFunction('top', 'redirectToCreate', 'redirectToCreate', 'beginning');

and inside your model, I don't know what is your model's name, so just add this line in your related model

public function redirectToCreate()
    {
        if(request('category')){
            $link = route('employee.create').'?category='.request('category');
        }else {
            $link = route('employee.create');
        }
        return '<a class="btn btn-primary" href="'.$link.'"><i class="la la-plus"></i> Create</a>';
    }

I already test it and it works, also please change the route('employee.create') to your own route name let me know if you found any issues, cheers mate

christmex
  • 91
  • 1
  • 10