No users found

Published 6 days ago by muazzamazaz

I have table users with two records but following code not getting it

use App\Helpers\Helper;
use App\Http\Controllers\Controller;
use App\User;
use App\UserRating;
use Hash;
use Illuminate\Http\Request;
use Validator;
use App\Requests;

class UserController extends Controller
{


    public function users()
    {
        $user = User::orderBy('created_at', 'desc')->get();

        return view('admin.users')->withPage('users')->with('sub_page', 'view-user')->with('users', $user);
    }

....
<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\SoftDeletes;

class User extends Authenticatable
{
    use SoftDeletes;
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
    
    /**
     * Fetch the list of recent users by creation date
     * 
     * @return User
     */
    private function fetchRecentUsers()
    {
       
        return $this->orderBy('created_at' , 'desc')->get();
    }
    
    /**
     * Returns a list of recent users by creation date
     * 
     * @return mixed
     */
    public static function getRecentUsers()
    {
        $user = new User();
        return $user->fetchRecentUsers();
    }
}

Best Answer (As Selected By muazzamazaz)
Cinek

Try do this

$users = User::withTrashed()->get();

If this works then it means your records are deleted using soft delete.

Cinek
Cinek
6 days ago (3,890 XP)

Try do this

$users = User::withTrashed()->get();

If this works then it means your records are deleted using soft delete.

Robstar
Robstar
6 days ago (36,110 XP)

You could also do:

$user = User::latest()->get();

Does your deleted_at column have a date populated? If so, your model is soft deleted - see https://laravel.com/docs/5.5/eloquent#soft-deleting

If the above doen't work, what is the output in your controller if you dd() the $user variable?

Please sign in or create an account to participate in this conversation.