Courses

Design Patterns in Laravel 11

Builder: in View, Validation, Relationships

Summary of this lesson:
- Examples of Builder pattern in Laravel View implementation
- Understanding Builder pattern in Laravel Validation rules
- Builder pattern usage in Eloquent relationships
- Laravel's internal use of method chaining and return $this

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...

The full lesson is only for Premium Members.
Want to access all 17 lessons of this course? (72 min read)

You also get:

  • 69 courses (majority in latest Laravel 11)
  • Premium tutorials
  • Access to repositories
  • Private Discord