5 Laravel’s Hidden Gems

5 Laravel’s Hidden Gems

Spread the love1. Stop on first validation error By default, Laravel will check for all validation rules and return a list of errors. But if you want to stop this process after first validation failure, that’s how you can achieve…Read More→

1. Stop on first validation error

By default, Laravel will check for all validation rules and return a list of errors. But if you want to stop this process after first validation failure, that’s how you can achieve it

$request->validate([
    'title' => 'bail|required|unique:posts|max:255',
    'body' => 'required',
]);

2. Find many

Most of us developers have used Eloquent’s method find to search for one specific id, but do you know we can pass multiple ids to find which will return a collection.

// Will return Eloquent Model
$user = User::find(1);
// Will return Eloquent Collection
$users = User::find([1,2,3]);

3. Relationship but with condition

Some times we need to apply where on relationships, instead of calling relationship and then chaining where we can achieve it by following:

// app/Post.php model
public function comments()
{
    return $this->hasMany(Comment::class);
}
public function approved_comments()
{
    return $this->hasMany(Comment::class)->where('approved', 1);
}

4. Rename pivotal table

In pivot table, if you want to rename pivot to some thing else, this can be done by following code:

public function podcasts() {
return $this->belongsToMany('App\Podcast')
->as('subscription')
->withTimestamps();
}
// Then somewhere in Controller...
$podcasts = $user->podcasts();
foreach ($podcasts as $podcast) {
// instead of $podcast->pivot->created_at ...
echo $podcast->subscription->created_at;
}

laravel php find many laravel prepare for validation laravel relationship but with condition laravel rename pivotal table laravel stop on first validation error laravel

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to validate mobile phone number in laravel with example

Data validation is a very important thing for a web application. In this article i will let you know how to validate mobile phone number in laravel.

#4.2 has-many-through relation using Pivot model | Laravel Eloquent Relationships

In this video, you will learn the "has-many-through" relationship using a Pivot model. 𝐋𝐚𝐫𝐚𝐯𝐞𝐥 𝐄𝐥𝐨𝐪𝐮𝐞𝐧𝐭 𝐑𝐞𝐥𝐚𝐭𝐢𝐨𝐧𝐬𝐡𝐢𝐩𝐬 𝐏𝐥𝐚𝐲𝐥𝐢𝐬𝐭 https://www.youtube.com/playli...

Laravel 7/6 Image Validation

Laravel image validation. Here you will learn how to validate image mime type, size, and dimension in laravel app before uploading image to DB and folder.

#5.2 Many to Many Polymorphic Relationship | Laravel Eloquent Relationships

"many-to-many" polymorphic relations are slightly more complicated than "one-to-one" `morphOne and "one-to-many" 'morphMany' polymorphic relationships. In th...

Image Validation In Laravel 7

Laravel image validation. Here you will learn how to validate image mime type, size, and dimension in laravel app before uploading image to DB and folder.