Eloquent Query Scope on Relationships
Problem :
$activePosts = Post::where('active', true)->get();
Solution:
class Post extends Model
{
public function scopeActive($query)
{
return $query->where('active', 1);
}
}
$activePosts = Post::active()->get();
=======================================================
Create Dynamic Scope:
class Post extends Model
{
public function scopeActive($query, $value)
{
return $query->where('active', $value);
}
}
// Get active posts
$activePosts = Post::active(true)->get();
// Get not active posts
$notActivePosts = Post::active(false)->get();
===========================================================
Scope with Relation :
$category = Category::find(1);
$activePost = $category->posts()->active(true)->get();
Also in PHP:
- Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (SQL: alter ta
- clear composer cache
- display date and hour php
- 500 Server Error laravel when sign up
- append single qoute arounf variable in php string
- 500 internal server error php laravel
- array reverse php
- check array item exists php
- duplicate values in array php
- Argument 1 passed to NotORM_Result::update() must be of the type array, string php slim
- combine date time php
- current uri php[
- download html content from url php
- composer artisan
- alias migratedb='php artisan migrate'
- easiest way to setup a new laravel project
- command line read line by line
- Call to undefined function str_limit() laaravel8
- convert timestamp string to date php
- add days to date in php
- Customer::insert([ 'customer_name' => $request->customer_name, 'customer_mobile'=> $request->customer_mobile, 'user_id'=>Auth::user()->id, 'created_at'=> Carbon::now() ]);
- Allowed memory size of 1610612736 bytes exhausted (tried to allocate 4096 bytes) laravel
- cash clear laravel
- dd laravel query