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 generate:model ModelName
php artisan make:model ModelName
php artisan new:model ModelName
2
By default, what table name will Laravel assume for a model named Flight?
flight
tbl_flights
flight_table
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 $source
protected $table
public $tableName
private $db_table
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()
down()
reverse()
rollback()
6 Which Artisan command executes all outstanding migrations?
php artisan schema:update
php artisan db:migrate
php artisan migrate:run
php artisan migrate
7 In the context of the Query Builder, which method is used to retrieve all rows from a table?
all()
get()
retrieve()
fetch()
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?
$hidden
$visible
$guarded
$fillable
11 Conversely, which property acts as a blacklist for mass assignment in Eloquent?
$blocked
$protected
$guarded
$fillable
12 Which of the following describes the syntax to create a new record using Eloquent Mass Assignment?
User::insert(['name' => 'John']);
User::make(['name' => 'John'])->save();
User::add(['name' => 'John']);
User::create(['name' => 'John']);
13 What is the purpose of Database Seeding?
14 Which Artisan command creates a new Seeder class?
php artisan new:seeder Name
php artisan create:seed Name
php artisan db:seed Name
php artisan make:seeder Name
15 When using Laravel with MongoDB, which library is commonly used to provide Eloquent-like functionality?
doctrine/mongodb
laravel/mongo-connector
illuminate/nosql
jenssegers/mongodb
16
In a MongoDB Eloquent model, what class should the Model extend instead of Illuminate\Database\Eloquent\Model?
Illuminate\Database\NoSQL\Model
Mongo\Eloquent
MongoDB\Model
Jenssegers\Mongodb\Eloquent\Model
17 What is the default primary key field name in MongoDB?
uuid
_id
key
id
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?
getOrFail()
firstOrFail()
find()
findOrFail()
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?
app/Http/routes.php
routes/console.php
routes/api.php
routes/web.php
24 In Eloquent, how do you define a Soft Delete?
deleted = 1 column manually.
$softDelete = true.
delete:soft command.
SoftDeletes trait in the model.
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:refresh
php artisan migrate:rollback
php artisan db:undo
php artisan migrate:reset
27 When using the Query Builder, which method creates a raw expression that is injected into the query as-is?
DB::sql()
DB::raw()
DB::inject()
DB::text()
28 In an API Controller, what method is conventionally used to show a single specific resource?
show($id)
store()
index()
create()
29 Which of the following is correct to delete a user with ID 1 using Eloquent?
User::destroy(1);
User::delete(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
timestamp column
created_at and updated_at columns
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->call(Class::class);
$this->run(Class::class);
$this->execute(Class::class);
$this->seed(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?
redirect('/home')
return null;
response()->json(['error' => 'Not Found'], 500)
response()->json(['error' => 'Not Found'], 404)
36 What function is used to convert an Eloquent collection to a JSON string directly?
serialize()
stringify()
toJson()
encode()
37 Consider the equation for pagination offset: . Which Query Builder method handles this calculation automatically?
chunk()
limit()
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?
update()
change()
insert()
replace()
41 To use a specific database connection for a model other than the default, which property is used?
$connection
$link
$database
$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::exec()
DB::statement()
DB::run()
DB::select()
45 Which Artisan command generates a migration file specifically to modify an existing table?
php artisan db:alter users
php artisan make:migration add_votes_to_users_table --table=users
php artisan modify:table users
php artisan make:migration create_users_table
46 How do you define a route parameter in Laravel (often used for REST APIs)?
Route::get('/user/$id', ...)
Route::get('/user/?', ...)
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
ArrayAccess
Serializable
Model