0

When I return with my relationship with the collection is always empty, I do have data in my database.

Client model

public function orders()
{
    return $this->belongsToMany(Service::class)->withTimestamps();
}

Service model

public function clients()
{
    return $this->belongsToMany(Client::class)->withTimestamps();
}

Query the relationship is always empty

$client = Client::with('orders')->firstOrFail();

My table migration;

Schema::create('client_service', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->unsignedBigInteger('client_id');
    $table->unsignedBigInteger('service_id');
    $table->timestamps();
});

I don't see how the relationship is empty using the many to many setup above.

Query log

array:2 [▼
  0 => array:3 [▼
    "query" => "select * from `clients` limit 1"
    "bindings" => []
    "time" => 0.59
  ]
  1 => array:3 [▼
    "query" => "select `services`.*, `client_service`.`client_id` as `pivot_client_id`, `client_service`.`service_id` as `pivot_service_id`, `client_service`.`created_at` as `pivot_created_at`, `client_service`.`updated_at` as `pivot_updated_at` from `services` inner join `client_service` on `services`.`id` = `client_service`.`service_id` where `client_service`.`client_id` in (1) ◀"
    "bindings" => []
    "time" => 1.05
  ]
]
Sam
  • 1,546
  • 4
  • 14
  • 22
  • Your code seems fine. Enable query log before that query `\DB::enableQueryLog();` and after the query `dd(\DB::getQueryLog())`. It should print the two executed queries. – Uroš Anđelić Jan 21 '20 at 12:00
  • If the answer below doesn't work. Change the function name from `orders()` to `services()` and then of course instead of `::with('orders')` make it `::with('services')` – Kabelbaan Jan 21 '20 at 13:22
  • @Sam the queries looks good. Try to run the second query directly from phpMyAdmin or whatever you use. If it returns no results then the problem is with your data. Maybe the `client_service` table doesn't contain the needed rows. – Uroš Anđelić Jan 21 '20 at 13:45
  • @UrošAnđelić I run the query in SQL and it returns the results correctly – Sam Jan 21 '20 at 15:54

1 Answers1

1

You need to set foreign keys. Everything else looks ok and should work.

Schema::create('client_service', function (Blueprint $table) {
    $table->bigIncrements('id');

    $table->unsignedBigInteger('client_id');
    $table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');

    $table->unsignedBigInteger('service_id');
    $table->foreign('service_id')->references('id')->on('services')->onDelete('cascade');

    $table->timestamps();
});
Dino Numić
  • 1,414
  • 2
  • 9
  • 17
  • Thank you this resolved my issue, I don't understand why this worked as I thought I followed the Laravel naming conventions? – Sam Jan 21 '20 at 16:07
  • @Sam I'm glad you resolved your issue but Eloquent doesn't check if there is a foreign key. Interesting situation. – Uroš Anđelić Jan 21 '20 at 19:04
  • @UrošAnđelić Yes indeed. You are only required to set up the relationship methods. But I like to set foreign keys so I don't end up with orphan rows and such. Don't know why it didn't work for him. – Dino Numić Jan 22 '20 at 11:04