E
E
ettychel2020-05-18 14:02:01
Laravel
ettychel, 2020-05-18 14:02:01

Why is Eloquent communication not working?

Good afternoon
There are 3 tables:
PM
Event
SnPmContact

Made connections in the models:

PM
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class PM extends Model
{
    public $timestamps = false;

    public function events()
    {
        return $this->hasMany(Event::class, 'pm_id', 'PODE');
    }

    public function contacts()
    {
        return $this->hasMany(SnPmContact::class, 'PODE_PM', 'PODE');
    }
}

event
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Event extends Model
{

    public function pm(): BelongsTo
    {
        return $this->belongsTo(PM::class, 'pm_id', 'PODE');
    }

    public function contacts(): BelongsTo
    {
        return $this->belongsTo(SnPmContact::class, 'pm_id', 'PODE_PM');
    }
}

SnPmContact
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class SnPmContact extends Model
{

    public function pm(): BelongsTo
    {
        return $this->belongsTo(PM::class, 'PODE_PM', 'PODE');
    }

    public function events()
    {
        return $this->belongsTo(Event::class, 'PODE_PM', 'pm_id');
    }
}

Using the route, I tested the connection with such a request that it works
App\Models\SnPmContact::select(['PODE_PM', 'USER_NAME'])->with(['pm:PODE'])->get()

The query returns data, everything is fine

Then I decided to add a connection to GraphQL (I use this package )
Type GraphQL
type PM {
    ID: Int
    PODE: Int
    NAMEPM: String
    NAME2PM: String
    TIME_ZONE: Int
    contacts: [SnPmContact]
    events: [Event]
}

type SnPmContact {
    PODE_PM: Int
    USER_NAME: String
    USER_POST: String
    pm: PM
    events: [Event]
}

type Event {
    id: Int!
    pm_id: Int!
    start_time: DateTime
    status: Int
    type: Int
    log_text: String
    created_at: DateTime
    updated_at: DateTime
    pm: PM
    contacts: [SnPmContact]
}

Added Query
type Query {
    contactsByPodePM(PODE_PM: Int! @where): [SnPmContact] @all 
}


And the connection itself refuses to work, here is the error for one object:
Mistake
{
      "message": "Internal server error",
      "extensions": {
        "category": "internal"
      },
      "locations": [
        {
          "line": 4,
          "column": 5
        }
      ],
      "path": [
        "contacts",
        0,
        "pm"
      ]
    }


Actually, what is the error is not clear, while the connection for Event-> pm works fine in GraphQL, but for SnPmContact-> pm it does not work in GraphQL.
Help me to understand

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
ettychel, 2020-05-18
@ettychel

The indication of the primary key in the Xs model helped, which he did not like
protected $primaryKey = 'PODE_PM';

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question