
How to check if user has created any post or not in laravel
You can check if user has created post or not in laravel using has() method on relation model. Here has() method returns the null value if user has not created any post. If user has created any post then it will return the user record from the 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); }
0Output :
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.
Random Code Snippet Queries: Laravel
- Create record with unique slug in laravel
- Laravel 5.4 save data to database
- Laravel create multiple records in Pivot table
- SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'user_id'
- How to get all route list
- Use withCount() to get total number of records with relationship
- How to get specific columns using Laravel eloquent methods
- Undefined property: stdClass::$title
- Create a record if not exist in laravel
- Argument 1 passed to App\Http\Controllers\Auth\LoginController::authenticated() must be an instance of App\Http\Controllers\Auth\Request
- Class 'App\Rules\Hash' not found in Laravel
- Conditional where clause in Laravel
- How to pass variable from controller to model in Laravel
- RuntimeException You must enable the openssl extension in your php.ini to load information from https://repo.packagist.org
- Automatically remove records using Prunable trait in Laravel
- 419 page expired error in Laravel
- Laravel URL validation not working
- Laravel 9 pagination with search filter
- FirstOrCreate() Not Inserting Model
- How to get random string in Laravel
- Target class [HomeController] does not exist
- Use withCount() to Calculate Child Relationship Records
- How to create static page in Laravel
- How to Get records between two dates in Laravel
- How to create laravel project using composer