Crud with Eloquent Laravel

Asked

Viewed 256 times

-3

I’m trying to do a CRUD with Eloquent Laravel in 2 tables with FK AND PK, but I’m not succeeding, already have the models just missing the CRUD function even, someone can help me?

This is model with FK:

class crud_consignado_acordo extends Model
{
    protected $fillable = [
        'cpf',
        'valor',
        'formaenvio',
        'datavencimento',
        'numerocontrato',
        'id_consignado_registro',
        'id'
    ];

    protected $table = 'tb_consignado_acordo';

    public function registro()
    {
        return $this->belongsTo(crud_consignado_registro::class, 'id_consignado_registro');
    }
}

E esta com a PK:

    class crud_consignado_registro extends Model
    {
        protected $fillable = [

            'produto',
            'datareg',
            'nomeoperador',
            'celula',
            'usuariox',
            'aspect',
            'supervisor',
            'hora',
            'id'

        ];

        protected $table = 'tb_consignado_registro_operador';

        public function acordo()
        {
            return $this->hasMany(crud_consignado_acordo::class, 'id_consignado_registro');
        }
    }

The doubt is how to do the CRUD with Eloquent Laravel ORM in these tables.

  • Always post your code to help, what have you already done in PHP? What is the structure of your bank? Where you are having difficulty more exactly?

  • I recommend you try to follow the convention of the Eloquent Models whenever possible, this facilitates the maintenance of the code by other programmers and still avoids having to go through much thing per parameter, because it automatically recognizes the things based on the convention.

1 answer

0

Buddy, it’s pretty simple, it’s 2 CRUDS. You can do it 3 ways:

$ccr = crud_consignado_registro::create([
    'produto' => $request->input('form_produto'),
    'datareg' => $request->input('form_datareg'),
    'nomeoperador' => $request->input('form_nomeoperador'),
    'celula' => $request->input('form_celula'),
    'usuariox' => $request->input('form_usuariox'),
    'aspect' => $request->input('form_aspect'),
    'supervisor' => $request->input('form_supervisor'),
    'hora' => $request->input('form_hora'),
]);

//opcao 1
$cca = $ccr->acordo()->create([
    'cpf' => $request->input('form_cpf'),
    'valor' => $request->input('form_valor'),
    'formaenvio' => $request->input('form_formaenvio'),
    'datavencimento' => $request->input('form_datavencimento'),
    'numerocontrato' => $request->input('form_numerocontrato'),
]);

//opcao 2
$cca = new crud_consignado_acordo($request->all());
$ccr->acordo()->save($cca);

//opcao 3
$cca = new crud_consignado_acordo($request->all());
$cca->id_consignado_registro = $ccr->id;
$cca->save();

His example incidentally is very similar to the example of Post and Comment of the documentation of Laravel: Inserting & Updating Related Models.

See also in the documentation how to recover request dodos: Retrieving Input

I hope it helps.

  • Paulo Sakamoto, thank you for your help, but my form is not in the default database, as I enter each value individually?

  • I updated the answer, but basically it’s using the $request->input('field_name')

  • Hello Paulo Sakamoto, thank you so much for the help, but for some reason my ajax still returns false, I tested only Insert in a table like this:

  • Hello Paulo Sakamoto, thank you so much for the help, but for some reason my ajax still returns false, I tested only Insert in a table like this: $ccr = crud_consignado_record()->create([ 'product' => 'test', 'datareg' => date('Y-m-d'), 'operator name' => $name, 'cell' => $cell, 'user' => $user, 'Aspect' => $supervisor, 'supervisor' => $Aspect, 'hour' => $hours, ]);

  • sql raw works, I really wanted to use eloquent because it makes the code more elegant and is easier to insert in fk, so it works: DB::Insert('Insert into tb_consignado_registro_operator(product, datareg, operator name, cell, user,Aspect, supervisor, time) values (?,? ,? ,? ,? ,? ,? ,? ,? )', ['test', date('Y-m-d'), $name , $cellula, $usuariox, $Aspect, $supervisor, $hours] );

  • Friend, in this case you have to post the code you are using to make the Insert, I believe some method of your controller.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.