Laravel Questions and Answears

5/5 - (1 vote)

What is PHP artisan?

PHP artisan is the command-line tool included with Laravel.

How to know Laravel version and where is it defined?

php artisan –version

The Laravel version is defined here: The Laravel framework version.

     * The Laravel framework version.
     * @var string
    const VERSION = '8.x-dev';

How to install and set up Laravel?

Follow this tutorial to read How to install and set up Laravel with Git?

What is database migration? How to create migration via artisan?

Migrations are like version control for your database, that allows you to easily modify and share the application’s database schema
Use the command below to create migration data via artisan.

// creating Migration
php artisan make:migration table_name

How to remove a column from existing Laravel tables?

Follow this tutorial How to remove a column from existing tables using Laravel migrations?.

How to refresh Laravel migration for specific table?

Delete the table and the migration from the migrations table in your database for the table you wish to migrate again, then php artisan migrate will work and just create the table again.

About Composer

What is composer?

Composer is a tool for managing dependency in PHP.

What does composer dump-autoload do in Laravel?

composer dump-autoload will regenerates the list of all classes that need to be included in the project (autoload_classmap.php)

What are Laravel eloquent?

The Eloquent ORM included with Laravel provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding “Model” which is used to interact with that table. Models allow you to query for data in your tables, as well as insert new records into the table.

How to create a model?

Models typically live in the app directory, but you are free to place them anywhere that can be auto-loaded according to your composer.json file. All Eloquent models extend Illuminate\Database\Eloquent\Model class

e.g to create a Category model:

php artisan make:model Category

Types of relationships available in Laravel Eloquent

Below are the types of relationships supported by Laravel Eloquent ORM.

  • One To One
  • One To Many
  • Many To Many
  • Has Many Through
  • Polymorphic Relations
  • Many To Many Polymorphic Relations

How to validate user input forms?

In controller, e.g to create categories, CategoryController.php:

     * Store a newly created resource in storage.
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
    public function store(Request $request)

Then go to form, in view, e.g create.blade.php

@if ($errors->any())
    <div class="alert alert-danger">
        <strong>Whoops!</strong> There were some problems with your input.<br><br>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>

<form action="{{ route('') }}" method="POST">

How to limit string length in Laravel Blade?

Find the simplest method about how to limit the string length in a Laravel Blade file.

How to check request is ajax or not?

Use $request->ajax() method to check request is ajax or not.

      public function saveData(Request $request)
                return "Request is of Ajax Type";
            return "Request is of Http type";

What is repository in Laravel?

You can use Repository in a Laravel to create a bridge between models and controllers. In other words, to decouple the hard dependencies of models from the controllers.
The model should not be responsible for communicating with or extracting data from the database. A model should be an object that represents a given table/document/object or any other type in our data structure and this should be its sole responsibility. Read more here about Laravel Repository Pattern

How to use Repository Pattern in Laravel?

Using Repository Pattern in Laravel has many benefits, below are the most important ones:

  • Business and data access logic can be tested separately
  • Reduces duplication of code
  • A lower chance for making programming errors
  • Centralization of the data access logic makes code easier to maintain

How to create a new Repository in Laravel?

The repository pattern is not connected to the framework, so you have to create your own.

  1. Check if the php artisan make:repo command is supported for your project. (open a terminal in the root of your project and type the php artisan command. Check near the make section.
  2. If you see a make:repository command, then you can use above command.
  3. If you didn’t find, can install Laravel 5 repository generator, A simple package for adding repository.

What’re the differences between PUT and PATCH requests?

The HTTP methods PATCH can be used to update partial resources. For instance, when you only need to update one field of the resource, PUTting a complete resource representation might be cumbersome and utilizes more bandwidth

PATCH /user/webpedia HTTP/1.1

Also, the PUT method is idempotent. PUTting the same data multiple times to the same resource, should not result in different resources, while POSTing to the same resource can result in the creation of multiple resources. Read more here – When should we use the PATCH HTTP method?

What is the difference between table->string(‘…’) and text(‘..’)?

This table contains a variety of column types that you may use when building your tables in Laravel.

$table->string() uses a VARCHAR equivalent
$table->text() uses a TEXT equivalent

The difference is that VARCHAR has a specified length (default with string() is 255 characters) whereas TEXT doesn’t have that.

What is the difference between {{ }} and {!! !!} in Laravel blade files?

By default, Blade {{ }} statements are automatically sent through PHP’s htmlentities function to prevent XSS attacks. If you do not want your data to be escaped, you may use {!! !!}.

What is namespace in Laravel? How to use namespace in Laravel?

PHP doesn’t allow two classes that share the same name. They have to be unique.

Namespaces are used to avoid class name collisions, so you can have two same class names under two different namespaces.

How to generate secure https URL from route?

Starting from Laravel 5.5 You only need to add https on your .env file as AppServiceProvider already had a function that checks if your APP_URL or app.url on your config has https on it.

class AppServiceProvider extends ServiceProvider
public function boot()

if (str_contains(\Config::get(‘app.url’), ‘https://’)) {

Url segments in Laravel

Get The Request URL

$url = Request::url();

Retrieve A Request URI Segment

$segment = Request::segment(1);

Get last part of current URL


Hello there!

I hope you find this post useful!

I'm Mihai, a programmer and online marketing specialist, very passionate about everything that means online marketing, focused on eCommerce.

If you have a collaboration proposal or need helps with your projects feel free to contact me. I will always be glad to help you!

subscribe youtube

Leave a Comment