Laravel-admin: How to get model value in Form?

Created on 16 Jan 2018  ·  9Comments  ·  Source: z-song/laravel-admin

I want database column value within Form.
I search and try many way but there is no way to get database column value in Form.

e.g I want to get value of User(table) id
$user->id but no way to get the value.

Most helpful comment

I tried to access the model value, but it is only accessing the model, not the real object. So, I made it as follows:

  1. Get the ID of model from the route as follows:
    $id = request()->route()->parameter('<route_param_name>');
    Here, "route_param_name" is the binding key, which can be found by just dumping var_dump(request()->route()->parameters());
    @lambae96 in your case it will be 'stock'
  2. Get the object by ID:
    model = $form->model()->find($id);

So, the final code of mine was:
if ($form->isEditing()) { $id = request()->route()->parameter('config'); $model = $form->model()->find($id); }

All 9 comments

how about this?

$form->model()->colum_name;

$form->model()->colum_name;

I already tried but no vlaue can retrieve.
I do a little research and found.
Form is not same as Grid and before return the Form there is no value from database is retrieve.

class UserController extends Controller
{
    public function edit($id)
    {
         ....
        $content->body($this->form($id)->edit($id));
        ....
    }

    public function form($id = null)
    {
        // Get database value here
        if ($id) {
            $user = User::find($id);
        }
        .....
    }
}

@z-song this returns null for me

@simonpeters
you need to pass (id) like this.

$this->form($request->id);

@z-song i have a same proplem with @zinmyotun-gic
my code:
protected function form()
{
return Admin::form(Stock::class, function (Form $form) {
$form->model()->id;
}
}
and this return null
pls help me

I tried to access the model value, but it is only accessing the model, not the real object. So, I made it as follows:

  1. Get the ID of model from the route as follows:
    $id = request()->route()->parameter('<route_param_name>');
    Here, "route_param_name" is the binding key, which can be found by just dumping var_dump(request()->route()->parameters());
    @lambae96 in your case it will be 'stock'
  2. Get the object by ID:
    model = $form->model()->find($id);

So, the final code of mine was:
if ($form->isEditing()) { $id = request()->route()->parameter('config'); $model = $form->model()->find($id); }

overwirte the following code:

    public function edit($id, Content $content)
    {
        return $content
            ->title($this->title())
            ->description($this->description['edit'] ?? trans('admin.edit'))
            ->body($this->form($id)->edit($id));
    }

I tried to access the model value, but it is only accessing the model, not the real object. So, I made it as follows:

  1. Get the ID of model from the route as follows:
    $id = request()->route()->parameter('<route_param_name>');
    Here, "route_param_name" is the binding key, which can be found by just dumping var_dump(request()->route()->parameters());
    @lambae96 in your case it will be 'stock'
  2. Get the object by ID:
    model = $form->model()->find($id);

So, the final code of mine was:
if ($form->isEditing()) { $id = request()->route()->parameter('config'); $model = $form->model()->find($id); }

thanks a lot dude, works for me

Was this page helpful?
0 / 5 - 0 ratings

Related issues

donglianyou picture donglianyou  ·  3Comments

abufalbo picture abufalbo  ·  3Comments

xiaalngf picture xiaalngf  ·  3Comments

cdhraesaemer picture cdhraesaemer  ·  3Comments

MarKco picture MarKco  ·  3Comments