Eloquent Flashcards
What does MVC stand for?
Model View Controller
What is the definition of ActiveRecord?
An architectural pattern found in software that stores in–memory object data in relational databases.
What does ORM stand for?
Object–relational Mapping
What is the definition of Object–Relational Mapping?
In computer science is a programming technique for converting data between incompatible type systems in object–oriented programming languages.
Which Laravel feature utilizes is ORM?
Eloquent
When does a Mass Assignment error occur?
A Mass Assignment vulnerability occurs unless you specify either a fillable or guarded attribute on the model.
What is the result of using Soft Delete on a model?
When models are soft deleted, they are not actually removed from your database. Instead, a deleted_at attribute is set on the model and inserted into the database.
How would you Mass Update records?
Any number of models that match a given query can be updated:
App\Flight::where(‘active’, 1)–>where(‘destination’, ‘San Diego’)–>update([‘delayed’ => 1]);
What is the simplest way to create a record using eloquent?
To create a new record in the database, simply create a new model instance, set attributes on the model, then call the save method:
$flight = new Flight;
$flight–>name = $request–>name;
$flight–>save();
What are the Aggregates methods provided by query builder?
count(), max(), min(), avg() and sum()
What Eloquent methods will throw a Not Found Exception if the record is not found?
findOrFail() and firstOrFail()
What Eloquent methods will retrieve a single record?
find() and first()
What is the purpose of the Eloquent cursor() method?
The cursor method allows you to iterate through your database records using a cursor, which will only execute a single query. When processing large amounts of data, the cursor method may be used to greatly reduce your memory usage.
foreach (Flight::where('foo', 'bar')–>cursor() as $flight) { // }
What is the purpose of the Eloquent chunk() method?
The chunk method will retrieve a chunk of Eloquent models, feeding them to a given Closure for processing. Using the chunk method will conserve memory when working with large result sets.
Flight::chunk(200, function ($flights) { foreach ($flights as $flight) { // } });
What is the purpose of the Eloquent delete() method?
To delete a model, call the delete method on a model instance or if you know the primary key of the model, you may delete the model without retrieving it:
$flight = App\Flight::find(1);
$flight–>delete();
Or
App\Flight::destroy([1, 2, 3]);
How would you Mass Delete records?
Multiple models can be deleted if they match the given query:
App\Flight::where(‘active’, 0)–>delete();
What is the purpose of the save() method?
The save method may also be used to update models that already exist in the database:
$flight = App\Flight::find(1);
$flight–>name = ‘New Flight Name’;
$flight–>save();
Perhaps you need to insert a new Comment for a Post model. Instead of manually setting the post_id attribute on the Comment, you may insert the Comment directly from the relationship’s save method:
$comment = new App\Comment([
‘message’ => ‘A new comment.’
]);
$post = App\Post::find(1);
$post–>comments()–>save($comment);
What is the purpose of the Model variable $fillable?
$fillable serves as a white list of attributes that should be mass assignable on a model
What is the purpose of the Model variable $guarded?
The $guarded property should contain an array of attributes that you do not want to be mass assignable. All other attributes not in the array will be mass assignable. So, $guarded functions like a black list.
What are the other creation methods?
firstOrCreate() , firstOrNew() and updateOrCreate()
What is the purpose of the Eloquent firstOrCreate() method?
The firstOrCreate() method will attempt to locate a database record using the given column / value pairs. If the model can not be found in the database, a record will be inserted with the given attributes.
What is the purpose of the Eloquent firstOrNew() method?
The firstOrNew method, like firstOrCreate will attempt to locate a record in the database matching the given attributes. However, if a model is not found, a new model instance will be returned. Note that the model returned by firstOrNew has not yet been persisted to the database. You will need to call save manually to persist it.
What is the purpose of the Eloquent updateOrCreate() method?
Update an existing model or create a new model if none exists.
Like the firstOrCreate method, updateOrCreate persists the model, so there’s no need to call save().
What is the purpose of the Eloquent withTrashed() method?
You may force soft deleted models to appear in a result set using the withTrashed method on the query:
$flights = App\Flight::withTrashed()–>where(‘account_id’, 1)–>get();
Or
$flight–>history()–>withTrashed()–>get();