George Rivera
George Rivera

Reputation: 261

One to many relationship returns NULL

I have the following Migrations:

Table: bebidas:

class CreateBebidasTable extends Migration{

  public function up() {
    Schema::create('bebidas', function ($table) {
      $table->increments('id');
      $table->integer('tipo_id')->unsigned();
      $table->string('bebi_name');
      $table->string('bebi_size');
      $table->float('bebi_price');
      $table->timestamps();
      $table->foreign('tipo_id')->references('id')->on('tipobebidas');
    });
  }

  public function down() {
    Schema::drop('bebidas');
  }

}

Table: tipobebidas

class CreateTiposBebidasTable extends Migration {

    public function up()
    {
        Schema::create('tipobebidas', function($table) {
            $table->increments('id');
            $table->string('name');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop('tipobebidas');
    }

}

These are the models:

class Bebida extends Eloquent{

  public function TipoBebida() {
    return $this->belongsTo('TipoBebida');
  }

}


class TipoBebida extends Eloquent{

  protected $table = "tipobebidas";


  public function Bebidas() {
    return $this->hasMany('Bebida');
  }

}

Each Bebida (drink) has a TipoBebida (drink type) and vive-versa. I'm trying to get a composed table showing all fields from the bebidas table and tipobebidas table.

Based on Laravel's documentation on eager loading, I'm running the following command:

$bebidas = Bebida::with('tipobebida')->get();

At this point $bebidas has the following value: (I'm removing the timestamps fields)

[
{"id":1,"bebi_name":"COCA-COLA","bebi_size":"1 litro",
 "bebi_price":4,"tipo_id":1,"tipobebida":null},
{"id":2,"bebi_name":"COCA ZERO","bebi_size":"1 litro",
 "bebi_price":4,"tipo_id":1,"tipobebida":null}
]

Instead of "tipobebida":null, I was expecting something like "name":"refrigerantes" or some representation of the tipobebidas table contents.

I inspected the SQL commands being run, here it is:

select * from `tipobebidas` where `tipobebidas`.`id` in (?)

How can I get this to work?

I'll be using this data on a couple nested foreach loops to show the drinks Bebida grouped by type TipoBebida.

Thank you!

Upvotes: 0

Views: 2382

Answers (2)

George Rivera
George Rivera

Reputation: 261

I got it to work. It all came down to naming conventions.

Here's what I did:

-The name of the foreign id field must be the singular of the table name plus _id, therefore the migration for bebidas was changed to the following:

class CreateBebidasTable extends Migration{

  public function up() {
    Schema::create('bebidas', function ($table) {
      $table->increments('id');
      $table->integer('tipobebida_id')->unsigned();  // ** new field name **
      $table->string('name');
      $table->string('size');
      $table->float('price');
      $table->timestamps();
    });
  }

  public function down() {
    Schema::drop('bebidas');
  }

}

Also, the foreign key relationship was producing a SQL error, tried to fix it, still nothing, so I removed the following line: $table->foreign('tipo_id')->references('id')->on('tipobebidas');

Everything else remained unaltered.

The eager loading is working.

Thanks everyone for your help!!!

Upvotes: 2

user2433934
user2433934

Reputation:

First, in tables bebidas and tipobebidas I don't see and foreign keys... I think in bebidas you should have tipobebidas_id and it is a foreign key to tipobebidas id field. After you do that, change your model methods to:

class Bebida extends Eloquent{
  protected $table = "bebidas";

  public function TipoBebida() {
    return $this->belongsTo('TipoBebida', 'tipobebida_id');
  }

}

class TipoBebida extends Eloquent{

  protected $table = "tipobebidas";

  public function Bebidas() {
    return $this->hasMany('Bebida');
  }

Upvotes: 0

Related Questions