Laravel-admin: How can I set the id as a primary key manual

Created on 19 Oct 2017  ·  3Comments  ·  Source: z-song/laravel-admin

  • Laravel Version: 5.4.*
  • PHP Version: >= 5.6.4
  • Laravel-admin: 1.4.*

Description:

How can I set the id as a primary key manual? In the other hand, I don't want to use Auto Increment for this primary key.

Steps To Reproduce:

1- My database
MyTable
id - int --> auto increment <-- I don't want to use it.
data - text

I want to insert to this table using Form and I want to set the value of id as the value as I want.

Most helpful comment

@mfvodka107 Try following code

protected function form($creating = false)
{
    ....
    if ($creating) {
        $form->text('gid', 'ID');
    }

    $form->saving(function ($form) {
        $form->input('id', request()->get('gid'));
    });   
}

And in create method of controller

    public function create(Content $content)
    {
        return $content
            ->header('Header')
            ->description('description')
            ->body($this->form(true));
    }

All 3 comments

In mysql remove AUTO_INCREMENT from id column

In form use $form->text('id');

I have followed exactly steps but when go to create page the Primary key field do not show up.

@mfvodka107 Try following code

protected function form($creating = false)
{
    ....
    if ($creating) {
        $form->text('gid', 'ID');
    }

    $form->saving(function ($form) {
        $form->input('id', request()->get('gid'));
    });   
}

And in create method of controller

    public function create(Content $content)
    {
        return $content
            ->header('Header')
            ->description('description')
            ->body($this->form(true));
    }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

antranapp picture antranapp  ·  3Comments

MarKco picture MarKco  ·  3Comments

daguye918 picture daguye918  ·  3Comments

xiaalngf picture xiaalngf  ·  3Comments

zhenyangze picture zhenyangze  ·  3Comments