Black Friday: coupon FRIDAY24 for 40% off Yearly/Lifetime membership! Read more here
Courses

Design Patterns in Laravel 11

So, we move into the world of patterns inside the framework itself, starting with Builder.

Of course, the Builder pattern can be seen in more places than just your code - for example, in the /vendor folder!


Builder in View

Take a look at this code.

Some Controller

return view('greeting')
->with('name', 'Victoria')
->with('occupation', 'Astronaut');

It's a typical Laravel view usage, but if we look closer, it's a Builder pattern!

Especially if we take a look at the source code of the ->with() method, we can see that it returns $this:

Illuminate/View/View.php

public function with($key, $value = null)
{
if (is_array($key)) {
$this->data = array_merge($this->data, $key);
} else {
$this->data[$key] = $value;
}
 
return $this;
}

In this case, the Builder pattern is one of...

This lesson is only for Premium Members.
Want to access all lessons of this course?

You also get:

  • 67 courses (1172 lessons, 43 h 18 min total)
  • Premium tutorials
  • Access to repositories
  • Private Discord