SOFTonSOFA

Tag: laravel (page 1 of 2)

Tinker like a boss – aliases

This is part of the series tinker like a boss (in psysh)

  1. tinker… what?
  2. includes
  3. casters (presenters)
  4. aliases
  5. http (and other) helpers
  6. wtf is this error?

Aliases

Class aliasing is very simple concept, that allows you to call a class by its alas, rather than original name. It’s not very common to use aliases when writing code , for your IDE gets you covered.

If you’re working with Laravel, it’s worth mentioning that all the facades are also aliases, that’s why you have things like request, validator etc available in global namespace. this is a convention introduced by Taylor to make things a bit more expressive,but still something that your IDE does for you.

Continue reading

Tinker like a boss – custom casters

This is part of the series tinker like a boss (in psysh)

  1. tinker… what?
  2. includes
  3. casters (presenters)
  4. aliases
  5. http (and other) helpers
  6. wtf is this error?

Custom casters

Using Laravel, you’re already familiar with idea of casters even if you’re not aware of that. Check this out:

Continue reading

Tinker like a boss – custom includes

This is part of the series tinker like a boss (in psysh)

  1. tinker… what?
  2. includes
  3. casters (presenters)
  4. aliases
  5. http (and other) helpers
  6. wtf is this error?

Custom includes

Let’s stick to a convention of a .tinker file bootstrapping our REPL env. PsySH allows including such file in 2 ways:

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

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 – 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

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 – 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

Olderposts

Copyright © 2017 SOFTonSOFA

Theme by Anders NorenUp ↑