If you're familiar with Laravel, you'll know the concept of scopes. In Devisto, you can create and use scopes just like you would in Laravel.
Scopes let you group and reuse functionality. Let's say somewhere in your code, you do this:
When you find yourself doing the same thing over and over again, you can put this functionality in a scope and reuse it, like this:
Every scope has a name and a slug. Choose the slug wisely, because that's what you'll use in your code to call the scope (see below).
You can write the code for the scope just like you would in Laravel. Use the
$query variable to chain your query builder methods. If you have defined scope parameters, you can also use the
$parameters variable to access the parameter values.
Let's say we want to create a scope for the example above. We can write the scope like this:
In The Entry Table
If you go to the entries of a table, you'll find a Scopes dropdown in the upper right corner. In the dropdown, you can check one or more scopes, and optionally fill in the scope parameters. The checked scopes will be applied to the entry table, so you see the effects immediately.
As stated above, the slug is important to call your scope in code. Let's say you have a scope with the slug
foo. You can call it in your code like this:
Note that a scope can be chained to a query builder, together with other scopes, or other query builder methods from Laravel. Let's say you have a second scope
bar. You can call both scopes, and combine it with other methods:
If your slug contains multiple words, the slug will always be camel-case but when calling the scope in code, you'll have to use kebabCase. So if you have a scope with the slug
foo-bar, your code will look like this:
By defining parameters, you can make a scope dynamic. You can pass a value to each parameter at the moment you use the scope. In the scope code, you can use the
$parameters variable to access the parameters.
Let's say we create a scope
filter-title with a parameter
title and with the following code:
$query->where('title', 'like', '%'.$parameters['title'].'%');
Then we can use the scope:
// We can call the scope like this table('products')->filterTitle('foo')->get(); // Which is exactly the same as table('products')->where('title', 'like', '%foo%')->get();
When you enable the Global switch, the scope becomes global. This means the scope will always be automatically applied for every query. So without explicitly calling the scope, the scope will still be applied to all your queries.
Let's have a look at an example. You'll notice that for every new table in Devisto, we create a global Published scope for you. The scope filters the results and keeps only the published entries. Without this scope, you would have to filter the entries yourself every time you query the table:
// Without a global scope, you would have to do this every time table('products')->where('published', 1)->get(); // But with the global scope, the results will be automatically filtered table('products')->get(); // You can always disable a global scope, by calling `withoutGlobalScope` and passing the scope slug table('products')->withoutGlobalScope('published')->get(); // You can also disable multiple scopes at once table('products')->withoutGlobalScopes(['scope1', 'scope2'])->get(); // Or disable all global scopes table('products')->withoutGlobalScopes()->get();
So, next time you find yourself doing the same query over and over again, consider making it a global scope instead.
Note that global scopes can't have parameters.