Popular column with id created + date / time

1

I have a column named "code". Is it possible to fill it out using the same method that inserts the form? If so, how can I implement this in my code.

Method that receives the form and inserts it into the bank.

    public function insert(ManifestationFormRequest $request)
{

    //armazena form em dataForm
    $dataForm = $request->except('_token');

    //Insere Formulário no Banco
    $insert = $this->manifestation->insert($dataForm);

    //verifica se existe arquivo e se é válido
    if($request->hasFile('upload') && $request->file('upload')->isValid())
    {
        //Define default para variavel que vai conter nome do arquivo.
        $fileName = null;

        //Define nome aleatorio baseado no timestamp atual.
        $name = uniqid(date('dmYHis'));

        //Pega extensao do arquivo original e armazena em $ext.
        $ext = $request->upload->getClientOriginalExtension();

        //Define nome do arquivo + extensão.
        $fileName = "{$name}.{$ext}";

        //Armazena arquivo em Storage/app/uploads e renomeia.
        $upload = $request->upload->storeAs('uploads', $fileName);
    }

    Alert::success('Enviado...', 'Obrigado pela sua contribuição');
    return redirect()->route('index');
}

Migrate

Schema::create('manifestations', function (Blueprint $table) {
        $table->increments('id');
        $table->string('code', 64)->nullable();
        $table->string('name', 64);
        $table->string('email', 100);
        $table->string('rg', 20)->nullable();
        $table->string('cpf', 14)->nullable();
        $table->string('address', 150);
        $table->string('cep', 10);
        $table->string('city', 36);
        $table->string('state', 8);
        $table->string('phone', 16);
        $table->string('fax', 16)->nullable();
        $table->integer('FK_cat_id')->unsigned();
        $table->string('upload')->nullable();
        $table->string('manifestation', 800);
        $table->enum('response', [
                                    'Email',
                                    'Telefone',
                                    'Fax',
                                    'Não Informado'
                                ]);
        $table->timestamps();
    
asked by anonymous 08.11.2018 / 01:18

2 answers

1

For the comments and with a simple option you can do the following after this code just below:

//Insere Formulário no Banco
$insert = $this->manifestation->insert($dataForm);
if ($insert) // se for inserido com êxito você já pode utilizar no modo atualização
{
    //Passa o valor ao campo code
    $insert->code = hexdec(uniqid()).$insert->id;
    //Salve a alteração
    $insert->save();
}
//Continue o código normalmente

There is also another option to do this with Note that in your documentation explains that can be used in following events:

  • Retrieved
  • Creating and Created
  • Updating and Updated
  • Saving and Saved
  • Deleting and Deleted
  • Restoring and Restored

that by terminating ing the event has not yet occurred, and ted that the event has already occurred and other modifications can be made, in which case the corresponding event is created , that is, already created the registry , then you have access to id generated by the bank.

Within the folder app create a folder with the name of Observers and within that folder create a file as follows:

<?php namespace App\Observers;

use App\Manifestation; // model

class Manifestation
{
    public function created(Manifestation $manifestation)
    {
        $manifestation->code = hexdec(uniqid()).$manifestation->id;
        $manifestation->save();
    }
}

Then enter the file app\Providers\AppServiceProvider.php and add in the method boot the observer that was created:

<?php namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Manifestation;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Manifestation::observe(ManifestationObserver::class);
    }
}

After this, every time a record is inserted in the event it performs this modification and changes the code value.

You can only get the id inserted shortly after it is generated, so it has to be in the edit mode of this record.

08.11.2018 / 14:20
-2

First of all you can merge the request by adding the field.

$request->merge(['code' => $valor_que_quiser]);
$dataForm = $request->except('_token');
...

Give a dd ($ request-> all ()) after the merge and you will see that the code will be part of the form data.

At-te;

    
08.11.2018 / 17:56