【问题标题】:i want to show just my hotel's reservation LARAVEL 7我只想显示我的酒店预订 LARAVEL 7
【发布时间】:2020-06-05 04:54:57
【问题描述】:

这是预订表,它有hotel_id

Schema::create('reservations', function (Blueprint $table) {
        $table->id();
        $table->integer('user_id')->unsigned()->references('id')->on('users');
        $table->integer('hotel_id')->unsigned()->references('id')->on('hotel');
        $table->string('status')->default('pending');
        $table->integer('rooms');
        $table->date('checkin');
        $table->date('checkout');
        $table->timestamps();

我尝试只获取我的酒店 ID,并将其与预订的 hotel_id 进行比较,但不起作用

public function index()
{
    $myhotels = hotel::where('created_by',Auth::user()->id)->first('id');

 $reservations = Reservation::where('hotel_id',$myhotels)->get();

    return view('moderateur/reservation',compact('reservations'));
}

【问题讨论】:

  • 试试这个! $myhotels = hotel::where('created_by',Auth::user()->id)->first(); $reservations = Reservation::where('hotel_id',$myhotels->id)->get();

标签: laravel laravel-5 laravel-7


【解决方案1】:

我认为应该是Hotel 而不是hotel。区分大小写。

如果您在 User 和 Hotel 之间的 1-n 和 Hotel 和 Reservation 之间的 1-n 之间有关系,那应该更容易。

<?php

$reservation = User::with('hotels', 'hotels.reservations')->find(Auth::id());


【讨论】:

    【解决方案2】:

    我不太了解你,但试试这个

    public function index()
    {
        $myhotels = Hotel::where('created_by',Auth::user()->id)->pluck('id');
    
     $reservations = Reservation::where('hotel_id',$myhotels)->get();
    
        return view('moderateur/reservation',compact('reservations'));
    }
    

    【讨论】:

      猜你喜欢
      • 2016-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多