如何在Laravel 5中排序和搜索集合实例

时间:2019-04-02 19:34:36

标签: laravel laravel-5 search pivot laravel-collection

我在一个物业的两个模型之间有一个多对多的枢纽关系,还有一个日期,用户可以看到该日期是否可供预订。一切正常,直到我想搜索可用的日期。我的用户提交了两个日期,我想对与该会议室相关的所有日期进行排序,并显示这两个用户输入之间的日期。我知道并尝试过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();
});

0 个答案:

没有答案