odel::create(['key' => 'value']);
Model::save($options = []);
// Fill a model with an array of attributes, beware of mass assignment!Model::fill($attributes);
// $min = duration in minutesModel::remember($min)->get();
Model::remember($min, 'cache-key-name')->get();
Model::cacheTags('my-tag')->remember($min)->get();
Model::cacheTags(['my-first-key', 'my-second-key'])->remember($min)->get();
Model::on('connection-name')->find($id);
Model::with('relation')->get();
// Adding a scope to a modelpublic function scopeTriplePeriod($query)
{
return $query->where('title', 'LIKE', '...%');
}
Model::triplePeriod()->get();
Fetch methods
// Retrieve row based on idModel::find($id1, $columns = ['*']);
Model::find([$id1, $id2], $columns = ['*']);
// Throw an exception if the lookup failsModel::findOrFail($id, $columns = ['*']);
Model::findOrFail([$id1, $id2], $columns = ['*']);
Model::all($columns = ['*']);
// Execute query and retrieve the result(s)Model::{query constraint}->get($columns = ['*']);
// Retrieve the row from the first of the resultsModel::first($columns = ['*']);
Model::{query constraint}->first($columns = ['*']);
// Retrieve the column from the first of the resultsModel::pluck('column');
Model::{query constraint}->pluck('column');
// Retrieve the column from ALL results. Optionaly with another column as it's keyModel::lists($column, $key = null);
Model::{query constraint}->lists($column, $key = null);
// The next four methods return the number of effected rowsModel::{query constraint}->update(['column' => 'value']);
Model::{query constraint}->delete();
Model::destroy($id1);
Model::destroy([$id1, $id2]);
Model::{query constraint}->count();
// Get the SQL representation of the queryModel::{query constraint}->toSql();
Query Constraints
// Below methods also have a prefixed 'or'-methodModel::where('column', '=', 'value')->get();
Model::whereRaw('column = ?', ['value']);
// Below three methods have a combination of a prefixed 'or' and a 'Not' in the middleModel::whereBetween('year', [2005, 2012])->get();
Model::whereIn('column', ['value1', 'value2'])->get();
Model::whereNull('column')->get();
// Direction: 'asc' or 'desc'Model::orderBy('column', $direction = 'asc')->get();
Model::take(10)->get();
Model::skip(5)->get();
// Nested 'where'-query'sModel::whereNested(function($query){
$query->where(...);
$query->orWhere(...);
})->get();
Model::orWhere(function($query){
$query->where(...);
$query->orWhere(...);
})->get();
// The below three methods return the sameModel::where('column', '=', 'value')->get();
Model::where('column', 'value')->get();
Model::whereColumn('value')->get();
Soft Delete
Model::withTrashed()->where('cars', 2)->get();
Model::withTrashed()->where('cars', 2)->restore();
Model::where('cars', 2)->forceDelete();
Model::onlyTrashed()->where('cars', 2)->get();
Events
Model::creating(function($model){});
Model::created(function($model){});
Model::updating(function($model){});
Model::updated(function($model){});
Model::saving(function($model){});
Model::saved(function($model){});
Model::deleting(function($model){});
Model::deleted(function($model){});
Model::observe(new FooObserver);
Eloquent Configuration
Eloquent::unguard();
// Disables mass assignment exceptions from being thrown from model inserts and updatesEloquent::reguard();
// Renables any ability to throw mass assignment exceptions
No comments:
Post a Comment