SOFTonSOFA

Author: Jarek (page 2 of 4)

they can bite! – how (not) to use accessors in Eloquent

snake

Eloquent mutators & accessors are handy feature that can simplify your high level code and hide some data-related logic in the model.

However, it is pretty common to misuse them and soon regret it.

Let’s take a look and see where we can go wrong…

Continue reading

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

PHP how to use $this in Closure – context matters

closure

Anonymous functions, in php known as Closures, come in handy very often. One application in particular is very useful – extending classes, thanks to the capability to bind the closure to an instance and class scope.

However, binding to instance may prove a bit tricky, so let’s go through it together.

Continue reading

array_merge vs array_replace vs + (plus aka union) in PHP

join

If you wrote some PHP, most likely you have used array_merge here and there for your arrays. You may have met array_replace function, introduced in PHP5.3. Finally, you could notice + operator (aka union).

Since all 3 do similar thing, and the docs don’t quite describe the difference between them, here’s a nice image of it

Continue reading

Revisionable – a quick example

A short reminder about my revisionable package (compatible with L4 & L5+) and quick example of how you can use it:

Zgłoszenia

Continue reading

Laravel Searchable – the best package for eloquent

search

In this first post of a series about https://github.com/jarektkaczyk/eloquence – the package that allows working with eloquent models in even simpler manner – I’m going to introduce you to the searchable feature of the builder class.

Continue reading

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

Olderposts Newerposts

Copyright © 2018 SOFTonSOFA

Theme by Anders NorenUp ↑