簡體   English   中英

如何將兩個數組值合並為一個?

[英]how to combine two arrays values into one?

我嘗試在schedules_id等於請求schedules_id時獲取預訂座位,然后顯示屬於schedules_id的所有座位,但得到更多分離的數組

插入

$booking               = new Bookings();
$booking->users_id     = 4;
$booking->schedules_id = $schedules_id;
$booking->buses_id     = $buses_id;
$booking->routes_id    = $routes_id;
$booking->seat         = implode(',', $seat);
$booking->price        = $request->price;
$booking->profile      = 'pending';

收藏

class PassengersController extends Controller
{
    public function booking(Request $request)
    {
        //river is used to pass important params with flow of it from page to page
        $seat         = $request->seat;
        $buses_id     = $request->buses_id;
        $schedules_id = $request->schedules_id;
        $data         = Buses::where('buses_id', $buses_id)->first();
        $seat         = json_decode($data->seat_layout, true);
        $front        = json_decode($data->front_layout, true);
        $bookingSeat  = Bookings::whereColumn('schedules_id', 'schedules_id')->get();

        $bookingSeat = $bookingSeat->map(function ($bookSeat) {
            $bookSeat->seat = explode(",", $bookSeat->seat);
            return $bookSeat;
        });

        return view('frontend.booking', ['seat' => $seat, 'buses_id' => $buses_id, 'schedules_id' => $schedules_id, 'front' => $front, 'bookingSeet' => $bookingSeat]);

    }
}

bookings_id users_id schedules_id buses_id routes_id seat price profile
    1           1         6           1       3        1  Null  pending
    2           1         6           1       3        2  Null  pending

我的數組看起來像這樣:

#items: array:2 [▼

    0 => Bookings {#431 ▶}
        "bookings_id" => 1
        "users_id" => 1
        "schedules_id" => 6
        "buses_id" => 1
        "routes_id" => 3
        "seat" => "1"
        "price" => null
        "profile" => "pending"
        "created_at" => "2019-04-09 00:00:00"
        "updated_at" => "2019-04-09 00:00:00"
    1 => Bookings {#432 ▶}
        "bookings_id" => 2
        "users_id" => 1
        "schedules_id" => 6
        "buses_id" => 1
        "routes_id" => 3
        "seat" => "2"
        "price" => null
        "profile" => "pending"
        "created_at" => "2019-04-10 00:00:00"
        "updated_at" => "2019-04-10 00:00:00"

但是我想要什么我不想一次又一次地重復相同的數據。 問題是我的booking.blade.php顯示像2輛公共汽車的座位布局。 例如,如果一輛公共汽車有50個席位,而我在blade.php中獲得100個席位

我的預期結果如下所示:

#items: array:1 [▼

0 => Bookings {#431 ▶}
        "bookings_id" => 1,2
        "users_id" => 1
        "schedules_id" => 6
        "buses_id" => 1
        "routes_id" => 3
        "seat" => "1","2"
        "price" => null
        "profile" => "pending"
        "created_at" => "2019-04-09 00:00:00"
        "updated_at" => "2019-04-09 00:00:00"

或其他建議對我更有用。
謝謝前進

您要合並的不是兩個數組,而是兩個實體,每個實體在您的預訂表中代表一行,我認為它們不會被合並,這是主要原因

  • 每個對象代表數據庫中的一行,如果您像想合並它們那樣進行合並,該如何編輯呢? 刪除一個? 等等...

  • 預訂課程就像是一項“預訂”的藍圖,並非全部

  • 您不必“一次又一次地復制相同的數據”,對於具有不同值的不同對象,您只是擁有相同的屬性名稱:)

可能還有100多個原因,但它們是第一個來到我這里的原因,希望這對您有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM