SOFTonSOFA

Tag: relations

Querying relations in Laravel: get Models where latest related is…

tools

Another part of Querying relations in Laravel will cover such problem:

I want to get SomeModels that have latest RelatedModel (hasMany) matching.. _SOME CONSTRAINT HERE_

Eloquent provides expressive syntax for querying relations that can be used for simple cases: SomeModel::whereHas('relatedModels', function ($q) { ... })->get(). Unfortunately it is not capable of handling the latest RelatedModel part just like that.

Let’s dive into how we can tackle this problem using 2 approaches:

 

  1. The easy way – using Collection methods
  2. The right way – using subqueries

Continue reading

Laravel – querying any level far relations with simple trick

trick

Eloquent provides one Relation type for far related tables – hasManyThrough. However it works only with with cascade of hasOne/hasMany relations and you can use it only for 2 levels of nesting, while sometimes you need more.

Imagine, that you want to get the posts for logged-in user, having this setup:

Now, because there is no built-in method for such case, you would probably think of getting all the tags, looping through them, and merging all the posts from each tag. While this would work, it would be cumbersome, so let’s make it easier!

Continue reading

Tweaking Eloquent relations – how to get N related models per parent ?

related

Previously I showed you how to get single related model per each parent, while eager loading the hasMany relation. That was pretty easy and we just made the most of the Eloquent.

However, most likely the time will come, when you need several related models per parent.

It won’t be as easy, but it’s far from impossible, so let’s do it!

Continue reading

Tweaking Eloquent relations – how to get latest related model?

lates

Have you ever needed to show only single related model from the hasMany relationship on a set of parents?

Being it latest, highest or just random, it’s not very clever to load whole collection using eager loading, just like running query per every parent.

Of course you can do that better, and now let me show you how.  Continue reading

How to store clients in database? That is, Party Model implemented in Laravel 4 – part 1

party-model

Have you ever been building any app that handled client’s data, was it e-commerce, crm or anything similar, you surely wondered, how to store it in the database.

Most of the time we consider client as Organization or Person, one or the other. Obviously the two are completely different entities in real life, so it wouldn’t be wise to treat them the same. However, speaking of business, we want to treat both entities like a client, no matter if it’s a company or an individual.

So the problem arises, how to design the database efficiently while not having to do checks here and there within our code, to pick what entity we are dealing with this time.

And here comes the Party Model  Continue reading

Querying relations in Laravel 4: nested relation

eloquent-2

Previously we dealt with simple relations defined in Eloquent models and eager loading them in order to save database queries.

Now imagine nested relations like this:

So there are one-to-many relations between posts & categories as well as comments & posts.

Above relations look like this:

Continue reading

Querying relations with Eloquent in Laravel 4

eloquent

Laravel ORM Eloquent is brilliant!

That being said, let’s try to tackle very common questions that appear when you try to get related models loaded.

Continue reading

Copyright © 2017 SOFTonSOFA

Theme by Anders NorenUp ↑