Laravel Interview Questions

What do you know about Closures in Laravel?

Post Views: 37,912 In Laravel, a Closure is an anonymous method which can be used as a callback function. It can also be used as a parameter in a function. It is possible to pass parameters into a Closure. It can be done by changing the Closure function call in the handle() method to provide parameters to it. A Closure can access the variables outside the scope of the variable. Example function handle(Closure $closure) { $closure(); } handle(function(){ echo ‘Interview…

Read More

How will you explain Guarded Attribute in a Laravel model?

Post Views: 37,902 The guarded attribute is the opposite of fillable attributes. In Laravel, fillable attributes are used to specify those fields which are to be mass assigned. Guarded attributes are used to specify those fields which are not mass assignable. Code Source class User extends Model { protected $guarded = [‘role’]; // All fields inside the $guarded array are not mass-assignable } If we want to block all the fields from being mass-assigned, we can use: protected $guarded =…

Read More

How will you describe Fillable Attribute in a Laravel model?

Post Views: 37,898 In eloquent ORM, $fillable attribute is an array containing all those fields of table which can be filled using mass-assignment. Mass assignment refers to sending an array to the model to directly create a new record in Database. Code Source class User extends Model { protected $fillable = [‘name’, ’email’, ‘mobile’]; // All fields inside $fillable array can be mass-assigned }

Read More

How can we create a record in Laravel using eloquent?

Post Views: 37,894 We need to create a new model instance if we want to create a new record in the database using Laravel eloquent. Then we are required to set attributes on the model and call the save() method. Example public functionsaveProduct(Request $request ) $product = new product; $product->name = $request->name; $product->description = $request->name; $product->save();

Read More

How can we use maintenance mode in Laravel 5?

Post Views: 37,889 When an application is in maintenance mode, a custom view is displayed for all requests into the application. It makes it easy to “disable” application while it is updating or performing maintenance. A maintenance mode check is added in the default middleware stack for our application. When an application is in maintenance mode, a MaintenanceModeException will be thrown with a status code of 503. We can enable or disable maintenance mode in Laravel 5, simply by executing…

Read More

What do you know about Traits in Laravel?

Post Views: 37,883 PHP Traits is a group of methods which can be included within another class. A Trait cannot be instantiated by itself like an abstract class. Traits are generated to reduce the limitations of single inheritance in PHP. It allows a developer to reuse sets of methods freely in various independent classes living in different class hierarchies. Example trait Sharable { public function share($item) { return ‘share this item’; } } We can then include this Trait within…

Read More