http://img.youtube.com/vi/91lssWSBAZQ/0.jpgI want to remind you about 8 challenges created back in 2021, now updated to Laravel 10.Laravel News Links
This dating podcast is going viral for redpilling feminists. Here are a few of the most controversial clips.
https://media.notthebee.com/articles/63fe2c8b05bb663fe2c8b05bb7.jpg
Alright, these based clips have been popping up on my news feed for a week straight, so I figured it was time to share them with you.
Not the Bee
This clip of a minigun absolutely trashing an SUV is going viral so I rounded up a bunch of other minigun videos because I love you
https://media.notthebee.com/articles/63fd0552e14d863fd0552e14d9.jpg
I was famous for shooting my lever action rifle back in the day. Boy would I have loved to get my hands on one of these beauts:
Not the Bee
Pinball: The Man Who Saved the Game (Trailer)
https://theawesomer.com/photos/2023/02/pinball_the_man_who_saved_the_game_t.jpg
This movie dramatizes the story of Roger Sharpe, an NYC journalist who escapes from his stress by playing pinball, only to realize that the game is still illegal in the city. In 1976, he helped mount a campaign to overturn the 34-year ban and prove that pinball is a game of skill, not a game of chance. In theaters 3.17.23.
The Awesomer
Texas representative informs the federal government of tragic boating accidents that claimed all his constituents’ guns and ammo 😭
https://media.notthebee.com/articles/63fa8420de31263fa8420de314.jpg
Texas Rep. Bryan Slaton just submitted a resolution (H.C.R. No. 54) in the Texas Legislature to inform the federal government of a series of "alleged boating accidents" that occurred across Hunt, Hopkins, Van Zandt, and possibly other counties.
Not the Bee
When to use Laravel global scopes
https://hashnode.com/utility/r?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1676907059595%2Fd3de6e98-b36d-4d65-b56b-50a8993b4fff.png%3Fw%3D1200%26auto%3Dcompress%2Cformat%26format%3Dwebp%26fm%3Dpng
Laravel global scopes are great, but I don’t see them used a lot. Instead, I see a lot of local scopes being used to achieve the same thing. With proper implementation of global scopes, the code and security would be greatly improved. Let me illustrate this with a simple example.
The local scope way
In our codebase, we have a model Transaction
that stores transactions for our users. If we want to get the transaction for a logged-in user from the database, we could do it like this:
$transactions = Transaction::where('user_id', auth()->id())->get();
Since we would use this a lot throughout the codebase, it would make sense to create a local scope in the Transaction
model like this:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Transaction extends Model
{
public function scopeForLoggedInUser($query): void
{
$query->where('user_id', auth()->id());
}
}
With this local scope, we can make the query like this:
$transactions = Transaction::forLoggedInUser()->get();
This is a nice DRY (Don’t Repeat Yourself) refactor that cleans it up a little.
A question you should ask yourself
Local scopes are awesome, I use them where I can to keep the code DRY. But I learned to ask myself this question when I create a local scope: “Will the majority of the queries for this model use this local scope”.
If the answer is no, keep the local scope. It makes a lot of sense to use it.
When the answer is yes
This would be the point when considering a global scope. A global scope is always applied to all queries of a given model. You can create a global scope simply by creating a class that implements Illuminate\Database\Eloquent\Scope
.
<?php
namespace App\Models\Scopes;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
class TransactionsForLoggedInUserScope implements Scope
{
public function apply(Builder $builder, Model $model)
{
$builder->where('user_id', auth()->id());
}
}
After that, you should let your model be aware of the global scope:
<?php
namespace App\Models;
use App\Models\Scopes\TransactionsForLoggedInUserScope;
use Illuminate\Database\Eloquent\Model;
class Transaction extends Model
{
protected static function booted(): void
{
static::addGlobalScope(new TransactionsForLoggedInUserScope());
}
}
If you would now get all the transactions, it will only return the transactions of the logged-in user.
Transaction::all();
Removing a global scope
There are some scenarios thinkable where you want to create a Transaction
query without the global scope applied. For example, in an admin overview or for some global statistic calculations. This can be done by using the withoutGlobalScope
method:
Transaction::withoutGlobalScope(TransactionsForLoggedInUserScope::class)->get();
Anonymous Global Scopes
There is also a way to create a global scope without the use of an extra file. Instead of pointing to the TransactionsForLoggedInUserScope
class, you can include the query like this:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Transaction extends Model
{
protected static function booted(): void
{
static::addGlobalScope('for_logged_in_users', function (Builder $builder) {
$builder->where('user_id', auth()->id());
});
}
}
Personally, I don’t like anonymous global scopes since it can bloat your model if the query is a bit complex. To be consistent throughout the entire codebase, I always tend to use the external file global scopes, even if the query is as simple as in our example.
The downside of global scopes
I won’t lie to you, if you’re working on a codebase with global scopes and are not aware of them, you might fall into situations where nothing makes sense anymore. It made me question my career choices one (very bad) day 😂. You will get totally unexpected results when tinkering. If this has happened once, you’ve learned that it is good to have the withoutGlobalScopes
method in your tool belt while working in a codebase you’re not quite familiar with.
Security
If the security of your app relies on a global scope, it’s dangerous when you or a fellow developer will make changes to it in the future. Imagine in our example that someone would remove or change the global scope. Then all results for a user’s transactions would be completely faulty! This is why it’s really important to implement tests for global scopes, so this can’t happen. A typical (PEST) test for our example would look like this:
it("should only get the transactions for the logged-in user", function (){
$user = User::factory()->create();
$otherUser = User::factory()->create();
$transaction_1 = Transaction::factory()->create(['user_id' => $user->id]);
$transaction_2 = Transaction::factory()->create(['user_id' => $otherUser->id]);
$this->actingAs($user);
expect(Transaction::all())->toHaveCount(1)
->and($transaction_1->is(Transaction::first()));
});
Laravel News Links
NEW Primary Arms 3-18×44 & 4.5-27×56 FFP GLx Rifle Scopes Ready For Pre-Order
https://www.thefirearmblog.com/blog/wp-content/uploads/2023/02/PA-GLX-3-18X44F-ATHENA_04-180×180.jpg
If you’ve been visiting TFB regularly, you’ve probably noticed that Primary Arms has been busy releasing a number of new products this winter. We’ve recently announced new binoculars, a 1-6x LPVO optic, pistol red dot sights, and a 5x prism sight. Primary Arms has just announced the pre-order status for their new First Focal Plane […]
The post NEW Primary Arms 3-18×44 & 4.5-27×56 FFP GLx Rifle Scopes Ready For Pre-Order appeared first on The Firearm Blog.
The Firearm Blog
Laravel: Get Lat/Long from Address with Geocoder Package [VIDEO]
http://img.youtube.com/vi/6hl6fsNXRAU/0.jpgDemonstrating the example of how to get geo coordinates from the street address.Laravel News Links
Kamala just asked a crowd to clap for her … and they didn’t
https://media.notthebee.com/articles/63f7e2f79212c63f7e2f79212d.jpg
I never thought I’d see Jeb Bush topped for sheer cringe factor, but Kamala did it. She shattered the glass ceiling for women everywhere.
Not the Bee
WATCH: Marxist Angela Davis, who teaches that America was built by racist colonizers, finds out her ancestors were on the Mayflower
https://media.notthebee.com/articles/63f675171b9f163f675171b9f2.jpg
Please enjoy the entire Critical Race worldview fall apart in 49 seconds:
Not the Bee