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']);
A.It returns a JSON response to the user.
B.It redirects the user to the welcome route.
C.It prints the raw HTML of the welcome file.
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();
A.It returns an array of arrays.
B.It returns the raw SQL query string.
C.It returns a boolean indicating if users exist.
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);
}
A.It defines a one-to-many relationship.
B.It creates a database relationship automatically.
C.This defines a belongsTo relationship.
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.