1

I am using Eloquent, I managed to set up composite keys in laravel using the Trait as in this question here:

How I can put composite keys in models in Laravel 5?

However, now I get the following error everywhere:

ErrorException in BelongsTo.php line 82:
Array to string conversion (View: /home/www/app/resources/views/relatorios/show.blade.php)

I understand this is because now my models have a composite primary key:

protected $primaryKey = array('cliente', 'id');

and in the relationship I have:

public function medico()
{
    return $this->belongsTo('App\Medico','medico');
}

so this is of course wrong.

I tried to modify to:

public function medico()
{
    return $this->belongsTo('App\Medico',['medico','cliente'],['id','cliente']);;
}

but no success.

Any ideas?

Community
  • 1
  • 1
  • 1
    Because of how that trait works, you'd have to re-write all **13** of Eloquent's relationship definitions if you wish to use them with this trait. You shouldn't use composite key's in laravel. – Ohgodwhy Dec 07 '16 at 17:53
  • The trait you refer to does not handle relationship definitions; what you might want to do in this case is "build" your entity ID by combining the IDs of both relationships, and keeping them separate as regular `indexes`. – DfKimera Dec 07 '16 at 17:55

0 Answers0