0

Cannot seem to find what I am doing wrong anymore after trying to get something else to work using polymorphic relations.. I have two tables setup:

Payments:

- id
- name
- etc..

Deposits:

- id
- payment_id
- name
- etc..

My model relationships:

Payment:

public function deposits()
{
    return $this->hasMany(Deposit::class);
}

Deposit:

public function payments()
{
    return $this->belongsTo(Payment::class);
}

Now I have one record in each table for testing purposes. I am trying to eager load the payments when displaying the deposits:

For example:

$deposits = Deposit::with('payments')->get();

This does not return the payment that is associated with it, just returns null for the relation. In my table the payment_id is 1, just as the id in the payments table. To my recollection, this should work. What am I missing?

Edit, the getQueryLog returns this:

array(3) { [0]=> array(3) { ["query"]=> string(52) "select * from `users` where `users`.`id` = ? limit 1" ["bindings"]=> array(1) { [0]=> int(1) } ["time"]=> float(0) } [1]=> array(3) { ["query"]=> string(24) "select * from `deposits`" ["bindings"]=> array(0) { } ["time"]=> float(0) } [2]=> array(3) { ["query"]=> string(53) "select * from `payments` where `payments`.`id` in (?)" ["bindings"]=> array(1) { [0]=> int(0) } ["time"]=> float(0) } }

enter image description here

Hardist
  • 2,098
  • 11
  • 49
  • 85
  • 1
    You need to specify the foreign key in your `payments()` function as the second argument since laravel assumes the foreign key is `table1name_table2name_id`. So in your case it would be `return $this->belongsTo(Payment::class, 'payment_id');` – Andrei Jul 29 '16 at 14:46
  • I take that back, it's `return $this->hasMany(Deposit::class, 'payment_id');`. See [here](https://laravel.com/docs/5.1/eloquent-relationships#one-to-many). – Andrei Jul 29 '16 at 14:53
  • @Andrew laravel assume keys 'ModelName_id`, table name has nothing to do in that case. **Eloquent will take the "snake case" name of the owning model and suffix it with _id** – devnull Jul 29 '16 at 14:54
  • @xdevnull That is correct according to the laravel docs. I'm a little rusty on my laravel skills but that's no excuse. – Andrei Jul 29 '16 at 14:55
  • Yup it should work without specifying the foreign key – Hardist Jul 29 '16 at 14:56
  • @Ron What do u get when executing this ```Payement::all()``` – devnull Jul 29 '16 at 14:56
  • That works, also with eager loading the deposits. It's the other way around that isn't working, the Deposit::with('payments') – Hardist Jul 29 '16 at 14:57
  • @RonBrouwers ```Deposit::all()``` works too? – devnull Jul 29 '16 at 15:01
  • Yup, it's very strange... It all works except for the relations belongsTo it seems. Might be something else wrong somewhere in the code probably, since it seems the code above should simply work as expected.. – Hardist Jul 29 '16 at 15:03
  • Okay let's log the sql query for ```relationship```, Run this code after the relationship eager loading (that doesn't work) ```var_dump(DB::getQueryLog())``` – devnull Jul 29 '16 at 15:05
  • Then you should probably specify the foreign key in the `belongsTo` relationship as well. And check if the appropriate Payment::class is used.. in the code.. Check the `use` statements at the top of your Deposit Model – Huzaib Shafi Jul 29 '16 at 15:05
  • That getQueryLog returns nothing, not sure how to use it, never used it before. With or without the `use` statements it doesn't work. But I will look for more info on that getQueryLog :) – Hardist Jul 29 '16 at 15:08
  • @RonBrouwers http://stackoverflow.com/questions/27753868/how-to-get-the-query-executed-in-laravel-5-dbgetquerylog-returning-empty-arr – devnull Jul 29 '16 at 15:12
  • Thanks, was already checking out that question. Yeah it returns something. will edit it in the question – Hardist Jul 29 '16 at 15:14
  • Added a picture of the getQueryLog results – Hardist Jul 29 '16 at 15:19
  • So appearantly it checked for a "payments_id". So I guess we now know what's wrong.. Thanks for all the help lol, uess I should specify the foreign key after all... – Hardist Jul 29 '16 at 15:20
  • @ron-brouwers You missed my comment above.. :) – Huzaib Shafi Jul 29 '16 at 15:21
  • @RonBrouwers Add answer to your question and mark it as "Accepted". – devnull Jul 29 '16 at 15:23
  • Working on it :P Still trying to figure out why I have to specify the foreign key because it should work. PS. Even when specifying the foreign key, it's still looking for `payments_id`. Ofcourse I can change my database column but I like figuring out why stuff is "unexpected" like this.. – Hardist Jul 29 '16 at 15:25
  • How do you know it is searching for `payments_id`? – Huzaib Shafi Jul 29 '16 at 15:27
  • Good question, I don't, I assumed.. I thought so because of the queryLog results. I changed my database column name in deposits to `payments_id` and it worked – Hardist Jul 29 '16 at 15:29
  • Checked again using the foreign key and it works now, will add my answer now but still check as to why this is happening, I shouldn't have to add it (not that it matters ofcourse) – Hardist Jul 29 '16 at 15:30

1 Answers1

0

Changed my relation in my Deposit model and it works:

public function payment()
{
    return $this->belongsTo(Payment::class, 'payment_id');
}

Still figuring out why I have to add it, since it should work without it as well.

Hardist
  • 2,098
  • 11
  • 49
  • 85