SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'total_item' cannot be null - laravel

I’m trying to create payment integration snap token request, which is a local payment mostly like PayPal. the problem is I couldn’t get request from datatables transactions, and the error output is Integrity constraint violation: 1048 Column 'total_item' cannot be null.
controller.php
public function index()
{
return view('midtrans.index');
}
public function payment(Request $request)
{
// Set your Merchant Server Key
\Midtrans\Config::$serverKey = env('MIDTRANS_SERVER_KEY');
// Set to Development/Sandbox Environment (default). Set to true for Production Environment (accept real transaction).
\Midtrans\Config::$isProduction = false;
// Set sanitization on (default)
\Midtrans\Config::$isSanitized = true;
// Set 3DS transaction for credit card to true
\Midtrans\Config::$is3ds = true;
$penjualan = Penjualan::find(session('id_penjualan'));
$detail = PenjualanDetail::where('id_penjualan', $penjualan->id_penjualan)->get();
$validator = \Validator::make(request()->all(), [
'id_penjualan' => 'required|string|max:255',
'id_member' => 'required|string|max:255',
'total_item' => 'required|string|max:255',
'total_harga'=> 'required| numeric | min:0.01',
'ppn' => 'required|numeric|min:1',
'diskon' => 'required|numeric|min:1',
'bayar' => 'required|numeric|min:1',
'diterima' => 'required|int|numeric|min:1',
]);
// $validated = $request->validated();
\DB::transaction(function() use($request) { // use($validated)
$penjualan = Penjualan::create([
'id_penjualan' => $request->id_penjualan, // $validated['id_penjualan']
'id_member' => $request->id_member,
'total_item' => $request->total_item,
'total_harga' => $request->total_harga,
'ppn' => $request->ppn,
'diskon' => $request->diskon,
'bayar' => $request->bayar,
'diterima' => $request->diterima,
'id_user' => auth()->user()->id // Auth::user()->id,
]);
$params = array(
'transaction_details' => array(
'order_id' => rand(),
'gross_amount' => $penjualan->bayar,
),
'item_details' => array(
[
'id' => $penjualan->id_penjualan,
'price' => $penjualan->harga_jual,
'quantity' => $penjualan->jumlah,
'name' => $penjualan->nama_produk
]
),
'customer_details' => array(
'first_name' => $request->get('uname'),
'last_name' => '',
'email' => $request->get('email'),
'phone' => $request->get('number')
),
);
$snapToken = \Midtrans\Snap::getSnapToken($params);
$penjualan->snap_token = $snapToken;
$penjualan->save();
$this->response['snap_token'] = $snapToken;
});
return view('midtrans.payment', ['snap_token'=>$snapToken]);
}
public function payment_post(Request $request)
{
$json = json_decode($request->get('json'));
$penjualan = Penjualan::findOrFail($request->id_penjualan);
$penjualan->status = $json->transaction_status;
$penjualan->uname = $request->get('uname');
$penjualan->email = $request->get('email');
$penjualan->number = $request->get('number');
$penjualan->transaction_id = $json->transaction_id;
$penjualan->order_id = $json->order_id;
$penjualan->gross_amount = $json->gross_amount;
$penjualan->payment_type = $json->payment_type;
$penjualan->payment_code = isset($json->payment_code) ? $json->payment_code : null;
$penjualan->pdf_url = isset($json->pdf_url) ? $json->pdf_url : null;
return $order->save() ? redirect(url('/'))->with('alert-success', 'Order berhasil dibuat') : redirect(url('/'))->with('alert-failed', 'Terjadi kesalahan');
}
does anyone can help what I’m missing? thanks

Related

Create model row in Laravel when user_id is required and taken using Auth

I have a simple form a user fills out with some basic information. That form is received and a model row is created with the validated data AND the user_id which is found using Auth::id(). The user_id column is a foreign_key in the table. The problem I have is that I don't know how to create the new model row with the proper user_id.
I have the following in my controller
public function store(Request $request)
{
//
$test = Auth::check();
$user = Auth::id();
if ($test == false){
return redirect('/login');
}
else {
$data = request()->validate ([
'first_name' => '',
'last_name' => '',
'phone' => '',
'email' => '',
'city' => '',
'state' => '',
'zip_code' => '',
'procedure' => '',
'travel' => '',
'timetable' => '',
]);
$procedurePlan = ProcedurePlan::create($data);
// $procedurePlan = new ProcedurePlan;
$procedurePlan->user_id=$user;
$procedurePlan->save();
$procedurePlan->update($data);
return redirect ('/procedure-planner');
}
}
$procedurePlan = ProcedurePlan::create(['user_id' => $user]);
$procedurePlan->update($data);
Save it only once:
$procedurePlan = new ProcedurePlan($data);
$procedurePlan->user_id = Auth::id();
$procedurePlan->save();

ErrorException (E_ERROR) Trying to get property 'birthday' of non-object

public function profile_settings()
{
$student_information = Auth::user()->personal_information;
return view('StudentViews.profile_settings' , compact('student_information'));
}
public function save_profile_information(Request $request)
{
$flag = Auth::user()->personal_information;
if ($flag != "") {
$PersonalInformation = Auth::user()->personal_information;
$PersonalInformation->first_name = $request->first_name;
$PersonalInformation->last_name = $request->last_name;
$PersonalInformation->birthday = $request->birthday;
$PersonalInformation->email = Auth::user()->email;
$PersonalInformation->gender = Auth::user()->gender;
$PersonalInformation->occupation = Auth::user()->occupation;
$PersonalInformation->phone = $request->phone;
$PersonalInformation->website = $request->website;
$PersonalInformation->country = $request->country;
$PersonalInformation->province = $request->province;
$PersonalInformation->city = $request->city;
$PersonalInformation->description = $request->description;
$PersonalInformation->birthplace = $request->birthplace;
$PersonalInformation->marital_status = $request->marital_status;
$PersonalInformation->facebook = $request->facebook;
$PersonalInformation->twitter = $request->twitter;
$PersonalInformation->linkedin = $request->linkedin;
$PersonalInformation->google_plus = $request->google_plus;
$PersonalInformation->save();
$user = Auth::user();
$user->first_name = $request->first_name;
$user->last_name = $request->last_name;
$user->phone = $request->phone;
$user->save();
}
else {
PersonalInformation::create([
'user_id' => Auth::user()->id,
'first_name' => $request->first_name,
'last_name' => $request->last_name,
'birthday' => $request->birthday,
'email' => Auth::user()->email,
'phone' => $request->phone,
'country' => $request->country,
'province' => $request->province,
'city' => $request->city,
'description' => $request->description,
'gender' => Auth::user()->gender,
'occupation' => Auth::user()->occupation,
'website' => $request->website,
'birthplace' => $request->birthplace,
'marital_status' => $request->marital_status,
'facebook' => $request->facebook,
'twitter' => $request->twitter,
'linkedin' => $request->linkedin,
'google_plus' => $request->google_plus,
]);
}
}
value = {{ ($student_information->birthday)?$student_information->birthday:""}}
it gives you an error because it has no value so replace the value in the blade page by the previous line, hope it will work.
Check by using is_object() function. It returns TRUE if it is an object.
And also check value is null or empty.
if (( $student_information != null) && (is_object($student_information))) {
// code here
// print any property here
}
Then Error will be fixed. If you print a property that doesn't contain value shows error like this.

use cookie to store and retrieve order data in laravel

use cookie to store and retrieve order data in laravel
I want to use cookie to store and retrieve order data in store order:
public function store(Request $request, $serviceId) {
$request->validate([
'company_id' => 'required',
'user_id' => 'required',
'individual_count' => 'required',
'date' => 'required',
'time' => 'required',
'total_price' => 'required',
'is_home' => 'required',
]);
$request['date'] = date('Y-m-d H:i:s', strtotime($request->date . $request->time));
$request['total_price'] = explodeBySpace($request->total_price)[0];
$request['service_id'] = Hashids::decode($serviceId)[0];
session([ 'totalOrderPrice' => $request['total_price'] ]);
session([ 'companyName' => $request->company_name ]);
session([ 'individualCount' => $request->individual_count ]);
session([ 'orderDate' => $request['date'] ]);
// dd($request->all());
$created = Orders::create($request->all());
if ($created) {
session(['orderId' => $created->id]);
Cookie::make('orderId', $created->id, 180); // ?
return redirect()->route('payment.method');
}
return redirect()->route('web.orders.create', $serviceId)->with('alert', 'error');
}
to retrieve order data for payment operation :
public function storeReceipt(Request $request, $method) {
$request->validate([
'price' => 'required|numeric',
]);
$request['order_id'] = $request->cookie('orderId');
$request['method'] = $method;
$created = Payment::create($request->all());
return $created->count() > 0
? redirect()->route('home')->with('alert', 'success')
: redirect()->route('payment/method/create', 'receipt')->with('alert', 'error');
}
but this error occurs
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'order_id' cannot be null
what is the wrong
Hi you can try Cookie::get(); to retrieve the cookies
public function storeReceipt(Request $request, $method) {
$request->validate([
'price' => 'required|numeric',
]);
$request['order_id'] = Cookie::get('orderId');
$request['method'] = $method;
$created = Payment::create($request->all());
return $created->count() > 0
? redirect()->route('home')->with('alert', 'success')
: redirect()->route('payment/method/create', 'receipt')->with('alert', 'error');
}

creating default object from empty value in laravel 5

I'm trying to make two functions in controller that have post action and that are in the same page.
My Controller
public function store(Request $request)
{
$status = DB::table('analytics')->where('dienstleistung', '!=', '')->get();
//Save data
$rules = [
'site_id' => 'required',
'dienstleistung' => 'required',
'objekt' => 'required',
'zimmer' => 'required',
'vorname' => 'required',
'name' => 'required',
'strasse' => 'required',
'ort' => 'required',
'plz' => 'required',
'tel' => 'required',
'email' => 'required|email',
'reinigungstermin' => 'required',
'gekommen' => 'required',
'message' => 'required',
'status' => 'required',
'answer' => 'required',
'notiz' => 'required',
'userId' => 'required',
];
$validator = Validator::make(Input::all(), $rules);
if($validator->fails()) {
return Redirect::to('anfrage')
->withErrors($validator)
->withInput();
}
else {
$anfrage = new Analytic();
$anfrage->site_id = Input::get('site_id');
$anfrage->dienstleistung = Input::get('dienstleistung');
$anfrage->objekt = Input::get('objekt');
$anfrage->zimmer = Input::get('zimmer');
$anfrage->vorname = Input::get('vorname');
$anfrage->name = Input::get('name');
$anfrage->strasse = Input::get('strasse');
$anfrage->ort = Input::get('ort');
$anfrage->plz = Input::get('plz');
$anfrage->tel = Input::get('tel');
$anfrage->email = Input::get('email');
$anfrage->reinigungstermin = Input::get('reinigungstermin');
$anfrage->gekommen = Input::get('gekommen');
$anfrage->message = Input::get('message');
$anfrage->status = Input::get('status');
$anfrage->answer = Input::get('answer');
$anfrage->notiz = Input::get('notiz');
$anfrage->userId = Input::get('userId');
try {
$anfrage->save();
flash()->success(trans('app.success'));
return Redirect::to('anfrage');
} catch (\Exception $e) {
Log::writeException($e);
return Redirect::to('anfrage')
->withErrors($e->getMessage())
->withInput();
}
}
}
public function editItem(Request $request) {
$anfrages = Analytic::find($request['id'] );
$anfrages->status = $request->status;
$anfrages->answer = $request->answer;
$anfrages->notiz = $request->notiz;
$anfrages->save();
return response ()->json( $anfrages );
}
My route:
Route::post('anfrage', 'AnfrageController#store');
Route::post ( 'anfrage', 'AnfrageController#editItem' );
EditItem function is OK, it makes changes when I want to edit data, but when I want to store data, message being displayed is:
creating default object from empty value
So, I need to leave active only one of these function, both are not working.

update not being done no errors are showing using query builder laravel 4.2

i have to update a table and change the value inside it and the table have many column inside it. i tried to use query builder and here is my current code
the query is inside the public function ugetstd($id)
public function ugetstd($id) //update
{
$rules = array(
'firstname' => 'required|max:80|regex:/^[\p{L}\p{N} - ]+$/u',
'middlename' => 'required|max:50|regex:/^[\p{L}\p{N} -]+$/u', //more here
);
$messages = array(
'firstname.required' => 'First name is required',
'firstname.max' => 'First name can only contain 80 characters' //more here
);
$validator = Validator::make(Input::all(), $rules, $messages);
if ($validator->fails())
{
return Redirect::to('view_students/' . $id)
->withErrors($validator)
->withInput(Input::except('password'));
}
else
{
DB::table('dbo_students')->where('StudentID', $id )
->update
(
array
(
'FirstName' => Input::get('firstname'),
'MiddleName' => Input::get('middlename'),
'LastName' => Input::get('lastname'),
'CurrentStatusID' => Input::get('scs'),
'Sex' => Input::get('sex'),
'ReligionID' => Input::get('rel'),
'EthnicityID' => Input::get('eth'),
'StreetAddress' => $enAdd,
'CityID' => Input::get('city'),
'YearLevelID' => Input::get('yearlevel'),
'Telephone' => Input::get('telephone'),
'Birthdate' => Input::get('date'),
'Birthplace' => Input::get('birthplace'),
'SchoolLastAttended' => Input::get('schoollastattended'),
'LastGradeCompleted' => Input::get('lastgradecompleted'),
'CurrentModuleLeft' => Input::get('currentmoduleleft'),
'CurrentModuleCriticalLevel' => Input::get('modulecriticallevel'),
'StudentDescription' => Input::get('description')
)
);
return Redirect::to('view_students/' . $id);
}
}
the thing is it does not execute the code. it does not update anything and it does not throw any error. any ideas what i am doing wrong? thanks

Resources