1
Guys I have a structure like this model (Modelo apenas ilustrativo)
and want to know how I can access the data of the whole relationship to make a insert/update
in these tables with the data received from a form, being that I can give a select
with LEFT JOIN
in all these tables using a query
with DB::raw
, but I want to know how to do this only with eloquent methods. From file to file:
Where as you can see one Processo
has only one solicitacao
and vice versa, one solicitacao
may have N boxes and a box can be in N solicitacoes
, whereas one box
may have N arquivos
.
My Model Process would look like this:
public function processo_solicitacao (){
return $this->belongTo(Solicitacao::class, 'id_solicitacao', 'solicitacao_id');
}
My Model request like this:
public function processo (){
return $this->hasOne(Processo::class);
}
My model box like this:
public function solicitacoes (){
return $this->belongsToMany(Processo::class);
}
public function arquivos(){
return $this->hasMany(Arquivo::class);
}
My Model file like this:
public function solicitacoes (){
return $this->belongsTo(Box::class, 'box_id', 'id_box');
}
From now on I appreciate any help!
associate()
in a relation N:N ? It wouldn’t be$solicitacao->attach($box);
$box->associate($arquivo);
?? I get lost in the relationships of the Aravel =(– Gabriel Carneiro
No, the Associate() is for a 1:N or 1:1 relationship. N:N you have other methods, like attach([ id1, id2...]), etc... It was just an example, I didn’t notice that this was an N:N. Sorry for my example.
– Matheus Picioli
Quiet, I understood then, I have to fill all the tables individually and then leave relating =)
– Gabriel Carneiro
Exactly, remember that the method
Model::create
andModel::update
run a save, if in your table the id’s arenot null
, will make an exception.$model = new Model; $model->attribute = $value; .... $model->attach() // ou $model->associate()
.– Matheus Picioli