
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 use or operator in laravel
- How to update record after save method in Laravel
- How to return error message from controller to view in laravel
- How to get file extension from input type file in laravel
- How to print form data in laravel
- Display option of select as selected with blade directive Laravel
- How to use more than one query scope in Laravel
- Connection could not be established with host smtp.gmail.com :stream_socket_client(): unable to connect to tcp://smtp.gmail.com:587 (Connection refused)"
- Laravel migration add foreign key to existing table
- How to restore multiple records after soft-deletes in Laravel
- How to check query string exists or not in laravel blade
- How to display 1 day ago in comments in laravel view
- How to pass query string with pagination in laravel
- How to pass link from controller to view in laravel on ajax call
- How to add dynamic page title in Laravel view
- Display data in table using foreach in Laravel
- How to get last record from object collection in laravel
- Comment .env file in laravel
- Route prefix with auth middleware in laravel
- How to get specific columns using with method in laravel Eloquent relationship
- Generate unique username in Laravel
- Print query in laravel
- Property [user] does not exist on this collection instance
- Method Illuminate\Database\Eloquent\Collection::appends does not exist
- Recursive function example code PHP Laravel