Crud with Eloquent Laravel

-3

I'm trying to do a CRUD with Eloquent Laravel in 2 tables with FK and PK, but I'm not having success, since the models only lack the CRUD function, can anyone 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 question is how to do CRUD with Eloquent Laravel ORM in these tables.

    
asked by anonymous 22.09.2018 / 19:14

1 answer

0

Friend, it's very simple, it's 2 CRUDS. You can do it in 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();

Your example in fact is very similar to the example of Post and Comment of documentation Laravel: Inserting & Updating Related Models .

See also in the documentation how to retrieve request dodes: Retrieving Input

I hope it helps.

    
24.09.2018 / 04:37