Challenge - 5 Problems
Laravel Model Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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
}Attempts:
2 left
💡 Hint
Laravel converts the model class name to snake_case and pluralizes it.
✗ Incorrect
Laravel automatically converts the model class name from PascalCase to snake_case and pluralizes it to determine the table name. So ProductCategory becomes product_categories.
📝 Syntax
intermediate2: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
}Attempts:
2 left
💡 Hint
The guarded property must be protected and assigned an array.
✗ Incorrect
The guarded property must be declared as protected and assigned an array of field names to protect them from mass assignment.
❓ state_output
advanced2: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;Attempts:
2 left
💡 Hint
The model property $timestamps controls if timestamps are managed automatically.
✗ Incorrect
Since the model explicitly sets public $timestamps = false, the property value is false after instantiation.
🔧 Debug
advanced2: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();
Attempts:
2 left
💡 Hint
Overriding save requires calling the parent's save method.
✗ Incorrect
Overriding the save method without calling parent::save() prevents the actual saving process, so no data is saved.
🧠 Conceptual
expert2:00remaining
Which statement about Laravel model events is true?
Select the correct statement about Laravel model events during model creation.
Attempts:
2 left
💡 Hint
Think about the order of events when a model is saved.
✗ Incorrect
The creating event fires before the model is saved, allowing you to modify data before insertion. The created event fires after saving.