z-song / laravel-admin

Build a full-featured administrative interface in ten minutes
https://laravel-admin.org
MIT License
11.13k stars 2.81k forks source link

How do you get an ID from another grid and retrieve the data to a form based on the ID? #5683

Open miaK1110 opened 1 year ago

miaK1110 commented 1 year ago

I have a customer model and person in charge model, and I want to get customer info from customer grid when creating or editing person in charge form.

I was able to get as far as giving the ID, but I don't know how to get this ID back into the form.

Here is my code. In person in charge form, you press the button to open a new window

$form = new Form(new Pic);

$form->html('<a href="http://localhost:8000/admin/customers" target="_blank" rel="nooper"><button type="button" class="btn btn-info" >open new window</button></a>');

$form->text('customer_name');
$form->text('customer_address');
$form->text('customer_phone');

return $form;

then call this function (this function is in the same controller as the person in charge form)

 $router->get('customers', 'PICController@searchCustomer');
  protected function searchCustomer(Content $content)
    {
        $grid = new Grid(new Customer);

        $grid->column('user_name');

        $grid->actions(function ($actions) {
            $id = $actions->getKey();
            $actions->add(new passId($id));
        });

        $grid->filter(function (Grid\Filter $filter) {
            $filter->like('user_name');

        return $content->header('Search Customers')->description('...')->body($grid);
    }

When you press 'Select' action, call this custom action

class PassId extends RowAction
{
    public $name = 'passId';

    public function handle($id)
    {
        return $this->response()->$id;
    }
    public function html()
    {
        return "<a class='report-posts btn btn-sm btn-danger'><i class='fa fa-info-circle'></i>Select</a>";
    }
}

Any advice would be greatly appreciated!

optiktr commented 1 year ago

### Don't answer these threads for too long. But "$form->model()->id" didn't work for me. I developed these methods this method.

//my exaple route localhost/admin/x/id/edit use Illuminate\Support\Facades\URL;

$string = URL::current(); $explode = explode('/x/', $string); $explode = explode('/edit', end($explode)); $id = $explode[0];