mardi 2 avril 2019

Call to undefined relationship [creator] on model \App\User

two table company_team and user related with user_id

here is definition in my CompanyTeam model

public function user_member_info()
{
    return $this->belongsTo(User::class, 'user_id');
}

and coding follow:

$c = new CompanyTeam();
$data = $c->with(['creator'])->find(1)->toArray();

then l got the error msg like my question's title

and then, l tried to print the sql

got these:

[
  {
    query: "select * from `n2_company_team` where `n2_company_team`.`company_team_id` = ? limit 1",
    bindings: [
      1
    ],
    time: 172.79
  },
  {
    query: "select * from `n2_user` where 0 = 1 and `n2_user`.`time_delete` is null and `client_id` > ?",
    bindings: [
      0
    ],
    time: 16.55
  }
]

the problem is, where condition of the second sql, it is where 0 = 1,

then l tried to use another way to get data

$c = new CompanyTeam();
$data = $c->find(1)->creator->toArray();

then print the sql:

[
  {
    query: "select * from `n2_company_team` where `n2_company_team`.`company_team_id` = ? limit 1",
    bindings: [
      1
    ],
    time: 172.79
  },
  {
    query: "select * from `n2_user` where `n2_user`.`user_id` = ? and `n2_user`.`time_delete` is null and `client_id` > ?",
    bindings: [
      0
    ],
    time: 16.55
  }
]

It work!

but because that l'm using graphql-laravel, and lib has been build the relation auto by with() way, So l cannot use the second way to build my sql, However, l can work



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire