Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Thor0's avatar
Level 1

Show the user who rents the most books with php laravel

I want to show the user who rents the most books with php laravel, but I get an error like this, what is the reason for the error? error: Undefined property: stdClass::$user

This is my code:

$kiras = DB::table('kiras')
        ->select('user_id',DB::raw('count(*) as total'))
        ->groupBy('user_id')
        ->orderBy('total','DESC')
        ->take(1)
        ->get();

        return view('livewire.home-component',[
            'kiras'=>$kiras,
            ])->layout('layouts.base');

Page:

<div class="panel-body">
              <div class="col-md-10">
                    <div class="card" style="width: 20rem;">
                         <div class="card-body">
                          <h4 class="card-title">User who rents the most books : </h4>
                                @foreach($kiras as $kira)
                                       {{$kira->user->name}}
                                @endforeach
                         </div>
             </div>
      </div>
 </div>

Rent table:

Schema::create('kiras', function (Blueprint $table) {
            $table->id();
            $table->bigInteger('user_id')->unsigned()->nullable();
            $table->bigInteger('kitap_id')->unsigned()->nullable();
            $table->date('AlimTarihi')->nullable();
            $table->date('TeslimTarihi')->nullable();
            $table->enum('durum',['Edildi','Edilmedi'])->default('Edilmedi');
            $table->timestamps();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('kitap_id')->references('id')->on('kitaplars')->onDelete('cascade');
        });

User table :

Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('lastName')->nullable();
            $table->BigInteger('Yasi')->nullable();
            $table->string('Adres')->nullable();
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->foreignId('current_team_id')->nullable();
            $table->string('profile_photo_path', 2048)->nullable();
            $table->string('utype')->default('USR')->comment('Yonetici icin ADM Kullanici icin USR');
            $table->timestamps();
        });

Rental table model :

public function user()
    {
        return $this->belongsTo(User::class,'user_id');
    }
0 likes
9 replies
tykus's avatar
tykus
Best Answer
Level 104

If you're using the Query Builder (DB::table...) then you do not have access to relationships like this $kira->user->name. Use Eloquent instead (assuming a kiras relationship on the User model)

$users = User::withCount('kiras')->orderBy('kiras_count', 'desc')->get()
@foreach($users as $user)
    {{$user->name}}
@endforeach

Note that you are listing Users, not Books, so start from that Model

1 like
Thor0's avatar
Level 1

@tykus I mean, to show the name of the user with the most records in the kiras table, there is a user_id field in the kiras table.

Thor0's avatar
Level 1

@tykus yes i tried and got error Call to undefined method App\Models\User::kiras()

Thor0's avatar
Level 1

@tykus Because it should be like this, but all users come to the page, I only want the most users to come $kiras = Kira::withCount('user')->orderBy('user_count', 'desc')->get(); @foreach($kiras as $kira) {{$kira->user->name}} @endforeach

tykus's avatar

@Thor1 like I said... "assuming a kiras relationship on the User model"

// User
public function kiras()
{
    return $this->hasMany(Kira::class);
}

EDIT this is counting the number of users who rented each Kira - it is not what you need.

$kiras = Kira::withCount('user')->orderBy('user_count', 'desc')->get()
Thor0's avatar
Level 1

@tykus I understood, I did as you said, but all users are coming to the page, I only want to bring the most users, how can I do that?

tykus's avatar

@Thor1 what do you mean... top 5, top 10, top 100???? use the take Builder method, e.g.

$users = User::withCount('kiras')->orderBy('kiras_count', 'desc')->take(10)->get()
1 like

Please or to participate in this conversation.