SOFTonSOFA

Author: Jarek (page 2 of 3)

Laravel – Custom pivot model in Eloquent

pivot

Sometimes you may wish to use custom pivot model when working with many-to-many relationships in Eloquent, in order to add some behaviour on top of the core features.

There are a few things to consider before you start.

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

Eloquence emerging

eloquence-emerging

After spending long time with Eloquent I finally decided to release some of my work – extensions that will make your life easier, when working with Eloquent ORM.

They will be released bit by bit on github and much more will be found in the book I’m working on.

https://leanpub.com/eloquence-emerging

As a starter take a look at the repo and make sure to follow, for I will be updating it regularly!

https://github.com/jarektkaczyk/eloquence

 

Leave your thoughts in the comments – what would you like me to cover in the book, what is the most difficult part that you need help with, what use-cases would be most desirable.

Laravel 5 Eloquent Global Scope how-to

scope

If you are looking for an example of Eloquent Global Scope feature, then you’re in the right place!

Check the ready-to-use demo at …

Continue reading

Laravel Query Builder global scope – how to use custom Connection and Query Builder in Laravel 4

builder

Eloquent Global Scopes make it easy to customize your Model’s queries, however sometimes it is just not enough.

You may want to use similar solution for you DB::table(..) calls as well. While there’s no built-in feature for this, you can still do it with just a little more effort, and here’s how it’s done.

Continue reading

sofa/revisionable – nice & easy way to keep track of db changes

revisions

It is essential for every application to keep track of the data revisions. As an admin or sort of supervising user, you want to know how, when and who is responsible for changing the data.

Here’s something you should try out, because it makes this kind of work stupid-simple:

Sofa/Revisionable

Continue reading

Tweaking Eloquent relations – how to get hasMany relation count efficiently?

count

When working with relationships, most likely you need to get count of related models in many places. There are obvious ways to achieve this of course, but not always efficient. Especially when you are loading a collection of models and their relations.

So let me show you what we can do about it!

Continue reading

Laravel – how to define and use Eloquent Global Scopes

Laravel 4.2 introduced a new way of handling soft deletes by making use of traits and query builder macros wrapped in as a feature, that many people asked for for a very long time – Global Scopes.

This might be very useful for everyone, however the docs are not very eloquent in this case. So keep on reading to learn how you can implement global scopes in your project – it’s really easy and lets you write even more expressive code for your Eloquent models.

Check the ready-to-use demo at …

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

Olderposts Newerposts

Copyright © 2017 SOFTonSOFA

Theme by Anders NorenUp ↑