Как сортировать и искать экземпляр коллекции в Laravel 5 - PullRequest
0 голосов
/ 02 апреля 2019

У меня есть отношение «многие ко многим» между двумя моделями объекта недвижимости и дата, когда пользователь может увидеть, доступна ли эта комната для резервирования или нет.Все работает хорошо, пока я не хочу искать доступную дату.Мой пользователь отправляет две даты, и я хочу отсортировать все даты, относящиеся к этой комнате, и показать дни между этими двумя пользовательскими вводами.Я знаю и пробовал dateBetween, но так как я использую сводную таблицу, я должен как-то сделать это на экземпляре сортировки и поиска.Это моя коллекция, и я хочу посмотреть, можно ли с ней отсортировать и выполнить операцию.

Collection {#1606 ▼
 #items: array:1 [▼
    0 => Property {#1441 ▼
      #connection: "mysql"
      #table: "properties"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:14 [▶]
      #original: array:14 [▶]
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:1 [▼
        "dates" => Collection {#1605 ▼
          #items: array:5 [▼
            0 => Date {#1586 ▼
              #connection: "mysql"
              #table: "dates"
              #primaryKey: "id"
              #keyType: "int"
              +incrementing: true
              #with: []
              #withCount: []
              #perPage: 15
              +exists: true
              +wasRecentlyCreated: false
              #attributes: array:5 [▼
                "id" => 1
                "date" => "2019-03-31 00:00:00"
                "price" => 21000.0
                "created_at" => "2019-03-31 00:00:00"
                "updated_at" => null
              ]
              #original: array:7 [▶]
              #changes: []
              #casts: []
              #dates: []
              #dateFormat: null
              #appends: []
              #dispatchesEvents: []
              #observables: []
              #relations: array:1 [▶]
              #touches: []
              +timestamps: true
              #hidden: []
              #visible: []
              #fillable: []
              #guarded: array:1 [▶]
            }
            1 => Date {#1587 ▶}
            2 => Date {#1588 ▶}
            3 => Date {#1589 ▶}
            4 => Date {#1590 ▶}
          ]
        }
      ]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #fillable: []
      #guarded: array:1 [▶]
    }
  ]

Контроллер

if (!empty($request->start_date)) 
{
    $pdate = Property::with('dates')->get();
    // Here on pdate i wana do some sorting and searching base on user send start and end date
    dd($pdate);
}

Таблица дат

Schema::create('dates', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->dateTime('date');
    $table->float('price');
    $table->timestamps();
});

Таблица свойств

public function up()
{
    Schema::create('properties', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->integer('type');
        $table->text('title');
        $table->longText('description');
        $table->longText('image');
        $table->float('base_price');
        $table->dateTime('available_start');
        $table->dateTime('available_end');
        $table->integer('base_availability');
        $table->integer('base_capacity');
        $table->integer('max_occupancy');
        $table->float('extra_price');
        $table->timestamps();
    });
}

Сводная таблица

Schema::create('date_property', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->integer('property_id');
    $table->foreign('property_id')->references('id')->on('property');
    $table->integer('date_id');
    $table->foreign('date_id')->references('id')->on('date');
    $table->integer('reserved');
    $table->timestamps();
});
...