mardi 2 avril 2019

How to sort & search a collection instance in Laravel 5

I have a many-to-many pivot relation between two models of a Property, and a date that a user can see if the room is available for reserve or not. Everything is working fine until I want to search for the available date. My user submits two dates, and I want to sort all dates related to that room and show the days between those two user inputs. I know and tried dateBetween, but because I'm using a pivot table, i must somehow do this on sort and search collection instance. This is my collection, and I want to see if it's possible to sort and do an operation on it.

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 [▶]
    }
  ]

Controller

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

Date table

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

Property table

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

Pivot table

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



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2uIUS9b
via IFTTT

Aucun commentaire:

Enregistrer un commentaire