search

Home  >  Q&A  >  body text

php - Two tables in Laravel are related through multiple fields. How to write the model relationship?

For example, table A has x_id, y_id, and table B also has x_id, y_id. The two fields in table A are 1 to multiple B tables.

select XXX from A left join B on A.x_id = B.x_id and A.y_id = B.y_id

There are belongsTo and belongsToMany in laravel. Finding relevant documents may not be able to meet my needs. The key is that there is an And relationship here.

漂亮男人漂亮男人2767 days ago771

reply all(1)I'll reply

  • 怪我咯

    怪我咯2017-05-27 17:45:51

    Redesign the database table structure. Add an auto-incrementing primary key to table A. The x_id and y_id in table A are jointly unique, UNIQUE KEY A_x_id_y_id_UNIQUE (x_id, y_id). Then joining is easy.

    reply
    0
  • Cancelreply