I'm trying to define a one-to-many relationship in laravel where I have a Movimentacao
class and a TipoMovimentacaoFin
. One Movimentacao
is one type TipoMovimentacaoFin
and a TipoMovimentacaoFin
can have many Movimentacao
(s) associated with it. For it, in their models I declared:
class Movimentacao extends Model
{
public $timestamps = false;
public function tipo(){
return $this->hasOne('App\TipoMovimentacaoFin', 'tipo', 'tipo');
}
}
and
class TipoMovimentacaoFin extends Model
{
public $timestamps = false;
public function movimentacaos(){
return $this->hasMany('App\Movimentacao', 'tipo', 'tipo');
}
}
At the database level I got:
tipo_movimentacao_fins.tipo (varchar) as PK plus columns 1 and 2. and movimentacaos.tipo as FK to the first table. The names of the tables were auto generated by Laravel standards.
Now the problems begins in the View when I'm trying to access the type (tipo) of the Movimentacao. I give to the view the collections with $movimentacaos = Movimentacao::orderBy('data_prevista', 'desc')->get()
and when I loop @foreach($movimentacaos as $mov)
if I access $mov->tipo
I get the value of movimentacaos.tipo column but if I want to access tipo_movimentacao_fins.column1 or 2 for this $mov I can't. If I just write it gives me the error "htmlspecialchars() expects parameter 1 to be string, object given". If I try to acess any of the columns like $mov->tipo()->column1
it says Undefined property. So I tryied to check what is in this object of type HasOne returned by $mov->tipo()
but either dd
, var_dump
or var_export
also gives errors. Also the Laravel docs didn't helped too much
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire