如何在php中创建一对多关系

如何在php中创建一对多关系,php,mysql,sql,Php,Mysql,Sql,我以前在拉威尔工作。所以最近我转向了核心php,所以我尝试了一对多关系,但它总是返回单独的记录。 我有以下几张表 作者 id | username | created_by | updated_by id | author_id | book_name | book_image | 书籍 id | username | created_by | updated_by id | author_id | book_name | book_image | 查询 select * from au

我以前在拉威尔工作。所以最近我转向了核心php,所以我尝试了一对多关系,但它总是返回单独的记录。 我有以下几张表

作者

id | username | created_by | updated_by
id | author_id | book_name | book_image |
书籍

id | username | created_by | updated_by
id | author_id | book_name | book_image |
查询

select * from authors left join books  on author.id=books.author_id
我在看我的结果,就像这样

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Norm

            [books] => Array
                (
                  [0] =>Array
                    (
                    [id] => 4
                    [book_name] => great wall
                    [created_at] => 2015-09-11 04:45:07
                    [updated_at] => 2015-09-11 04:45:07
                    )

                    [1] =>Array
                    (
                    [id] => 6
                    [book_name] =>new book
                    [created_at] => 2015-09-11 04:45:07
                    [updated_at] => 2015-09-11 04:45:07
                    )
                )
        )
    [1] => Array
        (
            [id] => 2
            [name] => Norm

            [books] => Array
                (
                  [0] =>Array
                    (
                    [id] => 2
                    [book_name] => amazing star
                    [created_at] => 2015-09-11 04:45:07
                    [updated_at] => 2015-09-11 04:45:07
                    )

                    [1] =>Array
                    (
                    [id] => 3
                    [book_name] =>way of journy
                    [created_at] => 2015-09-11 04:45:07
                    [updated_at] => 2015-09-11 04:45:07
                    )
                )

        )


but in core php i am getting  ouput


Array
(
    [0] => Array
        (
            [id] => 1
            [username] => david
            [created_by] => 
            [user_id] => 1
            [books] => book1
        )

    [1] => Array
        (
            [id] => 2
            [username] => david
            [created_by] => 
            [user_id] => 1
            [books] => book2
        )

    [2] => Array
        (
            [id] => 3
            [username] => david
            [created_by] => 
            [user_id] => 1
            [books] => book3
        )


    [3] => Array
        (
            [id] => 5
            [username] => miller
            [created_by] => 
            [user_id] => 2
            [books] => new1
        )

    [4] => Array
        (
            [id] => 6
            [username] => miller
            [created_by] => 
            [user_id] => 2
            [books] => new2
        )

)
所以我想在很多书中显示作者姓名。有人能帮我吗,我怎样才能做到这一点

class Author extends Model
{
    /**
     * Get the books for the author.
     */
    public function books()
    {
        return $this->hasMany('App\Book');
    }
}

...

class Book extends Model
{
    /**
     * Get the atuhor that owns the book.
     */
    public function post()
    {
        return $this->belongsTo('App\Author');
    }
}

--

您好,我正在寻找的是核心php查询,而不是laravel。您对核心php有什么意思?带PDO的简单PHP?@Krenor.yes简单PHP。