首页 > 解决方案 > 如何将两个数组值合并为一个?

问题描述

我试图在 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"

或任何其他建议对我更有帮助。
谢谢提前

标签: phplaravel

解决方案


您要合并的不是两个数组,而是两个实体,每个实体代表您的预订表中的一行,在我看来,它们不会被合并,这是主要原因

  • 每个对象代表数据库中的一行,如果您像您想要的那样合并它们,您如何编辑一个?删除一个?ETC...

  • 预订舱位就像一个“预订”的蓝图,而不是全部

  • 您不会“一次又一次地复制相同的数据”,您只需为具有不同值的不同对象获得相同的属性名称:)

可能还有 100 多个原因,但它们是第一个来找我的我希望这可以帮助你


推荐阅读