Search code examples
mysqldatabaselaravelrelational-databasebelongs-to

laravel two identical belongsTo() relations, one returns an array another string


I don't get it I have two foreign keys identically set and two belongsTo() functions but one of them doesn't work right, instead of returning an array it just returns a string value from database.

Here are the tables that I have in database.

Table pages

-----------------------------------------------------
| id | author |      title     | content   | status |
-----------------------------------------------------
| 1  |   1    | page one title | page text |    1   |
-----------------------------------------------------
| 2  |   1    | page one title | page text |    2   |
-----------------------------------------------------

Table users

----------------------------------------------
| id | username |      email          | role |
----------------------------------------------
| 1  |   darth  | darth@deathstar.dot |   1  |
----------------------------------------------

Table page_statuses

--------------------------
| id |    name   | value |
--------------------------
| 1  | Published |   1   |
--------------------------
| 2  |   Draft   |   2   |
--------------------------
| 3  |  Review   |   3   |
--------------------------

So the author and status fields from pages table are foreign keys of tables users and table page_statuses.

Both fields, author and status from pages are integers and unsigned, and also indexes. When i check relations in phpmyadmin I get this on pages table:

----------------------------------------------------------------------
| Column |    Foreign key constraint (INNODB) |         Keyname          |
----------------------------------------------------------------------
| author |       `laravell`.`users`.`id`      |   pages_author_foreign   |
----------------------------------------------------------------------
| status | `laravell`.`page_statuses`.`value` | pages_status_foreign     |
----------------------------------------------------------------------

When I input some data in database on my pages table in author and status fields i can click on numbers and it will lead me to proper data (in phpmyadmin). For example if I click on author number it will redirect me to users table and show the row of that author, so that works.

And now i have set the models

models/
   Page.php
   PageStatus.php
   User.php

And finally i need to set relations. Under Page.php i have

public function author() {
    return $this->belongsTo('User', 'author', 'id');
}

public function pagestatus() {
    return $this->belongsTo('PageStatus', 'status', 'value');
}

So everything is set right and everything should be working but something is not.

When I die & dump the author returns just a string while pagestatus returns and array of that row. The output is

----------------------------------------------
| author |                status             |
----------------------------------------------
|    1   | {"id":2,"name":"Draft","value":2} |
----------------------------------------------

So the status returns correct and author doesn't returns correct.


Solution

  • You can't name the relationship the same as the name of your foreign key column!

    Changing either one should work, however I suggest you change the foreign key columns to author_id. Then you also don't need to specify the foreign key column in your relation since you're using the conventional names.