John Davidson

php - Eloquent hasMany doesn't load collection

0 comments
Message:


I'm relatively new to eloquent, and have problems loading data from a hasMany relation in an app that uses eloquent as the database layer. My (simplified) code is as follows:


use Illuminate\Database\Eloquent\Model;
class Answer extends Model{
protected $table = 'answer';
public $timestamps = false;
public $incrementing = false;
protected $fillable = [
"nerdId",
"invitationid",
"eatingno",
"price",
"haspaid",
"attending",
"noeating",
];
public function topping() {
return $this->hasMany(AnswerTopping::class, 'answerid');
}
}

class AnswerTopping extends Model{
protected $table = 'eating';
public $timestamps = false;
protected $fillable = [
'answerid',
'toppingid',
'add'
];

public function answer() {
return $this->belongsTo(Answer::class);
}
}


The SQL Schema is like below


CREATE TABLE `answer` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nerdid` int(11) NOT NULL,
`invitationid` int(11) NOT NULL,
`eatingno` tinytext,
`price` float NOT NULL,
`haspaid` tinyint(4) NOT NULL,
`attending` tinyint(1) NOT NULL DEFAULT '0',
`noeating` tinyint(4) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2119 DEFAULT CHARSET=utf8;
CREATE TABLE `eating` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`answerId` int(11) NOT NULL,
`toppingid` int(11) NOT NULL,
`add` tinyint(4) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=280 DEFAULT CHARSET=utf8;

I then do the following query:


    $answer = Answer::with('topping')->find(32);
return $answer->toJson());

This results in a json like the following


{"id":32,"nerdid":1,"invitationid":54,"eatingno":"51","price":60,"haspaid":1,"attending":2,"noeating":0,"topping":[]}

Raw SQL query shows me that I do have data in the relation, so it should return more in "topping".


UPDATE
Checking sql queries in mysql (Setting it up for logging), I see that it actually do the expected queries on the database:


select * from `answer` where `answer`.`id` = 32 limit 1;
select * from `eating` where `eating`.`answerid` in (32);

Manually executing the SQL are giving me 2 entries in the eating table. But they are not showing up on the upper "Answer" json.



My Answer:
The issue might be with the naming convention of the foreign key in your `AnswerTopping` model. In your `AnswerTopping` model, you have defined the foreign key as `answerid` instead of `answerId` which is the actual foreign key in your database table.

To fix this issue, you need to update the `answerid` to `answerId` in your `AnswerTopping` model like below:

php
class AnswerTopping extends Model{
protected $table = 'eating';
public $timestamps = false;
protected $fillable = [
'answerId',
'toppingid',
'add'
];

public function answer() {
return $this->belongsTo(Answer::class, 'answerId');
}
}


After making this change, try running your query again:

php
$answer = Answer::with('topping')->find(32);
return $answer->toJson();


This should now load the collection of `topping` in your `Answer` model.

Rate this post

5 of 5 based on 1418 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us