12 Laravel Features That Will Blow Your Mind

Ismat Babirli
3 min readJun 26, 2023
Photo by Nangialai Stoman on Unsplash

Laravel Eloquent is a feature-rich ORM (Object Relational Model) that offers many powerful features not always highlighted in the official Laravel documentation. This article aims to shed light on these hidden gems, providing insights into how they can enhance your Laravel application development.

Expressive Where Syntax

The Expressive Where Syntax allows for more expressive queries when retrieving a particular record. Instead of the usual $product = Product::where('category', 2)->get();, you can make your query more expressive by suffixing the attribute to the where clause itself: $product = Product::whereCategory(2)->get();.

Returning Relations On Model Save

When saving a model, Laravel Eloquent typically only returns the model you are saving. However, if you want to return its related model, you can do so as follows:

public function store(Request $request, $categoryId)
{
$product = new Product();
$product->fill($request->all());
$product->category_id = $categoryId;
$product->category;
return $product->save();
}

Specifying Attributes in find() Method

When finding a model by a primary key, you can specify the attributes to select as a second argument, like so: $product = Product::find(1, ['name', 'sku', 'price']);

Increment And Decrement Model Attributes

Laravel Eloquent allows you to increment or decrement a model attribute using increment() and decrement() methods. For example, to add one view to a product model, you would use Product::find($productId)->increment('views');.

Checking If Models Are…

--

--