JobsPing

PHP / Laravel interview practice

Covers Eloquent, routing, queues, security. Questions are written for real interviews at junior, mid and senior level.

Start a PHP / Laravel practice test

15 multiple-choice questions · 15 minutes · instant score with explanations

Sample PHP / Laravel interview questions

Three questions from the bank, with answers. The practice test draws different ones each time.

Question 1 · Views · junior

What does this return statement do?

// Inside a Controller
return view('welcome', ['name' => 'Claude']);
  1. A.It returns a JSON response to the user.
  2. B.It redirects the user to the welcome route.
  3. C.It prints the raw HTML of the welcome file.
  4. D.It returns a view with 'name' available as $name.
Show answer

D. It returns a view with 'name' available as $name.

The view helper function takes the name of the template (located in resources/views) and an array of data that will be available as variables within that view.

Question 2 · Query Builder · junior

What is the return type of the get() method in the Query Builder?

use Illuminate\Support\Facades\DB;

$users = DB::table('users')->where('active', 1)->get();
  1. A.It returns an array of arrays.
  2. B.It returns the raw SQL query string.
  3. C.It returns a boolean indicating if users exist.
  4. D.It returns a Collection of objects.
Show answer

D. It returns a Collection of objects.

The get() method executes the query and returns a Collection of stdClass objects. Without get() or a similar terminator (like first()), the query would remain as a Builder object.

Question 3 · Eloquent ORM · junior

What type of relationship is defined by this method?

public function posts()
{
    return $this->hasMany(Post::class);
}
  1. A.It defines a one-to-many relationship.
  2. B.It creates a database relationship automatically.
  3. C.This defines a belongsTo relationship.
  4. D.It defines a one-to-one relationship.
Show answer

A. It defines a one-to-many relationship.

This is the standard Eloquent relationship definition. 'hasMany' indicates that one User model corresponds to multiple Post models. Laravel assumes the foreign key is 'user_id' by default.

Ready to apply? Browse remote PHP / Laravel jobs →