Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

Laravel model - CRUD only with records where one column = certain value

<?php
 
namespace AppScopes;
 
use IlluminateDatabaseEloquentBuilder;
use IlluminateDatabaseEloquentModel;
use IlluminateDatabaseEloquentScope;
 
class AdministrationScope implements Scope
{
    /**
     * Apply the scope to a given Eloquent query builder.
     *
     * @param  IlluminateDatabaseEloquentBuilder  $builder
     * @param  IlluminateDatabaseEloquentModel  $model
     * @return void
     */
    public function apply(Builder $builder, Model $model)
    {
        $builder->where('administration', '=', 2);
    }
}

<?php
 
  // model
namespace AppModels;
 
use AppScopesAdministrationScope;
use IlluminateDatabaseEloquentModel;
 
class YourModel extends Model
{
    /**
     * The "booted" method of the model.
     *
     * @return void
     */
    protected static function booted()
    {
        static::addGlobalScope(new AdministrationScope);
    }
}
 
PREVIOUS NEXT
Tagged: #Laravel #model #CRUD #records #column
ADD COMMENT
Topic
Name
4+4 =