Git Product home page Git Product logo

laravel-one-of-many's Introduction

Laravel one-of-many relation

This repository contains examples for laravel/framework#37252. The laravel-debugbar may be used to inspect queries and eager loaded models.

Relevant files:

  • app/Models/...
  • app/Http/Controllers/TestController

Setup

  1. Preparing
composer install
cp .env.example .env
php artisan key:generate
  1. Migrating & Seeding
php artisan migrate:fresh --seed
  1. Checkout subselect or join approach
composer setup-subselect
composer setup-join

laravel-one-of-many's People

Contributors

cbl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

laravel-one-of-many's Issues

Can't be filtered dynamically

Since the new ofMany() was added to Laravel I noticed that you can't filter it dynamically as there is no way to append the filter conditions to the inner sub select.

Lets say you have a ofMany:

    public function latestDetails(): HasOne
    {
        return $this->hasOne(UserVesselDetails::class)->latestOfMany('created_at');
    }

and you want to filter this down by an id (in my example a vessel_id), you can't. because you either have to add a argument to the relationship - preventing its use in eager loading, or you have to use a static value in it's definition (such as in the doc's example of using now() when defining the relationship)

This does not work (can't eager load it due to the requirement of an parameter):

    public function latestDetails(Vessel $vessel): HasOne
    {
        return $this->hasOne(UserVesselDetails::class)->forVessel($vessel)->latestOfMany('created_at', fn($q) => $q->forVessel($vessel));
    }

and neither does this (where does the var come from?):

    public function latestDetails(): HasOne
    {
        return $this->hasOne(UserVesselDetails::class)->where('vessel_id', ????)->latestOfMany('created_at', fn($q) => $q->where('vessel_id', ????));
    }

At least for me, this greatly limits the usefulness of this feature

I believe it should be possible to add filtering when modifying the with clause, like you would with any other eager load:

        $watches = Watch::with(['user.latestDetails' => fn($q) => $q->forVessel($vessel)])
                        ->forUser($request->user())
                        ->forVessel($vessel)
                        ->get()

The above does not work as expected, as the vessel filter is only applied to the outer select of the ofMany relation, resulting in SQL that does not do as you'd hope:

select
  `user_vessel_details`.*
from
  `user_vessel_details`
  inner join (
    select
      MAX(`user_vessel_details`.`id`) as `id_aggregate`,
      `user_vessel_details`.`user_id`
    from
      `user_vessel_details`
      inner join (
        select
          MAX(`user_vessel_details`.`created_at`) as `created_at_aggregate`,
          `user_vessel_details`.`user_id`
        from
          `user_vessel_details`
        where
          `user_vessel_details`.`user_id` in (133)
        group by
          `user_vessel_details`.`user_id`
      ) as `latestOfMany` on `latestOfMany`.`created_at_aggregate` = `user_vessel_details`.`created_at`
      and `latestOfMany`.`user_id` = `user_vessel_details`.`user_id`
    group by
      `user_vessel_details`.`user_id`
  ) as `latestOfMany` on `latestOfMany`.`id_aggregate` = `user_vessel_details`.`id`
  and `latestOfMany`.`user_id` = `user_vessel_details`.`user_id`
where
  `vessel_id` = 5755

I have not yet found a work around for this, short of not using this feature, building the query myself and using setRelation() :(

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.