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

时间:2015-11-16 03:59:26

标签: php mysql sql

我之前曾在laravel工作过。所以最近我转移到核心PHP,所以我尝试一对多的关系。但它总是返回单独的记录。 我有以下表格

作者

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
        )

)

所以我想用很多书来显示作者姓名。任何人都可以帮助我,我怎样才能实现这个目标

1 个答案:

答案 0 :(得分:1)

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

- Laravel Documentation