7 Laravel Functions That ALSO Accept Array Parameter

Laravel has many functions that we use with a single parameter, but they also accept an array of values. In this tutorial, I will show the most widely used ones.


Eloquent: find($id) VS find($array)

The find() method on Eloquent Models is the most common example. We all know that we can do this:

$products = Product::find(1);

And it will return a single product:

But we can also pass an array of IDs to the find() method:

$products = Product::find([1, 2, 3]);

And it will return a Collection of products:

Notice: This does not work with Query Builder, only with Eloquent Models.


Eloquent: where($key, $value) VS where($conditionsArray)

Another cool feature of Eloquent is that we can pass an array of conditions to the where() method. A typical non-array example looks like this:

$products = Product::query()
->where('category_id', 1)
->where('manufacturer_id', 2)
->get();

But you can also write it to an array and have the same result:

$products = Product::query()
->where([
'category_id' => 1,
'manufacturer_id' => 2,
])
->get();

And this will produce the following SQL query:

select * from `products` where (`category_id` = 1 and `manufacturer_id` = 2)

Not only that, you can pass the operator like "<" or ">" to the array.

$products = Product::query()
->where([
'category_id' => 1,
['stock_left', '>', 100]
])
->get();

This will produce the following SQL query:

select * from `products` where (`category_id` = 1 and `stock_left` > 100)

Notice: Using an array will group the conditions, which removes the need for manual grouping like this:

$products = Product::query()
->where(function($query){
$query->where('category_id', 1)
->where('stock_left', '>', 100);
})
->get();

Session: put($key, $value) VS put($valuesArray)

Another example of multiple parameters comes from the Session helper. Typically we use it like this:

session()->put('cart', 'Cart information');
session()->put('cart_total', 'Cart total');

But you can also do this:

session()->put([
'cart' => 'Cart information',
'cart_total' => 'Cart total'
]);

It will set both keys and values in the session; no need to call put() twice.

Similarly, we can check if a key exists in the session like this:

session()->has('cart');

But what if we want to check if multiple keys exist? We can do this:

session()->has(['cart', 'cart_total']);

And it will return true if both keys exist or false if any of the keys do not exist.


Cache: put($key, $value) VS put($valuesArray)

Caching is another example, similar to the session, but it comes with a twist this time. Typically we would do something like this:

Cache::put('cart', 'Cart information', 60);
Cache::put('cart_total', 'Cart total', 60);

But it's also possible to do this:

Cache::put([
'cart' => 'Cart information',
'cart_total' => 'Cart total'
], 60);

Remember that the put() method accepts "value" as a second parameter, but if you pass an array - it will be treated as a time parameter.


Migrations: dropColumn($name) VS dropColumn($columnsArray)

In Migrations, this is useful when dropping multiple columns:

Schema::table('products', function (Blueprint $table) {
$table->dropColumn('name');
$table->dropColumn('description');
$table->dropColumn('price');
});

It can also be written like this:

Schema::table('products', function (Blueprint $table) {
$table->dropColumn(['name', 'description', 'price']);
});

Gates: has($gate) VS has($gatesArray)

When working with Gates, we can check if a user has a single ability like this:

Gate::has('edit-post');

But you can also check if the user has all the abilities:

Gate::has(['edit-post', 'delete-post']);

This check will return false when one of the abilities is missing. An excellent way to check if the user has all the required abilities rather than listing them one by one.


App: environment($env) VS environment($envArray)

Last on our list is the App facade. We all know that we can get the current environment like this:

App::environment();

Or check if the environment is local like this:

App::environment('local');

But you can also check if the environment is any of the listed ones:

App::environment(['local', 'testing']);

Do you know any other functions that accept arrays? Let us know in the comments below.

avatar

Actually me I have learned alot. I didn't know that find() accepts and an array. That is noted for "where()" I tried it sometime back to see its bavour and it worked

But what is most important for me this article is the Session: put() and Cache: put() not because they accept arrays but I have not been using them and I think they can help me where I have been using CartTable for cart holding.

👍 2
avatar
Charles-Louis Girardot

Oh dear, my where conditions are going to be smaller !

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 58 courses (1054 lessons, total 46 h 42 min)
  • 78 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent Premium Tutorials