MCQs Of Day CRUD 8 Operations with Eloquent

MCQs Of Day 8 CRUD 

Operations with Eloquent

 CRUD Operations with Eloquent - MCQs

1. What does CRUD stand for in database management?

a) Create, Read, Update, Delete
b) Connect, Retrieve, Update, Delete
c) Create, Return, Update, Delete
d) Collect, Read, Update, Delete

Answer: a) Create, Read, Update, Delete
Explanation: CRUD is a set of operations that stand for Create, Read, Update, and Delete, which are basic functions used in databases to manage records.


2. Which Laravel method is used to create a new record in a database?

a) create()
b) store()
c) insert()
d) add()

Answer: a) create()
Explanation: The create() method is used in Laravel's Eloquent ORM to create a new record in the database.


3. Which of the following is true about Eloquent ORM in Laravel?

a) It allows only raw SQL queries.
b) It does not support relationships between models.
c) It provides an active record implementation for working with the database.
d) It is used only for Read operations.

Answer: c) It provides an active record implementation for working with the database.
Explanation: Eloquent ORM is an implementation of the Active Record pattern, allowing you to interact with the database using models, rather than writing raw SQL queries.


4. What is the purpose of the find() method in Eloquent?

a) To find a record by its primary key.
b) To find all records that match a condition.
c) To create a new record.
d) To update an existing record.

Answer: a) To find a record by its primary key.
Explanation: The find() method in Eloquent is used to retrieve a record by its primary key (usually id).


5. How do you update an existing record in Eloquent?

a) Using the save() method after modifying the model's attributes.
b) Using the update() method directly.
c) Using the edit() method.
d) Using the modify() method.

Answer: a) Using the save() method after modifying the model's attributes.
Explanation: After retrieving the record using methods like find(), you can update its attributes and then call the save() method to persist the changes.


6. Which Eloquent method is used to delete a record?

a) delete()
b) remove()
c) destroy()
d) erase()

Answer: a) delete()
Explanation: The delete() method is used to remove a record from the database in Eloquent.


7. How do you retrieve all records from a database table using Eloquent?

a) find()
b) all()
c) select()
d) get()

Answer: b) all()
Explanation: The all() method retrieves all records from the table associated with the model.


8. What is the purpose of $fillable in Eloquent models?

a) To specify which attributes can be mass-assigned.
b) To specify which attributes are required.
c) To specify the model's table name.
d) To specify the primary key field.

Answer: a) To specify which attributes can be mass-assigned.
Explanation: The $fillable property defines which fields can be mass-assigned, preventing unwanted or malicious data from being inserted.


9. Which of the following is the correct syntax to use updateOrCreate()?

a) Model::updateOrCreate(condition, updateData)
b) Model::updateOrCreate(condition, createData)
c) Model::findOrUpdate(condition, updateData)
d) Model::createOrUpdate(condition, updateData)

Answer: a) Model::updateOrCreate(condition, updateData)
Explanation: The updateOrCreate() method will attempt to find a record with the given condition and update it; if it doesn't exist, it will create a new record with the provided data.


10. How do you retrieve records in Eloquent with conditions?

a) where()
b) find()
c) all()
d) select()

Answer: a) where()
Explanation: The where() method is used in Eloquent to add conditions to queries.


11. How do you update multiple fields of a model in Eloquent?

a) update()
b) updateFields()
c) massUpdate()
d) save()

Answer: a) update()
Explanation: The update() method is used to update multiple fields in a model and save the changes to the database.


12. What does the withTrashed() method in Eloquent do?

a) Restores a soft-deleted record.
b) Includes soft-deleted records in the query result.
c) Deletes records permanently.
d) Prevents soft-deleted records from being retrieved.

Answer: b) Includes soft-deleted records in the query result.
Explanation: The withTrashed() method allows you to include soft-deleted records in your query results.


13. Which of the following methods is used for performing a soft delete in Eloquent?

a) delete()
b) softDelete()
c) forceDelete()
d) destroy()

Answer: a) delete()
Explanation: The delete() method is used to soft-delete records when the model is set up to use the SoftDeletes trait.


14. Which method is used to restore a soft-deleted record in Laravel?

a) restore()
b) recover()
c) undoDelete()
d) reactivate()

Answer: a) restore()
Explanation: The restore() method is used to restore a soft-deleted record.


15. What is the pluck() method used for in Eloquent?

a) It retrieves specific columns from the database.
b) It updates records in bulk.
c) It deletes a specific record.
d) It fetches all columns for a model.

Answer: a) It retrieves specific columns from the database.
Explanation: The pluck() method is used to retrieve specific columns from the database, typically returning an array of values.


16. How can you retrieve a specific user's name by their email using Eloquent?

a) User::find('email')
b) User::where('email', 'user@example.com')->first()
c) User::email('user@example.com')
d) User::select('name')->where('email', 'user@example.com')

Answer: b) User::where('email', 'user@example.com')->first()
Explanation: The where() method adds a condition, and first() retrieves the first matching record.


17. Which of the following methods returns the number of records in a table?

a) count()
b) total()
c) records()
d) findAll()

Answer: a) count()
Explanation: The count() method is used to return the number of records in the table.


18. What is the default table name for an Eloquent model named Post?

a) posts
b) post
c) post_table
d) posts_table

Answer: a) posts
Explanation: Eloquent automatically assumes the plural form of the model name as the table name. Therefore, the table name for the Post model would be posts.


19. What is the role of the $guarded property in an Eloquent model?

a) It specifies which attributes should be mass-assigned.
b) It specifies which attributes should not be mass-assigned.
c) It defines the table name.
d) It defines the primary key of the table.

Answer: b) It specifies which attributes should not be mass-assigned.
Explanation: The $guarded property is used to specify which attributes should not be mass-assigned, as opposed to $fillable, which specifies the allowed attributes.


20. Which of the following methods retrieves the first record that matches the given condition?

a) get()
b) first()
c) find()
d) fetch()

Answer: b) first()
Explanation: The first() method retrieves the first record that matches the given condition. It returns null if no records match.


21. How do you get the last inserted record in Eloquent?

a) last()
b) recent()
c) orderBy('id', 'desc')->first()
d) latest()

Answer: c) orderBy('id', 'desc')->first()
Explanation: You can use orderBy() to sort the records by ID in descending order and then use first() to get the most recently inserted record.


22. Which method would you use to find records by their primary key in Laravel?

a) find()
b) first()
c) retrieve()
d) get()

Answer: a) find()
Explanation: The find() method is used to retrieve records by their primary key, which is usually id.


23. How do you retrieve all users who were created after a specific date in Eloquent?

a) User::createdAfter('2024-01-01')
b) User::where('created_at', '>', '2024-01-01')
c) User::where('created', '>', '2024-01-01')
d) User::created()->after('2024-01-01')

Answer: b) User::where('created_at', '>', '2024-01-01')
Explanation: The where() method allows you to filter records based on conditions like date comparisons.


(Continued in the next response due to character limit...)

4o mini

You said:

more

ChatGPT said:

ChatGPT

Here are more MCQs related to CRUD operations and Eloquent ORM in Laravel:


24. How can you retrieve records from a database sorted by a specific column in Laravel?

a) orderBy('column')
b) sortBy('column')
c) fetchOrderBy('column')
d) sortedBy('column')

Answer: a) orderBy('column')
Explanation: The orderBy() method is used in Eloquent to sort records by a specific column.


25. Which of the following methods is used to delete a record permanently in Eloquent after it has been soft-deleted?

a) delete()
b) forceDelete()
c) remove()
d) softDelete()

Answer: b) forceDelete()
Explanation: The forceDelete() method is used to permanently delete a soft-deleted record from the database.


26. How do you paginate records using Eloquent?

a) paginate()
b) paginateRecords()
c) limit()
d) chunk()

Answer: a) paginate()
Explanation: The paginate() method is used to retrieve records in a paginated format, allowing for easier navigation of large datasets.


27. Which of the following is used to update a record with new data in Laravel without retrieving the model?

a) update()
b) save()
c) updateOrCreate()
d) create()

Answer: a) update()
Explanation: The update() method allows you to update a record with new data without needing to retrieve the model first.


28. What does the destroy() method do in Laravel Eloquent?

a) It restores soft-deleted records.
b) It updates a record in the database.
c) It deletes records based on their primary key.
d) It returns records matching specific conditions.

Answer: c) It deletes records based on their primary key.
Explanation: The destroy() method is used to delete records by their primary key.


29. How do you ensure a specific model can only be accessed by users with certain roles?

a) Using Auth::user()->role to check access.
b) Using authorize() method in controllers.
c) Defining model relationships.
d) Using permissions() in routes.

Answer: b) Using authorize() method in controllers.
Explanation: The authorize() method can be used to check permissions for a model based on user roles before allowing access to specific actions.


30. How can you prevent mass-assignment vulnerabilities in Eloquent?

a) By using $fillable property.
b) By using $guarded property.
c) By using $timestamps property.
d) Both a and b.

Answer: d) Both a and b.
Explanation: You can prevent mass-assignment vulnerabilities by using the $fillable property to specify which attributes can be mass-assigned or the $guarded property to specify which attributes cannot be mass-assigned.


31. Which of the following is used to define relationships between Eloquent models?

a) relationship()
b) hasMany(), belongsTo(), hasOne(), belongsToMany()
c) connect()
d) relations()

Answer: b) hasMany(), belongsTo(), hasOne(), belongsToMany()
Explanation: Eloquent provides methods like hasMany(), belongsTo(), and others to define relationships between models.


32. What is the with() method used for in Eloquent?

a) To fetch related models (eager loading).
b) To define model attributes.
c) To update a record in the database.
d) To retrieve specific columns.

Answer: a) To fetch related models (eager loading).
Explanation: The with() method is used to eager load relationships, reducing the number of queries needed to fetch related models.


33. Which method is used to add a condition to an Eloquent query for checking if a column is null?

a) isNull()
b) whereNull()
c) nullWhere()
d) checkNull()

Answer: b) whereNull()
Explanation: The whereNull() method adds a condition to the query to check if a column is NULL.


34. How do you count the number of records in a specific relationship in Eloquent?

a) count()
b) number()
c) total()
d) countRecords()

Answer: a) count()
Explanation: The count() method is used to get the number of related records in an Eloquent relationship.


35. How do you retrieve only distinct records using Eloquent?

a) distinct()
b) unique()
c) filter()
d) getDistinct()

Answer: a) distinct()
Explanation: The distinct() method retrieves only unique records from the database.


36. What is the default value of the primary key field in Laravel models?

a) id
b) primary_key
c) key_id
d) record_id

Answer: a) id
Explanation: By default, Eloquent assumes the primary key of the table is id.


37. What is the purpose of $timestamps property in an Eloquent model?

a) It defines whether a model should be timestamped.
b) It defines the format of timestamps.
c) It tracks the update timestamp.
d) It prevents timestamps from being added to the model.

Answer: a) It defines whether a model should be timestamped.
Explanation: The $timestamps property indicates whether the model should be automatically timestamped with created_at and updated_at fields.


38. What does the firstOrCreate() method do in Eloquent?

a) It updates a record if found, or creates a new record if not.
b) It retrieves the first matching record.
c) It creates a new record only if the specified condition is true.
d) It retrieves all records matching a condition.

Answer: a) It updates a record if found, or creates a new record if not.
Explanation: The firstOrCreate() method finds the first record matching the condition or creates it if it doesn't exist.


39. Which method would you use to retrieve multiple models by their primary keys?

a) find()
b) findMany()
c) whereIn()
d) retrieve()

Answer: b) findMany()
Explanation: The findMany() method allows you to retrieve multiple records based on their primary keys.


40. How do you handle many-to-many relationships in Eloquent?

a) By using hasMany() and belongsTo() methods.
b) By using hasManyThrough() method.
c) By using belongsToMany() method.
d) By using hasMany() and hasOne() methods.

Answer: c) By using belongsToMany() method.
Explanation: The belongsToMany() method is used to define many-to-many relationships between two models.


41. Which method is used to update a record in a many-to-many relationship table in Laravel?

a) update()
b) save()
c) sync()
d) attach()

Answer: c) sync()
Explanation: The sync() method is used in many-to-many relationships to update the pivot table by syncing related records.


42. Which of the following methods is used for eager loading related models in Laravel?

a) load()
b) with()
c) findWith()
d) eagerLoad()

Answer: b) with()
Explanation: The with() method is used to eager load related models, reducing the number of queries.


43. What is the function of the chunk() method in Eloquent?

a) It retrieves records in chunks to prevent memory overflow.
b) It retrieves all records at once.
c) It filters records based on a condition.
d) It updates records in chunks.

Answer: a) It retrieves records in chunks to prevent memory overflow.
Explanation: The chunk() method retrieves large datasets in smaller chunks to avoid memory issues when processing large amounts of data.


44. How would you retrieve all posts published in the last month in Eloquent?

a) Post::where('created_at', '>', now()->subMonth())
b) Post::lastMonth()
c) Post::where('published_at', '>', now()->subMonth())
d) Post::where('created_at', '>=', now()->subMonth())

Answer: a) Post::where('created_at', '>', now()->subMonth())
Explanation: You can use the where() method to filter records based on the created_at column, using now()->subMonth() to get records created in the last month.


45. Which method is used to add a new related record to a one-to-many relationship in Eloquent?

a) add()
b) save()
c) attach()
d) create()

Answer: b) save()
Explanation: The save() method is used to associate a new related record in a one-to-many relationship.


That covers more MCQs related to CRUD operations and Eloquent ORM. Let me know if you'd like further explanations or more questions!

4o

You said:

more

ChatGPT said:

ChatGPT

Here are additional MCQs related to CRUD operations and Eloquent ORM in Laravel:


46. What does the updateOrCreate() method in Eloquent do?

a) It creates a new record only if the conditions are met.
b) It updates the record if it exists, otherwise creates a new one.
c) It creates multiple new records.
d) It finds and deletes a record.

Answer: b) It updates the record if it exists, otherwise creates a new one.
Explanation: The updateOrCreate() method either updates an existing record based on the given conditions or creates a new record if none is found.


47. How can you force Eloquent to perform an SQL query instead of using the Eloquent model methods?

a) Use the DB::query() method.
b) Use the select() method on the model.
c) Use the raw() method in a query.
d) Use the toSql() method.

Answer: a) Use the DB::query() method.
Explanation: The DB::query() method allows you to directly execute SQL queries bypassing the Eloquent ORM.


48. How do you retrieve only specific columns for a model in Eloquent?

a) select('column1', 'column2')
b) columns('column1', 'column2')
c) fetch(['column1', 'column2'])
d) only('column1', 'column2')

Answer: a) select('column1', 'column2')
Explanation: The select() method is used to retrieve only specific columns for a model.


49. Which Eloquent method can be used to join a related model's table?

a) join()
b) has()
c) with()
d) hasManyThrough()

Answer: c) with()
Explanation: The with() method is used to eager load relationships, effectively joining the related model’s table.


50. How can you retrieve all records where a column's value is greater than a certain number?

a) where('column', '>', value)
b) greaterThan('column', value)
c) filter('column', '>', value)
d) find('column', '>', value)

Answer: a) where('column', '>', value)
Explanation: The where() method with the > operator allows you to retrieve records where the column value is greater than a specified value.


51. How would you add a "soft delete" feature to an Eloquent model?

a) By using the SoftDeletes trait.
b) By adding a deleted_at column to the table manually.
c) Both a and b.
d) Using the delete() method directly.

Answer: c) Both a and b.
Explanation: To enable soft deletes, you need to add a deleted_at column in your table and use the SoftDeletes trait in the model.


52. What does the exists() method check in Eloquent?

a) It checks if a record is saved in the database.
b) It checks if the record exists in the collection.
c) It checks if a record is in the cache.
d) It checks if a model instance has been deleted.

Answer: b) It checks if the record exists in the collection.
Explanation: The exists() method is used to check if a model instance exists in the database or collection.


53. Which of the following is used to define a "one-to-one" relationship in Laravel Eloquent?

a) hasOne()
b) belongsTo()
c) hasMany()
d) hasOneThrough()

Answer: a) hasOne()
Explanation: The hasOne() method is used to define a one-to-one relationship between models.


54. How can you prevent a model from using timestamps?

a) By setting $timestamps = false;
b) By setting $timestamps = true;
c) By using $timestamps = 'false';
d) By using $timestamps = null;

Answer: a) By setting $timestamps = false;
Explanation: The $timestamps property can be set to false to prevent Laravel from automatically managing created_at and updated_at columns.


55. What is the difference between first() and get() in Eloquent?

a) first() retrieves only the first record; get() retrieves all records.
b) first() retrieves all records; get() retrieves only the first record.
c) first() retrieves the latest record; get() retrieves the oldest record.
d) first() is used for creating records; get() is used for reading records.

Answer: a) first() retrieves only the first record; get() retrieves all records.
Explanation: The first() method retrieves only the first record that matches the query, whereas get() retrieves all matching records.


56. How do you delete multiple records in Eloquent?

a) By using delete() with conditions.
b) By using remove() method.
c) By using destroy() with an array of IDs.
d) By using softDelete().

Answer: a) By using delete() with conditions.
Explanation: The delete() method can be used with conditions to delete multiple records from the database.


57. Which method allows you to update multiple records in Eloquent?

a) update()
b) batchUpdate()
c) updateMany()
d) massUpdate()

Answer: a) update()
Explanation: The update() method is used to update existing records in Eloquent. It can be used with conditions to update multiple records at once.


58. What does the pluck() method in Eloquent return?

a) It returns the first record of the query.
b) It returns a single column's values.
c) It returns the raw SQL query.
d) It returns the count of records.

Answer: b) It returns a single column's values.
Explanation: The pluck() method is used to retrieve a single column’s values from a collection or query result.


59. How would you delete a model using Eloquent by its primary key?

a) Model::delete(1)
b) Model::destroy(1)
c) Model::find(1)->delete()
d) Both b and c

Answer: d) Both b and c
Explanation: Both destroy() and the find() method followed by delete() can be used to delete a record by its primary key.


60. How can you restore a soft-deleted model in Laravel?

a) By using restore() method.
b) By using revert() method.
c) By using recover() method.
d) By using reopen() method.

Answer: a) By using restore() method.
Explanation: The restore() method is used to restore a soft-deleted model to its original state.


61. How do you retrieve all records where a specific column is equal to a given value in Eloquent?

a) where('column', '=', value)
b) filter('column', '=', value)
c) find('column', value)
d) columnEqualTo('value')

Answer: a) where('column', '=', value)
Explanation: The where() method is used to filter records where a specific column equals a given value.


62. What is the purpose of the has() method in Eloquent?

a) To count related records.
b) To filter results based on a relationship.
c) To eager load relationships.
d) To update related records.

Answer: b) To filter results based on a relationship.
Explanation: The has() method is used to filter results based on the existence of a relationship.


63. How do you retrieve all models of a specific type in Eloquent?

a) Model::get()
b) Model::all()
c) Model::findAll()
d) Model::fetch()

Answer: b) Model::all()
Explanation: The all() method retrieves all models of a specific type from the database.


64. How can you retrieve related models from a one-to-many relationship in Eloquent?

a) By using the has() method.
b) By using the with() method.
c) By using the belongsTo() method.
d) By using the hasMany() method.

Answer: d) By using the hasMany() method.
Explanation: The hasMany() method is used to define a one-to-many relationship, allowing you to retrieve related models.


Let me know if you'd like to continue further or if you need additional explanations!

4o

You said:

more

ChatGPT said:

ChatGPT

Here are more MCQs related to CRUD operations and Eloquent ORM in Laravel:


65. How do you prevent a field from being mass-assigned in Eloquent?

a) By setting $guarded to the field name.
b) By setting $fillable to the field name.
c) By excluding the field from $casts.
d) By setting $hidden to the field name.

Answer: a) By setting $guarded to the field name.
Explanation: The $guarded property is used to specify which fields should not be mass-assigned in Eloquent models.


66. What is the default method used by Eloquent to retrieve records?

a) fetch()
b) get()
c) select()
d) all()

Answer: b) get()
Explanation: The get() method is the default method used by Eloquent to retrieve records from the database.


67. How can you get the first matching record from the database using Eloquent?

a) first()
b) find()
c) firstOrFail()
d) getFirst()

Answer: a) first()
Explanation: The first() method is used to retrieve the first matching record from the database based on the conditions provided.


68. Which method is used to create a new record in the database with Eloquent?

a) create()
b) save()
c) insert()
d) add()

Answer: a) create()
Explanation: The create() method is used to create a new record in the database with the attributes provided.


69. How do you use Eloquent to update a specific record by its ID?

a) Model::update(['id' => value])
b) Model::find(id)->update(['field' => value])
c) Model::set(id)->update(['field' => value])
d) Model::edit(id)->update(['field' => value])

Answer: b) Model::find(id)->update(['field' => value])
Explanation: The find(id) method retrieves the record by its primary key, and update() modifies the specified fields.


70. How would you retrieve the most recent records in Eloquent?

a) orderBy('created_at', 'desc')
b) latest()
c) newest()
d) reverseOrder()

Answer: b) latest()
Explanation: The latest() method is a shorthand for ordering results by the created_at column in descending order.


71. How do you retrieve all models that belong to a specific user in a one-to-many relationship?

a) User::find(1)->posts()
b) User::find(1)->hasMany(Post::class)
c) User::find(1)->posts()->get()
d) Post::find(1)->user()

Answer: c) User::find(1)->posts()->get()
Explanation: This will retrieve all posts that belong to the user with ID 1 in a one-to-many relationship.


72. What does the pluck() method do in Eloquent?

a) Retrieves all columns.
b) Retrieves specific columns as an array.
c) Retrieves related models.
d) Retrieves the first record in a collection.

Answer: b) Retrieves specific columns as an array.
Explanation: The pluck() method is used to retrieve a single column's values as an array.


73. What is the purpose of the increment() method in Eloquent?

a) To increase a column's value by a specified amount.
b) To increment the total number of records in a table.
c) To update multiple records at once.
d) To calculate a total value of a column.

Answer: a) To increase a column's value by a specified amount.
Explanation: The increment() method allows you to increment a column's value by a given amount in the database.


74. Which method in Eloquent can be used to check if a model exists?

a) exists()
b) isExist()
c) find()
d) hasRecord()

Answer: a) exists()
Explanation: The exists() method checks whether a model exists in the database.


75. What is the purpose of the delete() method in Eloquent?

a) It permanently deletes a record from the database.
b) It deletes all records from the table.
c) It soft deletes a record.
d) It clears the cache for a model.

Answer: a) It permanently deletes a record from the database.
Explanation: The delete() method is used to permanently remove a record from the database. For soft deletes, use softDelete().


76. What method would you use to retrieve records from a table where the value of a column is like a certain pattern?

a) whereLike()
b) where()
c) like()
d) where('column', 'like', 'pattern')

Answer: d) where('column', 'like', 'pattern')
Explanation: The where() method can be used with the like operator to search for records matching a given pattern.


77. Which method is used to create a new instance of an Eloquent model but does not save it to the database?

a) create()
b) save()
c) new()
d) make()

Answer: c) new()
Explanation: The new() method is used to create a new instance of a model without saving it to the database immediately.


78. What does the hasMany() method define in an Eloquent relationship?

a) A one-to-many relationship from the "many" side.
b) A one-to-many relationship from the "one" side.
c) A many-to-many relationship.
d) A one-to-one relationship.

Answer: b) A one-to-many relationship from the "one" side.
Explanation: The hasMany() method defines a one-to-many relationship from the "one" side (i.e., a single model has many related models).


79. How can you add a "created_at" and "updated_at" timestamp automatically in Eloquent models?

a) Set $timestamps = true;
b) Set $timestamps = false;
c) Laravel does this automatically if the columns exist in the database.
d) Use createTimestamps() method in the migration.

Answer: c) Laravel does this automatically if the columns exist in the database.
Explanation: By default, Laravel automatically manages created_at and updated_at timestamps if these columns are present in the table.


80. How do you fetch the first matching record or fail if not found in Eloquent?

a) firstOrFail()
b) findOrFail()
c) first()
d) getOrFail()

Answer: b) findOrFail()
Explanation: The findOrFail() method retrieves the first matching record based on the primary key or throws a ModelNotFoundException if no record is found.


81. Which Eloquent method is used to eager load relationships in a query?

a) load()
b) with()
c) loadRelationships()
d) loadAll()

Answer: b) with()
Explanation: The with() method is used to eager load relationships in a query, thus reducing the number of database queries.


82. How would you perform a "LIKE" search on a column in Eloquent?

a) where('column', 'like', '%value%')
b) whereLike('column', '%value%')
c) search('column', '%value%')
d) filter('column', '%value%')

Answer: a) where('column', 'like', '%value%')
Explanation: The where() method with the like operator is used to perform a "LIKE" search for partial matches.


83. How do you retrieve all records sorted by a specific column in ascending order in Eloquent?

a) orderBy('column', 'desc')
b) orderBy('column', 'asc')
c) sortedBy('column')
d) ascending('column')

Answer: b) orderBy('column', 'asc')
Explanation: The orderBy() method allows you to sort results by a specific column. 'asc' sorts in ascending order.


84. How do you retrieve a model by its primary key with Eloquent?

a) find()
b) fetch()
c) getByKey()
d) selectById()

Answer: a) find()
Explanation: The find() method is used to retrieve a model by its primary key.


85. Which of the following methods can be used to paginate results in Eloquent?

a) paginate()
b) limit()
c) page()
d) take()

Answer: a) paginate()
Explanation: The paginate() method is used to paginate query results in Eloquent. It divides results into pages.


Let me know if you'd like more!

4o

You said:

more

ChatGPT said:

ChatGPT

Here are additional MCQs related to CRUD operations and Eloquent ORM in Laravel:


86. What is the correct syntax to delete a model instance using Eloquent?

a) Model::delete()
b) Model::destroy()
c) $model->delete()
d) Model::remove()

Answer: c) $model->delete()
Explanation: The correct syntax for deleting a model instance is by calling the delete() method on an instance of the model.


87. How can you define a one-to-many relationship in Laravel Eloquent?

a) hasOne()
b) hasMany()
c) belongsTo()
d) has()

Answer: b) hasMany()
Explanation: The hasMany() method defines a one-to-many relationship, where a single model can be associated with multiple related models.


88. How do you update a specific column of a model using Eloquent?

a) $model->column_name = value;
b) Model::updateColumn('column_name', value);
c) $model->setColumn('column_name', value);
d) Model::update(['column_name' => value]);

Answer: a) $model->column_name = value;
Explanation: To update a specific column, you directly assign the value to the model’s attribute and then call the save() method.


89. How do you retrieve all records from a model’s table using Eloquent?

a) Model::get()
b) Model::all()
c) Model::fetch()
d) Model::list()

Answer: b) Model::all()
Explanation: The all() method retrieves all records from the model’s associated database table.


90. How do you specify which fields can be mass-assigned in Eloquent?

a) By using the $fillable property.
b) By using the $guarded property.
c) By using the $columns property.
d) By using the $massAssign property.

Answer: a) By using the $fillable property.
Explanation: The $fillable property defines which fields in the model are allowed to be mass-assigned.


91. What method would you use to delete a model by its primary key in Laravel?

a) Model::deleteById()
b) Model::destroy()
c) Model::removeById()
d) Model::remove()

Answer: b) Model::destroy()
Explanation: The destroy() method deletes the record by its primary key.


92. How would you retrieve the last inserted record in a model using Eloquent?

a) Model::last()
b) Model::latest()->first()
c) Model::getLast()
d) Model::lastInserted()

Answer: b) Model::latest()->first()
Explanation: The latest() method orders the records by the created_at field in descending order, and first() retrieves the last record.


93. How do you retrieve only specific columns for a model in Eloquent?

a) Model::select('column1', 'column2')
b) Model::only('column1', 'column2')
c) Model::fields('column1', 'column2')
d) Model::columns('column1', 'column2')

Answer: a) Model::select('column1', 'column2')
Explanation: The select() method allows you to specify which columns to retrieve from the database.


94. How can you delete multiple records at once in Eloquent?

a) Model::where('column', value)->delete()
b) Model::destroy()
c) Model::bulkDelete()
d) Model::removeAll()

Answer: a) Model::where('column', value)->delete()
Explanation: You can use the where() method to filter records and then call delete() to remove them.


95. How do you check if a model exists in Eloquent?

a) Model::exists()
b) Model::findOrFail()
c) $model->exists()
d) $model->checkExist()

Answer: c) $model->exists()
Explanation: The exists property of the model will return true if the model exists in the database and false otherwise.


96. Which method is used to update multiple records at once using Eloquent?

a) Model::bulkUpdate()
b) Model::updateMany()
c) Model::update()
d) Model::where()->update()

Answer: d) Model::where()->update()
Explanation: You can use the where() method to filter records and then chain the update() method to modify multiple records at once.


97. What will happen if you try to update a model that doesn't exist in the database?

a) An error is thrown.
b) The model will be created with default values.
c) The model will be updated with null values.
d) The save() method will fail silently.

Answer: a) An error is thrown.
Explanation: If you attempt to update a model that doesn't exist, Laravel will throw a ModelNotFoundException.


98. Which method in Eloquent is used to perform a "LIKE" search query?

a) whereLike()
b) like()
c) where('column', 'LIKE', 'pattern')
d) findLike()

Answer: c) where('column', 'LIKE', 'pattern')
Explanation: The where() method combined with the LIKE operator is used to perform pattern matching in queries.


99. How can you paginate results in Eloquent?

a) Model::paginate(10)
b) Model::limit(10)
c) Model::page(10)
d) Model::take(10)

Answer: a) Model::paginate(10)
Explanation: The paginate() method retrieves the records in pages, allowing you to specify the number of records per page.


100. What method would you use to prevent a specific column from being included in mass assignment in Eloquent?

a) $guarded
b) $fillable
c) $hidden
d) $excluded

Answer: a) $guarded
Explanation: The $guarded property is used to specify which columns should not be mass-assigned. You can list fields you don't want to be mass-assigned.


 


Comments

Popular posts from this blog

Lecture Notes Of Day 24: Laravel Policies

MCQs of Day 7 Eloquent ORM Basics