php


Laravel Mass Assignment Using JSON


These are my models:
Order:
class orders extends Model
{
protected $table = 'orders';
public function getItems()
{
return $this->hasMany('App\order_items');
}
protected $fillable = [
'subtotal','discount_amount','after_discount_price','mechanic_id','owned_cars_id','primary_id','promo_code_string',
'order_status_id'
];
}
Order Items:
class order_items extends Model
{
protected $table = 'order_items';
protected $fillable = [
'order_primary_id','primary_id','service_id','service_name','service_thumbnail','service_orignal_price','discount_amount',
'after_discount_price','service_description','service_classification'
];
}
This is the JSON structure
{
"orderID": null,
"PromoId": 0,
"subtotal": 2500,
"discount": 12,
"discountPrice": 3500,
"mechanic_id": null,
"ownedcarId": 1,
"ownedCarServerId": 0,
"order_status_id": 1,
"order_items": [
{
"order_item_server_id": null,
"order_id": null,
"order_primary_id": 1,
"primary_id": 1,
"service_id": 1,
"service_name": "Car Wash",
"service_thumbnail": "asd",
"service_original_price": 2500,
"discount_amount": 20,
"after_discount_price": 20,
"service_description": "description",
"service_classification": 1,
"service_sub_items":0
}
]
}
For a new order this code works fine:
$order = new order();
$order->user_id = 123;
$order->fill($request->all());
$order->save();
Is there any way i can use $request->all() to save the mass assignable data in the key "order items" directly? Or do i have to do it one by one?
You could try:
foreach($request->order_items as $item){
$orderItem = new OrderItem($item);
$orderItem->save();
}
If you have extra fields in your request that aren't meant to be in your model. You can do:
$input = $request->only(['username', 'password']);
$input = $request->only('username', 'password');
$input = $request->except(['credit_card']);
$input = $request->except('credit_card');
Taken from "Retrieving A Portion Of The Input Data" from https://laravel.com/docs/5.4/requests

Related Links

Laravel 4 application minifying files automatically?
Mutliple try catch php Database
What object should contain which? and is that right?
PHPMailer fails to send mails to #hotmail.com/#live.com
join node value which have same node name with PHP
Can't get php insert to work
can not insert to database in codeigniter
Symfony 2 not rendering Welcome Page as expected
Handling large number of MySQL tables
HtmlPurifierBundle How to configure URI.SafeIframeRegexp in config.yml Symfony2?
Compare with a value and sort in mysql
How to ORDER result according to user interaction
How to push values into array in php
Amazon Place order API
How to display short permalinks in WordPress?
why is PHP_EOL adding a strange character?

Categories

HOME
apache-nifi
opencart
verification
facebook-messenger
seedstack
xcode8.1
schema.org
paypal-ipn
apk
mongoid
dronekit-python
google-sheets-query
gentelella
gprs
firebase-dynamic-links
siesta
alljoyn
multi-step
powermock
trace32
xbox360
heap-dump
phonegap-build
pickle
codefluent
spam
multiple-tables
azure-machine-learning
channel
ini
function-points
web-push
pubxml
gravity-forms-plugin
math.js
bonita
boolean-logic
infopath2010
vcenter
transform
formsauthenticationticket
bluemixtools
cartopy
sbjson
imgur
ejml
yoast
notesview
xcode8.2
ipp-protocol
liteide
windows-media-player
roracle
lightning-workbench
pen
dtd
front-camera
perl-data-structures
maatwebsite-excel
oid
gawk
qtextedit
okio
e
data-management
uibinder
mathnet
flush
perceptron
msgpack
place
espresso
embeddedwebserver
textscan
whitespace-language
kognitio-wx2
comctl32
adhoc-polymorphism
multivariate-testing
tabris
osascript
otl
box2dweb
wso2bam
jchartfx
sensormanager
coalesce
jacob
statechart
donut-chart
firebird1.5
uipangesturerecognizer
datacontracts
cewolf
simile
bass
icicles
subtext
ning
autobench
stretchblt
isapi-redirect
webclient.uploaddata

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile