Articles in the year 2022

Different ways to pass data to a Laravel View

#1. Using a magic method

First up, Laravel uses some PHP magic to make sense of fluent methods. If, for example, you have an array of people in a variable $people, then you can use a magic method withPeople on the view() helper function (or View:: facade) to pass the array to your view. In your blade file, your people array will be available via a $people variable.

Route::get('/', function () { $people = ['Bob', 'John', 'Simon']; return view('welcome')->withPeople($people); });

#1. Using a magic method

First up, Laravel uses some PHP magic to make sense of fluent methods. If, for example, you have an array of people in a variable $people, then you can use a magic method withPeople on the view() helper function (or View:: facade) to pass the array to your view. In your blade file, your people array will be available via a $people variable.

Route::get('/', function () { $people = ['Bob', 'John', 'Simon']; return view('welcome')->withPeople($people); });

#1. Using a magic method

First up, Laravel uses some PHP magic to make sense of fluent methods. If, for example, you have an array of people in a variable $people, then you can use a magic method withPeople on the view() helper function (or View:: facade) to pass the array to your view. In your blade file, your people array will be available via a $people variable.

Route::get('/', function () { $people = ['Bob', 'John', 'Simon']; return view('welcome')->withPeople($people); });

Thank you for reading this article.

If you've made it this far, you might like to connect with me on 𝕏 where I post similar content and interact with like-minded people. If this article was helpful to you I'd really appreciate it if you would consider buying me a coffee.
Continue Reading

Better HTTP Status Codes In Laravel

'Magic numbers' like 200 or 401 can cause a lot of confusion for colleagues or your future self. It's not always immediately obvious what these numbers represent.

A magic number is a number in the code that has no context or meaning.



Luckily, when it comes to HTTP Status Codes, we can make use of a complete set of constants that will make the meaning of your code self evident.

'Magic numbers' like 200 or 401 can cause a lot of confusion for colleagues or your future self. It's not always immediately obvious what these numbers represent.

A magic number is a number in the code that has no context or meaning.



Luckily, when it comes to HTTP Status Codes, we can make use of a complete set of constants that will make the meaning of your code self evident.

'Magic numbers' like 200 or 401 can cause a lot of confusion for colleagues or your future self. It's not always immediately obvious what these numbers represent.

A magic number is a number in the code that has no context or meaning.



Luckily, when it comes to HTTP Status Codes, we can make use of a complete set of constants that will make the meaning of your code self evident.

Thank you for reading this article.

If you've made it this far, you might like to connect with me on 𝕏 where I post similar content and interact with like-minded people. If this article was helpful to you I'd really appreciate it if you would consider buying me a coffee.
Continue Reading