Reputation: 100
im setting up a One To One (Polymorphic) like this
My Models:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Payement extends Model{
protected $table = 'payements';
protected $primaryKey = 'id';
public function payementable(){
return $this->morphTo();
}}
class Recu extends Model{
protected $table = 'recus';
protected $primaryKey = 'id';
public function payement(){
return $this->morphOne('App\Payement', 'payementable');
}}
My Tables Schemas
Schema::create('recus', function (Blueprint $table) {
$table->bigIncrements('id');
});
Schema::create('payements', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('numero')->unique();
$table->bigInteger('payementable_id');
$table->string('payementable_type');
$table->timestamps();
});
the problem is this is working
App\Payement::find(1)->payementable;
this return null
App\Recu::find(1)->payement;
and this return empty collection
Recu::first()->payement()->get()
Upvotes: 1
Views: 1407
Reputation: 193
The settings you presented don't have any issue. Please test with this data:
payements:
---------------------------------------------
|id|numero|payementable_type|payementable_id|
---------------------------------------------
|1 |1 |App\Recu |2 |
---------------------------------------------
recus:
----
|id|
----
|1 |
----
|2 |
----
|3 |
----
Upvotes: 3