简体   繁体   中英

Insert data into two columns of a table in Laravel Controller using sql

I have been trying to insert data using query into two columns of a table but there's something missing that its not sending data in the other column named booking_code.
It is inserting the booking_id into the table but not booking_code.

Here is the controller:

 public function store(CreatePaymentRequest $request) { $input = $request->all(); $booking_id = $request->booking_id; $booking_code = Booking::find($booking_id)->booking_code; $this['booking_code'] = $booking_code; $payment = $this->paymentRepository->create($input); Flash::success('Payment saved successfully.'); return redirect(route('admin.payments.index')); } 

Please have a look at the Relevant Documentation Pages

You may also use the create method to save a new model in a single line. The inserted model instance will be returned to you from the method. However, before doing so, you will need to specify either a fillable or guarded attribute on the model, as all Eloquent models protect against mass-assignment by default.

That means that you have to set, in Model, which fields you will allow to be "mass-assigned".

In your case, it will looks something like

class Booking extends Model 
(...)
{
   protected $fillable = ['booking_code', (...)];
}
(...)

In the code you provided though, I can't see how you build an $input variable so maybe the issue is there? Maybe it's just some typo.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM