Result field name instead of id on Laravel 5?

Asked

Viewed 976 times

1

I have a relationship in Laravel but when I make an appointment comes the id and not the name of the field.

Model Evento

    public function tipo_evento() {
    return $this->hasOne('App\TipoEvento','id','tipo_evento');
}

Model TipoEvento

    public function evento() {
    return $this->belongsTo('App\Evento');
}

In the controller

$eventos = Evento::with('tipo_evento')->get();

The tables are

Eventos
  -id (1)
  -nome_evento(blabla)
  -tipo_evento(1)

Tipo Evento
  -id(1)
  -nome(festa)

want to while using $eventos = Evento::with('tipo_evento')->get(); come tipo_evento party nay 1 how are you coming?

  • The relationship is wrong

  • 2

    I thought it would be that way

2 answers

1

Try this:

foreach ( Evento::with('tipo_evento')->get() as $evento )
{
    echo $evento->tipo_evento->nome;
}
  • where I would put this, on the model?

  • Yeah, sorry I’ll edit to be clearer.

  • if I place error withPivot and neither recognizes the relationship put >withPivot('name','typo_event')

  • Which version are you using?

  • I’m using the last version of the Laravel 5.4

  • Undoes what you had done before and tries to do what I have now put in the answer.

  • now gave error in $event->type_event->name; speaking q event type_event is not an object (put in controller)

Show 3 more comments

1


The relationships of 1 for many in those two Models and the nomenclature to function would:

Class Evento

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Evento extends Model
{
    protected $table = "eventos";
    protected $primaryKey = "id";
    protected $fillable = ['nome_evento', 'tipo_evento'];

    public function tipoEvento()
    {
        return $this->belongsTo('App\TipoEvento','tipo_evento','id');
    }
}

Class TipoEvento

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class TipoEvento extends Model
{
    protected $table = "tipoeventos";
    protected $primaryKey = "id";
    protected $fillable = ['nome'];

    public function eventos()
    {
        return $this->hasMany('App\Evento','tipo_evento','id');
    }
}

How to use?

foreach (Evento::with('tipoevento')->get() as $evento)
{
    echo $evento->tipoevento->nome;
}

or making inner join also:

$eventos = Evento::join('tipoeventos', 'tipoeventos.id','=','eventos.id')
            ->select('tipoeventos.nome', 'eventos.id', 'eventos.nome_evento')
            ->get();

foreach ($eventos as $evento)
{
    echo $evento->nome;
}

Observing: check the changes, the way to write the methods and also observe the relations ...

References:

  • when I use hasOne?

  • 1

    @Guilhermefreire when the relation has only 1 different item from the collection that can have several items, in this case I suggested the best proposal that would be 1 for N, but for example at that link explains well better, particularly it is very little used, but, it is an option that exists in certain cases.

Browser other questions tagged

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