0
0
Laravelframework~20 mins

Model creation in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Laravel Model Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the default table name for this Laravel model?
Given this Laravel model class, what table will Eloquent use by default?
Laravel
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ProductCategory extends Model
{
    // No table property defined
}
A"productcategory"
B"product_categories"
C"product_category"
D"productcategories"
Attempts:
2 left
💡 Hint
Laravel converts the model class name to snake_case and pluralizes it.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a guarded property to protect 'id' and 'created_at' fields?
Choose the correct syntax to protect 'id' and 'created_at' from mass assignment in a Laravel model.
Laravel
class UserProfile extends Model
{
    // Define guarded property here
}
Apublic $guarded = ['id', 'created_at'];
Bprotected $guarded = 'id', 'created_at';
Cprotected $guarded = ['id', 'created_at'];
Dprotected $guarded = array('id', 'created_at');
Attempts:
2 left
💡 Hint
The guarded property must be protected and assigned an array.
state_output
advanced
2:00remaining
What is the value of $model->timestamps after this model is instantiated?
Consider this Laravel model. What is the value of the public property $timestamps after creating a new instance?
Laravel
class Order extends Model
{
    public $timestamps = false;
}

$model = new Order();
$value = $model->timestamps;
Afalse
Btrue
Cnull
Dundefined property error
Attempts:
2 left
💡 Hint
The model property $timestamps controls if timestamps are managed automatically.
🔧 Debug
advanced
2:00remaining
Why does this model fail to save data to the database?
This Laravel model does not save data when calling save(). What is the likely cause?
Laravel
class Customer extends Model
{
    protected $table = 'customers';
    protected $fillable = ['name', 'email'];

    public function save(array $options = [])
    {
        // Forgot to call parent save
    }
}

$customer = new Customer();
$customer->name = 'Alice';
$customer->email = 'alice@example.com';
$customer->save();
AThe save method overrides parent but does not call parent::save(), so data is not saved.
BThe $fillable array is missing 'id', so save fails.
CThe protected $table property is incorrect and causes failure.
DThe model class must be abstract to save data.
Attempts:
2 left
💡 Hint
Overriding save requires calling the parent's save method.
🧠 Conceptual
expert
2:00remaining
Which statement about Laravel model events is true?
Select the correct statement about Laravel model events during model creation.
AThe saved event fires before the creating event.
BThe created event fires before the model is saved to the database.
CThe saving event fires only after the model is saved to the database.
DThe creating event fires before the model is saved to the database.
Attempts:
2 left
💡 Hint
Think about the order of events when a model is saved.