
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
- Laravel create table migration with model
- How to get CSRF token in laravel controller
- How to create and run user seeder in laravel
- How to Get records between two dates in Laravel
- How to insert multiple rows in mysql using loop in laravel?
- How to set column as primary key in Laravel model
- How to get database name in Laravel 9 ?
- Get content from web URL in laravel
- Method Illuminate\Events\Dispatcher::fire does not exist
- Trying to access array offset on value of type null error in laravel
- How to get file extension from input type file in laravel
- RuntimeException You must enable the openssl extension in your php.ini to load information from https://repo.packagist.org
- How to prevent host header attack in Laravel
- Route group with URI prefix using middleware and route name prefixes
- 419 page expired error in Laravel
- OrderBy on Eloquent relationships method in Laravel
- Global scope in Laravel with example
- How to get only time from created_at in laravel
- How to get user information using hootlex/laravel-friendships package in laravel
- Class 'Facade\Ignition\IgnitionServiceProvider' not found
- How to add foreign key in laravel using migration
- Laravel change date format
- Include External CSS and JS file in Laravel
- How to Run CRON Job on LIVE SERVER on Cpanel in Laravel Project
- How to restore multiple records after soft-deletes in Laravel