如何将两个数组值合并为一个?

时间:2019-04-11 08:59:36

标签: php laravel

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

或其他任何建议对我更有用。
谢谢前进

1 个答案:

答案 0 :(得分:0)

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

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

  • 预订类就像一个“预订”的蓝图,并非全部“

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

可能还有100多个原因,但这是第一个来到我这里的原因 希望对您有帮助