Laravel-admin: Fill user id before save form

Created on 21 Jan 2017  ·  4Comments  ·  Source: z-song/laravel-admin

Hi

I have a model that has relationship with user model so I need to add the user_id before saving the form. Here is my code in protected funtion form():

$form->saving(function (Form $form) { // $form->text('user_id')->value(Admin::user()->id); $form->user_id = Admin::user()->id; });

I tried $form->user_id = Admin::user()->id; and $form->text('user_id')->value(Admin::user()->id); but still did not work. The user_id is inserted as NULL

The model $fillable already set with these cols

When I add $form->hidden('user_id')->value(Admin::user()->id); that works fine.

Do I miss something?

Thanks

P/S: I make somes tests by adding:
$form->user_id = Admin::user()->id; $form->testing_1 = Admin::user()->id; $form->desc = 'isdji ajsd s ' . Admin::user()->id;

Only desc col get added the user id but not the other 2.

Most helpful comment

mdoel-form only saves column data which defined as a field in the form.

Try this:

$form->hidden('user_id');
$form->saving(function (Form $form) {
    $form->user_id = Admin::user()->id;
});

All 4 comments

mdoel-form only saves column data which defined as a field in the form.

Try this:

$form->hidden('user_id');
$form->saving(function (Form $form) {
    $form->user_id = Admin::user()->id;
});

That works

Thanks

mdoel-form only saves column data which defined as a field in the form.

Try this:

$form->hidden('user_id');
$form->saving(function (Form $form) {
    $form->user_id = Admin::user()->id;
});

Hello,

I tried to follow this example, but it did not work.
Error
image

image

image

My Model
image

Table Artigo
image

Table Admin_users
image

Do you have any tips on what I should do?

mdoel-form only saves column data which defined as a field in the form.

Try this:

$form->hidden('user_id');
$form->saving(function (Form $form) {
    $form->user_id = Admin::user()->id;
});

That fixed my problem

Was this page helpful?
0 / 5 - 0 ratings

Related issues

clock1129 picture clock1129  ·  3Comments

fokoz picture fokoz  ·  3Comments

MarKco picture MarKco  ·  3Comments

vlongen picture vlongen  ·  3Comments

joernroeder picture joernroeder  ·  3Comments