一对多关系不起作用

one to many relation is not working

我对 laravel 的一对多关系有疑问。

我正在尝试根据此页面制作程序。 https://manablog.org/laravel_bulletin_board/

我的程序在这里。

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');
    }

}

桌子在这里。

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)

但是我的程序没有达到我的预期。 然后我检查了日志。 日志在这里。

[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,
  ),
)

没有关系。 我不明白为什么 'select * from comments where comments.post_id is null and comments.post_id is not null' 在这里。 这是什么问题?

我想这个问题是因为你的 id 列是 Id 而不是 id.

MySQL 本身是不区分大小写的,但是 PHP 不是。

您可以:

  • 在您的数据库(和迁移文件,如果适用)中将 Id 更改为 id

  • 将以下内容添加到您的模型中:

     protected $primaryKey = 'Id'; 
    

并将您的关系更改为:

    public function comments() {
        return $this->hasMany('App\Comment');
    }
    //or
    public function comments() {
        return $this->hasMany('App\Comment', 'post_id', 'Id');
    }