0

I'm trying to call this hasOne function, but I have 2 primary keys, how can I do it?

    public function user_ratings()
    {
        return $this->hasOne('App\User_rating', 'id_user', 'id');
    }

User_rating table

Schema::create('user_rating', function (Blueprint $table) {
            $table->integer('id_user')->unsigned(); //Increments es unsigned por defecto
            $table->integer('id_item')->unsigned();
            $table->integer('rating');
            $table->text('comment');
            $table->timestamps();

            $table->primary(['id_user', 'id_item']);

            $table->foreign('id_user')->references('id')->on('users')
                ->onUpdate('restrict')
                ->onDelete('cascade');

            $table->foreign('id_item')->references('id')->on('products')
                ->onUpdate('restrict')
                ->onDelete('cascade');
        });

This is my database

Database

Tiberius
  • 265
  • 1
  • 6
  • 14

2 Answers2

0
    public function user_ratings()
    {
        return $this->hasOne('App\User_rating', 'id_user', 'id');
    }

The second param is the foreign key and the third is the local key.

0

While the Laravel framework supports creating database tables with composite primary keys, Eloquent (the ORM) doesn't support them, and probably never will.

Erich
  • 2,408
  • 18
  • 40