How to check records exist in loaded relationship in Laravel blade view
How to check records exist in loaded relationship in Laravel blade view
With this article, we will examine several different instances of how to solve the "How to check records exist in loaded relationship in Laravel blade view".
You can check record exist or not in loaded relationship in Laravel blade view. Sometimes we have to check that the records exists or not in nested relationship, in that case it helps you to find out the solution for you-
Check records exist in loaded relationship in Laravel blade view
//routes\web.php Route::get('/user-with-posts', function(){ $userPosts = App\Models\User::withCount('posts')->find(1); return view('home', compact('userPosts')); }); //resources\views\home.blade.php @if($userPosts->posts_count > 0 ) <p> data exists in relationship! </p> @foreach($userPosts->posts as $post) <h5> {{ $post->title }} </h5> @endforeach @else <p> data is not exist in relationship! </p> @endif //app\Models\User.php public function posts(){ return $this->hasMany('App\Models\Post'); }
You have to create the posts table with foreign key (user_id) column and the posts relationship method in user model.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- How to include header file in laravel
- Non-static method App\Http\Helper::myFunction() should not be called statically
- If condition in foreach loop in laravel
- How to decrypt laravel password
- Call to undefined relationship [user] on model [App\Models\Post]
- Seed database using SQL file in Laravel
- How to run a specific seeder class in laravel
- Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
- Update existing pivot table data in laravel
- Class 'App\Http\Controllers\User' not found
- Get last week data in Laravel
- File_put_contents(/var/www/html/w3code/storage/framework/sessions/CXwN3EXKxERD6jgy3rADcaAAbAx8FRKih2JK7UO9): Failed to open stream: Permission denied
- The use statement with non-compound name 'Auth' has no effect
- Link storage folder in laravel 8
- Laravel csrf token mismatch for ajax POST Request
- Declaration of App\Models\Post::sluggable() must be compatible with Cviebrock\EloquentSluggable\Sluggable
- How to change default timestamp fields name in Laravel
- How to randomly get the user id from users table in laravel
- How to call Laravel route in jQuery
- Laravel create multiple records in Pivot table
- In order to use the Auth::routes() method, please install the laravel/ui package
- How to create pivot table in laravel using migration
- How to get the id of last record from collection object in laravel view
- Insert values in pivot table dynamically in laravel
- How to check relationship is loaded or not in Laravel