Home  >  Q&A  >  body text

Laravel association model problem

Laravel association model problem

PHP中文网PHP中文网2713 days ago585

reply all(2)I'll reply

  • 某草草

    某草草2017-05-16 16:51:25

    Loop

    foreach ($posts as $post) {
        foreach ($post->comments as $c) {
             //do something
        }
    }

    As long as the relationship is set correctly, it will be looped like this, similar to nested arrays, and paging usually does not need to be written by yourself.

    If you want to get the result of join, just use join instead of with.

    reply
    0
  • 怪我咯

    怪我咯2017-05-16 16:51:25

    laravel can also directly perform joint query, for example

    DB::table('orders')->join('order_items', 'order_items.order_id', '=', 'orders.id')->select('orders.id', 'orders.type', 'orders.created_at', 'order_items.amount', order_items.status')->paginate();

    These are actually included in the document, please read the document carefully before using it

    reply
    0
  • Cancelreply