Laravel Eloquent Cheatsheet

Model::create(array('key' => 'value'));  

Find first matching record by attributes or create

 Model::firstOrCreate(array('key' => 'value'));

Find first record by attributes or instantiate

 Model::firstOrNew(array('key' => 'value'));

Create or update a record matching attibutes, and fill with values

 Model::updateOrCreate(array('search_key' => 'search_value'), array('key' => 'value'));

Fill a model with an array of attributes, beware of mass assignment!

Model::fill($attributes);  
Model::destroy(1);  
Model::all();  
Model::find(1);  

Find using dual primary key

 Model::find(array('first', 'last'));

Throw an exception if the lookup fails

 Model::findOrFail(1);

Find using dual primary key and throw exception if the lookup fails

 Model::findOrFail(array('first', 'last'));
Model::where('foo', '=', 'bar')->get();  
Model::where('foo', '=', 'bar')->first();  

Find using relations

Model::whereHas('relation')->get();  
Model::with('relation')->where('relation.foo', 'bar')->get();  

Dynamic

 Model::whereFoo('bar')->first();

Throw an exception if the lookup fails

 Model::where('foo', '=', 'bar')->firstOrFail();
Model::where('foo', '=', 'bar')->count();  
Model::where('foo', '=', 'bar')->delete();  

php Output raw query

 Model::where('foo', '=', 'bar')->toSql();
Model::whereRaw('foo = bar and cars = 2', array(20))->get();  
Model::remember(5)->get();  
Model::remember(5, 'cache-key-name')->get();  
Model::cacheTags('my-tag')->remember(5)->get();  
Model::cacheTags(array('my-first-key','my-second-key'))->remember(5)->get();  
Model::on('connection-name')->find(1);  
Model::with('relation')->get();  
Model::all()->take(10);  
Model::all()->skip(10);  

Default Eloquent sort is ascendant

Model::orderBy('column')->get();  
Model::orderBy('column','desc')->get();  

Soft Delete

Model::withTrashed()->where('cars', 2)->get();  

Include the soft deleted models in the results

Model::withTrashed()->where('cars', 2)->restore();  
Model::where('cars', 2)->forceDelete();  

Force the result set to only included soft deletes

 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

Disables mass assignment exceptions from being thrown from model inserts and updates

 Eloquent::unguard();

Renables any ability to throw mass assignment exceptions

 Eloquent::reguard();