Home > OS >  Hi, to prevent the user logged in with php laravel from renting a different book again when the book
Hi, to prevent the user logged in with php laravel from renting a different book again when the book

Time:08-29

I am making a project in Php Laravel that allows the logged in user to rent a book, but if the logged in user has rented 1 book, I am trying to prevent different book rentals, but what I am doing does not work, where is my mistake?

1-"This is my code where i rent a book" :

public $user_id;
public $kitap_id;
public $AlimTarihi;
public $TeslimTarihi;
public $durum;

public function kirala($kitap_id)
{
    if(Auth::check())
    {
        if(Kira::where('user_id',Auth::user()->id)->where('durum',"Edilmedi"))
        {
            session()->flash('message','There is a book you are renting.');
            return redirect('/user/dashboard');
        }
        else
        {
            $kira = new Kira();
            $kira->user_id = Auth::user()->id;
            $kira->kitap_id = $kitap_id;
            $kira->AlimTarihi = Carbon::now();
            $kira->TeslimTarihi = Carbon::now()->addDays(7);
            $kira->durum = 'Edilmedi';
            $kitaplar = Kitaplar::find($kitap_id);
            $kitaplar->kiraliMi = 1;
            $kitaplar->save();
            $kira->save();
            session()->flash('message','book is rented.');
            return redirect('/user/dashboard');
        }

    }
    else
    {
        return redirect()->route('login');
    }
}

2- "Rent table" :

Schema::create('kiras', function (Blueprint $table) {
            $table->id();
            $table->bigInteger('user_id')->unsigned()->nullable();
            $table->bigInteger('kitap_id')->unsigned()->nullable();
            $table->timestamp('AlimTarihi')->nullable();
            $table->timestamp('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');
        });

3- "Book table" :

Schema::create('kitaplars', function (Blueprint $table) {
            $table->id();
            $table->string('KitapAdi');
            $table->BigInteger('BarkodNo');
            $table->BigInteger('SayfaSayisi');
            $table->decimal('SatisFiyati')->nullable();
            $table->string('image')->nullable();
            $table->text('images')->nullable();
            $table->bigInteger('kategori_id')->unsigned()->nullable();
            $table->bigInteger('yazar_id')->unsigned()->nullable();
            $table->boolean('kiraliMi');
            $table->timestamps();
            $table->foreign('kategori_id')->references('id')->on('kategoris')->onDelete('cascade');
            $table->foreign('yazar_id')->references('id')->on('yazarlars')->onDelete('cascade');
        });

4- "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('ADM for Admin and USR for User or Customer');
            $table->timestamps();
        });

CodePudding user response:

I believe your initial if-check isn't doing what you expect. This line:

if(Kira::where('user_id',Auth::user()->id)->where('durum',"Edilmedi"))

is an incomplete builder object, so will always return true. I suggest completing the query with something along the lines of:

if(isset(Kira::where('user_id',Auth::user()->id)->where('durum',"Edilmedi")->first()))

or if you have allowed multiples rented out in some previous instance, something like this might work as you wish:

if(Kira::where('user_id',Auth::user()->id)->where('durum',"Edilmedi")->count())
  • Related