
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
Answers 1
-
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'); }
0You have to create the posts table with foreign key (user_id) column and the posts relationship method in user model.
Random Code Snippet Queries: Laravel
- How to avoid duplicate entries in pivot table in Laravel
- Class 'App\Rules\Hash' not found in Laravel
- Display success message in laravel
- Add a subselect based on relationship using withAggregate method
- Save or update pivot table data with additional column in Laravel
- How to create pivot table in laravel using migration
- Laravel onclick function not working
- Laravel form request validation
- Validation for multiple forms on same page in laravel
- Call to a member function pluck() on null
- How to create and run user seeder in laravel
- How to display order by null last in laravel
- How to validate website url in laravel using validaiton
- PhpMyAdmin - Error The mysqli extension is missing
- How to delete record in Laravel with ajax
- How to validate URL with https using regex in laravel
- Permanently delete a record in laravel
- InRandomOrder() method with example in laravel
- How to include header file in laravel
- Permission denied error while creating storage link in Laravel
- OrderBy on Eloquent relationships method in Laravel
- How to upload image in laravel 8
- The Pusher library requires the PHP cURL module. Please ensure it is installed
- How to check if user has created any post or not in laravel
- Display message with session flash using bootstrap alert class in laravel