Laravel Eloquent ORM with multiple tables

How can I make one query to insert into multiple tables using laravel Eloquent ORM relationship.ie

Table 1: Users

  • ID
  • name
  • Email

Table 2: Messages

  • ID
  • user_id
  • Content

Table 3: Image

  • ID
  • user_id
  • post_id
  • image_name

relations

  • The usersid reference table user_idin the other two tables.
  • The table postsis related to One To Many s users.
  • The table imageshas One To Many relationship with usersand post, that is, it can be shared with other users and other messages.

So, when you create a post insert, it should insert a record into tables with a single query.

+4
1

:

$post = (new Post)->fill($request->all()->toArray())->user()->associate(Auth::user())->save();

, Post , static::created .

, , Post Image.

->toArray() , , .

+2

Source: https://habr.com/ru/post/1619418/


All Articles