Unit 6 - Practice Quiz
1 Which Artisan command is used to generate a new Eloquent model in Laravel?
php artisan create:model ModelName
php artisan make:model ModelName
php artisan new:model ModelName
php artisan generate:model ModelName
2
By default, what table name will Laravel assume for a model named Flight?
flight
flights
flight_table
tbl_flights
3 If your database table does not follow the default naming convention, which property must be defined in the Model to specify the table name?
protected $table
public $tableName
private $db_table
protected $source
4 What is the primary purpose of Database Migrations in an MVC framework like Laravel?
5
Which method in a migration file is used to reverse the operations performed by the up method?
back()
reverse()
down()
rollback()
6 Which Artisan command executes all outstanding migrations?
php artisan migrate:run
php artisan db:migrate
php artisan migrate
php artisan schema:update
7 In the context of the Query Builder, which method is used to retrieve all rows from a table?
all()
get()
fetch()
retrieve()
8
When using Query Builder, how do you prevent SQL injection when adding a WHERE clause?
raw() method only
9
What is the result of the following Query Builder code?
$user = DB::table('users')->where('name', 'John')->first();
stdClass object representing the record
10 Which property in an Eloquent model allows you to specify which attributes are mass assignable?
$guarded
$fillable
$visible
$hidden
11 Conversely, which property acts as a blacklist for mass assignment in Eloquent?
$guarded
$fillable
$protected
$blocked
12 Which of the following describes the syntax to create a new record using Eloquent Mass Assignment?
User::add(['name' => 'John']);
User::create(['name' => 'John']);
User::insert(['name' => 'John']);
User::make(['name' => 'John'])->save();
13 What is the purpose of Database Seeding?
14 Which Artisan command creates a new Seeder class?
php artisan make:seeder Name
php artisan create:seed Name
php artisan db:seed Name
php artisan new:seeder Name
15 When using Laravel with MongoDB, which library is commonly used to provide Eloquent-like functionality?
laravel/mongo-connector
jenssegers/mongodb
doctrine/mongodb
illuminate/nosql
16
In a MongoDB Eloquent model, what class should the Model extend instead of Illuminate\Database\Eloquent\Model?
MongoDB\Model
Jenssegers\Mongodb\Eloquent\Model
Illuminate\Database\NoSQL\Model
Mongo\Eloquent
17 What is the default primary key field name in MongoDB?
id
_id
uuid
key
18 How do you handle migrations for MongoDB in Laravel given it is schema-less?
19 Which Eloquent method is used to retrieve a model by its primary key or throw an exception if not found?
find()
firstOrFail()
findOrFail()
getOrFail()
20
In a REST API implemented with Laravel, which HTTP method corresponds to the update action?
21 When building an API, what is the standard HTTP status code for a successful resource creation?
22 To transform Eloquent models into a specific JSON format for an API, what Laravel feature is recommended?
23 Which file is specifically designated for defining API routes in a standard Laravel installation?
routes/web.php
routes/api.php
routes/console.php
app/Http/routes.php
24 In Eloquent, how do you define a Soft Delete?
SoftDeletes trait in the model.
$softDelete = true.
deleted = 1 column manually.
delete:soft command.
25
What is the equivalent of an SQL JOIN in Eloquent ORM?
26 Which command would you use to rollback the very last migration operation?
php artisan migrate:reset
php artisan migrate:refresh
php artisan migrate:rollback
php artisan db:undo
27 When using the Query Builder, which method creates a raw expression that is injected into the query as-is?
DB::raw()
DB::inject()
DB::sql()
DB::text()
28 In an API Controller, what method is conventionally used to show a single specific resource?
index()
store()
show($id)
create()
29 Which of the following is correct to delete a user with ID 1 using Eloquent?
User::delete(1);
User::destroy(1);
User::remove(1);
User::find(1)->kill();
30
If you are using MongoDB with Laravel, which operator is often used within where clauses for complex comparisons?
'in'
31
What does the timestamps() method create in a migration file?
date_created and date_modified columns
created_at and updated_at columns
timestamp column
start and end columns
32
In Eloquent, what is the default format for the $timestamps properties?
33
Which method enables you to call a seeder from within another seeder (e.g., inside DatabaseSeeder)?
$this->seed(Class::class);
$this->call(Class::class);
$this->run(Class::class);
$this->execute(Class::class);
34
When defining a foreign key in a migration, what does constrained() do in modern Laravel syntax?
Example: $table->foreignId('user_id')->constrained();
35 Which return response is most appropriate for an API endpoint if a requested resource is not found?
response()->json(['error' => 'Not Found'], 500)
response()->json(['error' => 'Not Found'], 404)
redirect('/home')
return null;
36 What function is used to convert an Eloquent collection to a JSON string directly?
toJson()
stringify()
encode()
serialize()
37 Consider the equation for pagination offset: . Which Query Builder method handles this calculation automatically?
limit()
chunk()
paginate()
offset()
38 In a MongoDB Laravel model, how are embedded documents usually accessed?
39 What is the purpose of Model Factories in the context of Seeding?
40 Which query builder method is used to update an existing record?
insert()
replace()
update()
change()
41 To use a specific database connection for a model other than the default, which property is used?
$connection
$database
$link
$driver
42
In a REST API, what does the HEAD method do?
43
When defining an Eloquent relationship, return $this->hasMany(Comment::class); implies what relationship structure?
44 If you want to run a raw SQL query using the facade but do not need a return value (e.g., deleting), which method is used?
DB::select()
DB::statement()
DB::run()
DB::exec()
45 Which Artisan command generates a migration file specifically to modify an existing table?
php artisan make:migration create_users_table
php artisan make:migration add_votes_to_users_table --table=users
php artisan modify:table users
php artisan db:alter users
46 How do you define a route parameter in Laravel (often used for REST APIs)?
Route::get('/user/?', ...)
Route::get('/user/{id}', ...)
Route::get('/user/:id', ...)
Route::get('/user/$id', ...)
47 In Eloquent, what is the 'N+1 query problem'?
48 How is the N+1 query problem solved in Eloquent?
all()
with()
49 When using MongoDB with Laravel, if you want to store a date, it is stored as:
50 Which interface must a resource class implement to define how a model maps to JSON?
JsonResource
Serializable
Model
ArrayAccess