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');
}