-1

I write code for insert booking data into my database when inserting new data it will create new row so I'm getting duplicate data's so I need to store the data if schedules_id equal to exists schedules_id store the seat data into that table an array format, here how I did.

$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';

user3837868
  • 917
  • 1
  • 12
  • 24

3 Answers3

0

try this

Assume Bookings is Model

$matchThese = ['schedules_id' => $schedules_id];

Bookings::updateOrCreate($matchThese,[
    'seat'  => implode(',', $seat)
]);

or

$data = $request->all();

$data['seat'] = implode(',', $seat);

$matchThese = ['schedules_id' => $schedules_id];

Bookings::updateOrCreate($matchThese, $data);

Make sure you added columns in $fillable in Bookings model

You can make casting in your model

protected $casts    = [
        'seat'           =>  'array',
    ];

If you are using Array casting then $data['seat'] should be array

bhavinjr
  • 1,663
  • 13
  • 19
0

You can use the updateOrCreate method on your model, so try this:

$booking = Bookings::updateOrCreate(
    ['schedules_id' => $schedules_id, 'users_id' => 4 ], // match the row based on this array
    [ // update this columns
        'buses_id' => $buses_id,
        'routes_id' => $routes_id,
        'seat' => json_encode($seat),
        'price' => $request->price,
        'profile' => 'pending',
    ]

);
nakov
  • 13,938
  • 12
  • 60
  • 110
  • sir i need to store a seat data in an array format JSON encode – user3837868 Apr 05 '19 at 07:39
  • 1
    Assuming your `$seat` is an array you don't need to use `implode` as that converts it to a string, you can directly use `json_encode` on the array, it will turn it into a string. Then the reverse would be `json_decode($seat, TRUE)` this returns array. – nakov Apr 05 '19 at 07:42
  • ty so much sir! – user3837868 Apr 05 '19 at 07:45
  • sir it is updating data, only one data will be stored in column – user3837868 Apr 05 '19 at 12:27
  • then you will need to display your previous data into the view before you update it with additional data. Or use another approach, where you first look for the item, if it exists then update the existing one adding the new fields, or create a new one if it does not exist. – nakov Apr 05 '19 at 12:35
  • prb is i need store all booked seat into one column in a array format, if schedules_id same i need to add another seat into that table after that i need to show all the booked seat into view so how can i do that sir? – user3837868 Apr 05 '19 at 13:13
0
$booking = Bookings::firstOrCreate(
           ['schedules_id' => $schedules_id ],// row to test if schedule id matches existing schedule id
           [ // update this columns
              'buses_id' => $buses_id,
              'routes_id' => $routes_id,
              'seat' => implode(',', $seat),
              'price' => $request->price,
              'profile' => 'pending',
           ]);

You can use this firstORcreate laravel method. More description about its usage is given on this stackoverflow page.

First Or Create

But if you want store the data as an array you can create text or json field in your database for the data and use eloquent serialization to work with it. https://laravel.com/docs/5.8/eloquent-serialization

Another stack overflow example where this question about storing data in json is answered is Laravel : How to store json format data in database?

Matovu Ronald
  • 782
  • 11
  • 13