【发布时间】:2019-04-11 08:59:36
【问题描述】:
我试图在 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"
或任何其他建议对我更有帮助。
提前感谢
【问题讨论】:
-
试试这个例子:php.net/manual/en/function.array-unique.php#116302 或简单的
array_unique() -
键必须是唯一的,如果你把它们组合起来,你会失去除了最后一个之外的所有东西......这是事实,除非你删除键,否则它只是一团糟。
-
@SamiAhmedSiddiqui 试过但得到这个“array_unique() 期望参数 1 是数组,给定对象”
-
它们不是重复的,你打算如何解决这个
"created_at" => "2019-04-09 00:00:00"和"created_at" => "2019-04-10 00:00:00",你只是在“预期”结果中忽略了这一点,有几个“这些”,跨度> -
@ArtisticPhoenix 是的,你说得对,我也想,所以我还有其他方法可以将座位数据传递到刀片或使用 ~profile~ 值吗?