I have a question about one to many relation of laravel.
I am trying to make programs based on this page. https://manablog.org/laravel_bulletin_board/
My programs are here.
class PostsController extends Controller
{
public function index()
{
\DB::enableQueryLog();
$comments = Post::find(9)->comments;
$query = \DB::getQueryLog();
Log::debug($query);
$posts = Post::all();
$query = \DB::getQueryLog();
Log::debug($query);
return view('bbc.index')->with('posts', $posts);
}
}
class Post extends Model {
protected $table = 'posts';
protected $connection = 'mysql';
public function comments(){
return $this->hasMany('App\Comment','post_id', 'id');
}
}
class Comment extends Model{
protected $table = 'comments';
protected $connection = 'mysql';
public function post() {
return $this->belongsTo('App\Post');
}
}
tables are here.
mysql> desc comments;
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| Id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| post_id | int(10) unsigned | NO | MUL | NULL | |
| name | varchar(50) | NO | | NULL | |
| content | varchar(300) | NO | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+------------+------------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
mysql> desc posts;
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| Id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(50) | NO | | NULL | |
| title | varchar(50) | NO | | NULL | |
| content | varchar(300) | NO | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+------------+------------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
But my program did not work what i expected. then i checked the log. Logs are here.
[2017-08-31 18:25:47] local.DEBUG: array (
0 =>
array (
'query' => 'select * from `posts` where `posts`.`id` = ? limit 1',
'bindings' =>
array (
0 => 9,
),
'time' => 4.5099999999999998,
),
1 =>
array (
'query' => 'select * from `comments` where `comments`.`post_id` is null and `comments`.`post_id` is not null',
'bindings' =>
array (
),
'time' => 0.53000000000000003,
),
)
there are no relation.
And i can't understand why 'select * from comments
where comments
.post_id
is null and comments
.post_id
is not null' is here.
what is this problem?
I would imagine the issue is because your id column is Id
and not id
.
MySQL itself is case insensitive, however, PHP is not.
You can either:
Id
to id
in your database (and migrations files if applicable)or
add the following to your models:
protected $primaryKey = 'Id';
and either change your relationship to:
public function comments() {
return $this->hasMany('App\Comment');
}
//or
public function comments() {
return $this->hasMany('App\Comment', 'post_id', 'Id');
}