
How to check record exist or not in relationship table
How to check record exist or not in relationship table
Good day, guys. In this post, we’ll look at how to solve the "How to check record exist or not in relationship table" programming puzzle.
You can check if records exist or not in the relationship table. You have to use the has() method with the name of relationship method to get the record (parent table) if records exist in the related table and it will return null if there are no records in it (child table).-
Check if user has created posts or not in laravel using has method
$userHasPosts = User::has('posts')->find(Auth::user()->id); dd($userHasPosts); //app\Models\User.php //Model path may be different as per your laravel version public function posts(){ return $this->hasMany(Post::class); }
Output :
Return user details from table if user has created any post
App\Models\User {#1556 ▼ #fillable: array:13 [▶] #hidden: array:2 [▶] #casts: array:1 [▶] #connection: "mysql" #table: "users" #primaryKey: "id" #keyType: "int" +incrementing: true #with: [] #withCount: [] +preventsLazyLoading: false #perPage: 15 +exists: true +wasRecentlyCreated: false #attributes: array:23 [▶] #original: array:23 [▶] #changes: [] #classCastCache: [] #dates: [] #dateFormat: null #appends: [] #dispatchesEvents: [] #observables: [] #relations: [] #touches: [] +timestamps: true #visible: [] #guarded: array:1 [▶] #rememberTokenName: "remember_token" }
If user has not created any postnull
This code snippet will return the record from the users table if there are post records (from the related post table) that belong to this particular user exists in the posts table else it will return null. To achieve this you have to create a users table with column id (primary key) and posts table with user_id (foreign key) in the database. You have to also create a hasMany relationship with name posts in the 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 get last record from object collection in laravel
- Route group with URI prefix using middleware and route name prefixes
- InRandomOrder() method with example in laravel
- There are no commands defined in the "route:" namespace
- Laravel route parameter
- Laravel create multiple records in Pivot table
- How to check duplicate entry in laravel
- Json encode method in laravel
- Laravel 7 login error message not showing
- Route [password.request] not defined
- Laravel form request validation
- How to return a column with different name in Laravel
- SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
- If condition in Laravel 9
- Permission denied error while creating storage link in Laravel
- How to pass data to partial view file in laravel
- OrderBy on Eloquent relationships method in Laravel
- Extract only time from datetime in laravel
- How to upload files to amazon s3 bucket using Laravel
- Use withCount() to Calculate Child Relationship Records
- How to get route method name in Laravel
- SQLSTATE[42000]: Syntax error or access violation: 1055
- Get the post details if it has at least one comment in comments table
- SQLSTATE[23000]: Integrity constraint violation: 1022 Can't write; duplicate key in table
- Show old value while editing the form in Laravel