In Laravel, you can set default conditions on your Eloquent models by using Global Scopes. A global scope allows you to add constraints to all queries for a given model.
Here’s how you can set a default condition like `post_status = 'publish'` in your Eloquent model:
### Step 1: Create a Global Scope
First, create a global scope class. You can place this class in the `app/Scopes` directory.
<code>
<?php
namespace App\Scopes;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
class PublishedScope implements Scope
{
public function apply(Builder $builder, Model $model)
{
$builder->where('post_status', 'publish');
}
}
</code>
### Step 2: Apply the Global Scope to Your Model
Next, apply this global scope to your model. For example, if you have a `Post` model, you would do this:
<code>
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Scopes\PublishedScope;
class Post extends Model
{
/**
* The "booted" method of the model.
*
* @return void
*/
protected static function booted()
{
static::addGlobalScope(new PublishedScope);
}
}
</code>
### Step 3: Remove the Scope When Necessary
If you ever need to query posts without the `post_status = 'publish'` condition, you can remove the global scope in your query using `withoutGlobalScope`:
<code>
use App\Models\Post;
use App\Scopes\PublishedScope;
// Query without the 'publish' condition
$posts = Post::withoutGlobalScope(PublishedScope::class)->get();
</code>
### Example Usage
Now, every time you query the `Post` model, the `post_status = 'publish'` condition will be automatically applied:
<code>
// This will only return posts with post_status = 'publish'
$publishedPosts = Post::all();
// This will only return posts with post_status = 'publish'
$publishedPosts = Post::where('title', 'like', '%Laravel%')->get();
</code>
This approach ensures that the default condition is consistently applied across all queries involving the `Post` model, unless explicitly removed using `withoutGlobalScope`.
Here’s how you can set a default condition like `post_status = 'publish'` in your Eloquent model:
### Step 1: Create a Global Scope
First, create a global scope class. You can place this class in the `app/Scopes` directory.
<code>
<?php
namespace App\Scopes;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
class PublishedScope implements Scope
{
public function apply(Builder $builder, Model $model)
{
$builder->where('post_status', 'publish');
}
}
</code>
### Step 2: Apply the Global Scope to Your Model
Next, apply this global scope to your model. For example, if you have a `Post` model, you would do this:
<code>
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Scopes\PublishedScope;
class Post extends Model
{
/**
* The "booted" method of the model.
*
* @return void
*/
protected static function booted()
{
static::addGlobalScope(new PublishedScope);
}
}
</code>
### Step 3: Remove the Scope When Necessary
If you ever need to query posts without the `post_status = 'publish'` condition, you can remove the global scope in your query using `withoutGlobalScope`:
<code>
use App\Models\Post;
use App\Scopes\PublishedScope;
// Query without the 'publish' condition
$posts = Post::withoutGlobalScope(PublishedScope::class)->get();
</code>
### Example Usage
Now, every time you query the `Post` model, the `post_status = 'publish'` condition will be automatically applied:
<code>
// This will only return posts with post_status = 'publish'
$publishedPosts = Post::all();
// This will only return posts with post_status = 'publish'
$publishedPosts = Post::where('title', 'like', '%Laravel%')->get();
</code>
This approach ensures that the default condition is consistently applied across all queries involving the `Post` model, unless explicitly removed using `withoutGlobalScope`.
Tags:
laravel