Laravel transactions with logic - php

I try to have logic in my transaction, but it just keeps adding the data to the database and I don't know what's going wrong...
The code I currently have:
public function addQuote($customer_id, Request $request)
{
//try catch, since there can be errors in it
DB::transaction(function() use ($customer_id, $request) {
try {
// Make a project for a quote, but not a main project, to not interfere with other existing projects
$quote_project = QuoteProject::create([
'projectnumber' => $request->project_number,
'name' => $request->project_name,
'address' => $request->project_address,
'zipcode' => $request->project_zipcode,
'city' => $request->project_city,
'country' => $request->project_country ?? 'BE',
'customer_id' => $customer_id,
]);
// Make a quote
$quote = Quote::create([
'status_id' => 1, // assign pending status
'reference' => $request->reference,
'number' => rand(),
'department_id' => $request->department,
'project_id' => $quote_project->id, // Created project id
'location_id' => ($request->location === 0) ? null : $request->location, // location -> can be 0 as value, if so, leave empty
'customer_id' => $customer_id,
'contact_id' => $request->contact_id,
'layout_id' => $request->layout_id,
'seller_id' => $request->seller_id,
'date_from' => Carbon::createFromFormat('d/m/Y', $request->date_from)->format('Y-m-d'),
'date_till' => Carbon::createFromFormat('d/m/Y', $request->date_till)->format('Y-m-d'),
'document_number' => $request->document_number,
'customer_number' => $request->customer_number,
'vat_type_id' => $request->vat_type_id,
'vat_tariff_id' => $request->vat_tariff_id,
]);
$quote->conditions()->attach($request->payment_conditions);
if (isset($request->head_group)) {
// set a global sync data variable
$sync_data = [];
// Loop over all the head groups
foreach ($request->head_group as $key => $head_group) {
// create or update head group
$created_head_group = ArticleGroup::updateOrCreate([
'quote_id' => $quote->id,
'name' => $head_group ?? ''
], [
'comment' => $request->head_group_comment[$key] ?? '',
'head_group' => 1,
'parent_group' => null,
'bold' => filter_var($request->bold_head[$key], FILTER_VALIDATE_BOOLEAN) ?? false,
'italic' => filter_var($request->italic_head[$key], FILTER_VALIDATE_BOOLEAN) ?? false,
'underline' => filter_var($request->underline_head[$key], FILTER_VALIDATE_BOOLEAN) ?? false,
'color' => str_replace('#', '', $request->color_head[$key]) ?? null
]);
// Loop over the sub groups in a main group
foreach ($request->sub_group[$key] as $s_key => $sub_group) {
// Create or update a subgroup
$created_sub_group = ArticleGroup::updateOrCreate([
'quote_id' => $quote->id,
'name' => $sub_group ?? ''
], [
'comment' => $request->sub_group_comment[$key][$s_key] ?? '',
'head_group' => 0,
'parent_group' => $created_head_group->id,
'bold' => filter_var($request->bold_sub[$key][$s_key], FILTER_VALIDATE_BOOLEAN) ?? false,
'italic' => filter_var($request->italic_sub[$key][$s_key], FILTER_VALIDATE_BOOLEAN) ?? false,
'underline' => filter_var($request->underline_sub[$key][$s_key], FILTER_VALIDATE_BOOLEAN) ?? false,
'color' => str_replace('#', '', $request->color_sub[$key][$s_key]) ?? null
]);
// Loop over the articles in the subgroup
foreach ($request->articles[$key][$s_key] as $a_key => $article_id) {
if (isset($request->articles[$key][$s_key])) {
$id = explode('-', $article_id);
$sync_data[$id[0]] = [
'name' => $request->custom_article_name[$key][$s_key][$a_key],
'quantity' => $request->quantity[$key][$s_key][$a_key],
'thickness' => $request->thickness[$key][$s_key][$a_key],
'price' => $request->article_price[$key][$s_key][$a_key],
'description' => $request->description[$key][$s_key][$a_key],
'group_id' => $created_sub_group->id
];
}
}
}
}
$quote->articles()->sync($sync_data); // sync the articles
}
// return data for ajax call, since the wizard works via an ajax call submit
return url('customers/' . $customer_id . '/quotations/');
} catch (\Exception $ex) {
return response()->json(['error' => $ex->getMessage()], 500);
}
}
);
}
If someone could explain me what I'm doing wrong here, that would be a really great help!

This is how it works. If inside DB::transaction exception is thrown then transaction is rolled back automatically. However in your implementation exception is not thrown because you catch it inside transaction and just try to return error response (what by the way won't work because you miss return in DB::transaction(function() use ($customer_id, $request) { line).
The easiest way to solve is to catch exception not inside DB::transaction but outside of it - then it will behave as you expected, transaction will be rolled back.
Alternative solution in some cases it not using DB::transaction but instead using manual:
DB::beginTransaction();
DB::rollBack();
DB::commit();
as described in documentation.

Related

How to attach different picture for each product type

I am building an eCommerce website and I want to add different combinations for the same product.
I use jQuery to add extra input fields as well.
I can successfully insert all data related to product_variation things like color, weight, type, qty but except images.
My database tables: products, product_images, product_variations.
In product_variations table I have field product_variation_image which I want to use for storing image for specific product type. I just don't know how to insert image there for specific product type, yes I know I need to use foreach loop but the problem is I'm already using foreach loop for product variation data, I tried so many different things nesting foreach into foreach but nothing seems to work.
P.S product_images table I'm only use for product basic images also for products which don't have any type.
I will leave my code below... with some comments on where the problem might be.
public function product_store(Request $request)
{
// PRODUCT TYPE VARIABLES //
$productTypes = $request->product_type;
$productColors = $request->product_color;
$productWeight = $request->product_weight;
$productQtys = $request->product_qty;
$productPrices = $request->product_variation_price;
$productTypeImages = $request->file('product_variation_image');
// PRODUCT TYPE VARIABLES ENDS //
// PRODUCT COVER IMAGE //
$productCoverImage = $request->file('product_cover_image');
$productCoverImageNewName = hexdec(uniqid()).'.'.$productCoverImage->getClientOriginalExtension();
Image::make($productCoverImage)->resize(917,1000)->save('upload/products/cover-images/'.$productCoverImageNewName);
$productCoverImageLink = 'upload/products/cover-images/'.$productCoverImageNewName;
// PRODUCT COVER IMAGE ENDS //
if(!empty(implode($productTypes))) {
$productId = Products::insertGetId([
'category_id' => $request->category_id,
'sub_category_id' => $request->sub_category_id,
'sub_sub_category_id' => $request->sub_sub_category_id,
'product_name' => $request->product_name,
'product_link' => strtolower(str_replace(' ','-', $request->product_name)),
'product_desc' => $request->product_desc,
'product_short_desc' => $request->product_short_desc,
'product_price' => $request->product_price,
'product_discount' => $request->product_discount,
'product_cover_image' => $productCoverImageLink,
'new_product' => $request->new_product,
'featured' => $request->featured,
'special_offer' => $request->special_offer,
'status' => $request->status,
]);
// PRODUCT TYPE IMAGES //
foreach ($productTypeImages as $img) {
$productTypeImagesNewName = hexdec(uniqid()) . '.' . $img->getClientOriginalExtension();
Image::make($img)->resize(917, 1000)->save('upload/products/product-images/' . $productTypeImagesNewName);
$productTypesImagesLink = 'upload/products/product-images/' . $productTypeImagesNewName;
}
// PRODUCT TYPE IMAGES ENDS //
foreach ($productTypes as $id => $key) {
ProductVariations::insert([
'product_id' => $productId,
'product_type' => $productTypes[$id],
'product_color' => $productColors[$id],
'product_weight' => $productWeight[$id],
//'product_variation_image' => $productTypesImagesLink[$id], <--- PROBLEM HERE
'product_variation_qty' => $productQtys[$id],
'product_variation_price' => $productPrices[$id],
'created_at' => Carbon::now()
]);
}
} else {
$productId = Products::insertGetId([
'category_id' => $request->category_id,
'sub_category_id' => $request->sub_category_id,
'sub_sub_category_id' => $request->sub_sub_category_id,
'product_name' => $request->product_name,
'product_link' => strtolower(str_replace(' ','-', $request->product_name)),
'product_desc' => $request->product_desc,
'product_short_desc' => $request->product_short_desc,
'product_price' => $request->product_price,
'product_discount' => $request->product_discount,
'product_cover_image' => $productCoverImageLink,
'new_product' => $request->new_product,
'featured' => $request->featured,
'special_offer' => $request->special_offer,
'status' => $request->status,
'created_at' => Carbon::now()
]);
}
// PRODUCT MULTIPLE IMAGES //
$productImages = $request->file('product_image');
foreach ($productImages as $productImage) {
$productImagesNewName = hexdec(uniqid()).'.'.$productImage->getClientOriginalExtension();
Image::make($productImage)->resize(917,1000)->save('upload/products/product-images/'.$productImagesNewName);
$productImagesLink = 'upload/products/product-images/'.$productImagesNewName;
ProductImages::insert([
'product_id' => $productId,
'product_image' => $productImagesLink,
'created_at' => Carbon::now()
]);
}
// PRODUCT MULTIPLE IMAGES ENDS //
$notification = array(
'message' => 'Product Inserted!',
'alert-type' => 'success'
);
return redirect()->back()->with($notification);
}
I have come up with different solution.
After inserting product with multiple images and if product has any variations, I redirect user to another page. Where he can select image for each type if he wants to. And I'm inserting only image id from product_images table, to product_variations table field - product_variation_image.
By the way, I use a jQuery plugin called image-picker for that. And now everything works and my client is happy with that.
Product function:
public function product_store(Request $request)
{
// PRODUCT TYPE VARIABLES //
$productTypes = $request->product_type;
$productColors = $request->product_color;
$productWeight = $request->product_weight;
$productQtys = $request->product_qty;
$productPrices = $request->product_variation_price;
$productTypeImages = $request->file('product_variation_image');
// PRODUCT TYPE VARIABLES ENDS //
// PRODUCT COVER IMAGE //
$productCoverImage = $request->file('product_cover_image');
if($productCoverImage) {
$productCoverImageNewName = hexdec(uniqid()) . '.' . $productCoverImage->getClientOriginalExtension();
Image::make($productCoverImage)->resize(917, 1000)->save('upload/products/cover-images/' . $productCoverImageNewName);
$productCoverImageLink = 'upload/products/cover-images/' . $productCoverImageNewName;
} else {
$productCoverImageLink = 'upload/no-image/image.png';
}
// PRODUCT COVER IMAGE ENDS //
if(!empty(implode($productTypes))) {
$productId = Products::insertGetId([
'category_id' => $request->category_id,
'sub_category_id' => $request->sub_category_id,
'sub_sub_category_id' => $request->sub_sub_category_id,
'product_name' => $request->product_name,
'product_link' => strtolower(str_replace(' ','-', $request->product_name)),
'product_desc' => $request->product_desc,
'product_short_desc' => $request->product_short_desc,
'product_price' => $request->product_price,
'product_discount' => $request->product_discount,
'product_cover_image' => $productCoverImageLink,
'new_product' => $request->new_product,
'featured' => $request->featured,
'special_offer' => $request->special_offer,
'status' => $request->status,
]);
foreach ($productTypes as $id => $key) {
ProductVariations::insert([
'product_id' => $productId,
'product_type' => $productTypes[$id],
'product_color' => $productColors[$id],
'product_weight' => $productWeight[$id],
'product_variation_qty' => $productQtys[$id],
'product_variation_price' => $productPrices[$id],
'created_at' => Carbon::now()
]);
}
} else {
$productId = Products::insertGetId([
'category_id' => $request->category_id,
'sub_category_id' => $request->sub_category_id,
'sub_sub_category_id' => $request->sub_sub_category_id,
'product_name' => $request->product_name,
'product_link' => strtolower(str_replace(' ','-', $request->product_name)),
'product_desc' => $request->product_desc,
'product_short_desc' => $request->product_short_desc,
'product_price' => $request->product_price,
'product_discount' => $request->product_discount,
'product_cover_image' => $productCoverImageLink,
'new_product' => $request->new_product,
'featured' => $request->featured,
'special_offer' => $request->special_offer,
'status' => $request->status,
'created_at' => Carbon::now()
]);
}
// PRODUCT MULTIPLE IMAGES //
$productImages = $request->file('product_image');
if($productImages) {
foreach ($productImages as $productImage) {
$productImagesNewName = hexdec(uniqid()) . '.' . $productImage->getClientOriginalExtension();
Image::make($productImage)->resize(917, 1000)->save('upload/products/product-images/' . $productImagesNewName);
$productImagesLink = 'upload/products/product-images/' . $productImagesNewName;
ProductImages::insert([
'product_id' => $productId,
'product_image' => $productImagesLink,
'created_at' => Carbon::now()
]);
}
}
// PRODUCT MULTIPLE IMAGES ENDS //
$notification = array(
'message' => 'Product Inserted!',
'alert-type' => 'success'
);
if(!empty(implode($productTypes))) {
return redirect()->route('admin.product-variations-images-settings', ['id' => $productId])->with($notification);
} else {
return redirect()->route('admin.products')->with($notification);
}
}
Functions for image varations:
public function product_variations_images_settings($id)
{
$productVariations = ProductVariations::where('product_id', $id)->get();
$productImages = ProductImages::where('product_id', $id)->get();
return view('administrator.pages.products.product-variations', compact('productVariations','productImages'));
}
public function product_variations_images_store(Request $request)
{
$productVariationId = $request->id;
$productImageId = $request->product_variation_image;
$array = array_combine($productVariationId, $productImageId);
foreach ($array as $id => $key){
//dd($key);
ProductVariations::findOrFail($id)->update([
'product_variation_image' => $key,
'updated_at' => Carbon::now()
]);
}
$notification = array(
'message' => 'Images Set!',
'alert-type' => 'success'
);
return redirect()->route('admin.products')->with($notification);
}

How to get the feedback of DB insert laravel

my database insert query is as follows
DB::table('job_details')->insert([
'job_id' => $jobId,
'item_id' => $itemId,
'type_id' => $typeId,
'qty' => $qnty,
'laminating' => $laminating,
'mat_id' => $matId,
'rates' => $rates,
'sqft' => $sqft,
'ups' => $ups,
'master_qty' => $masterQnty
]);
and I want to get the status if the query was successful or failed.
The insert method return a boolean you can save the result in a variable and check if the result is true.
$queryState = DB::table('job_details')->insert([...])
if($queryState) {
// the query succeed
} else {
// the query failed
}
While performing the DB operations in laravel the method will return a response either true or false also for catching exceptions you can keep the code in try catch block.
try{
$response= DB::table('job_details')->insert([
'job_id' => $jobId,
'item_id' => $itemId,
'type_id' => $typeId,
'qty' => $qnty,
'laminating' => $laminating,
'mat_id' => $matId,
'rates' => $rates,
'sqft' => $sqft,
'ups' => $ups,
'master_qty' => $masterQnty
]);
if($response)
echo 'Query was successfull';
else
echo 'There was some error';
}catch{
print_r($e->getMessage);
}

Laravel inserts two user records at register, how to prevent this?

I am creating an application where I have to insert a user at registration with custom fields. As found online, i customised the create method in the Laravel RegisterController. However, now the application inserts two user records whenever I register a new user. Can someone help me with this please?
Here is the code of my create method in the RegisterController
protected function create(array $data)
{
/********************************************************************************
* CALCULATE ALL THE NEEDED DATA FOR THE USER
********************************************************************************/
// Delete the uncompleted registration
UncompletedRegistration::deleteByEmail($data['email']);
// Set the right values based on the filled values
$compercentagecreative = 0.0;
$compercentagenotcreative = 0.0;
$creativepercent = 0.0;
switch ($data['headjob']) {
case 1:
$compercentagecreative = config('constants.percentageRates.comPercentageCreative.headjob');
$compercentagenotcreative = config('constants.percentageRates.comPercentageNotCreative.headjob');
$creativepercent = config('constants.percentageRates.creativePercent.headjob');
break;
case 2:
$compercentagecreative = config('constants.percentageRates.comPercentageCreative.notheadjob');
$compercentagenotcreative = config('constants.percentageRates.comPercentageNotCreative.notheadjob');
$creativepercent = config('constants.percentageRates.creativePercent.notheadjob');
break;
default:
$compercentagecreative = config('constants.percentageRates.comPercentageCreative.headjob');
$compercentagenotcreative = config('constants.percentageRates.comPercentageNotCreative.headjob');
$creativepercent = config('constants.percentageRates.creativePercent.headjob');
break;
}
// Format the VAT number
$data['vatnumber'] = Helper::formatVatNumber($data['vatnumber']);
$isVatValid = false;
try {
// Check if vat is valid
$response = Helper::checkVat($data['vatnumber']);
$responseArray = json_decode($response);
$isVatValid = $responseArray->valid;
} catch (\Exception $exception) {
$isVatValid = false;
}
// Generate an activation key
$activationKey = md5(uniqid('CS', true));
/********************************************************************************
* CREATE THE USER IN THE DATABASE
********************************************************************************/
// Create the user and insert in the database
return User::create([
'usertype' => config('constants.userTypes.USER'),
'registeredon' => strtotime(date("Y-m-d H:i:s")),
'activationkey' => $activationKey,
'language' => 'nl',
'email' => Helper::truncate($data['email']),
'fullname' => Helper::truncate($data['lastname'] . ' ' . $data['firstname']),
'firstname' => Helper::truncate($data['firstname']),
'lastname' => Helper::truncate($data['lastname']),
'password' => Hash::make($data['password']),
'lastloginon' => strtotime('now'),
'lastloginip' => $_SERVER['REMOTE_ADDR'],
'activatedon' => strtotime(date('Y-m-d H:i:s', strtotime('1970-01-01 00:00:00'))),
'deleted' => false,
'companyname' => Helper::truncate($data['companyname']),
'street' => Helper::truncate($data['street']),
'number' => Helper::truncate($data['number']),
'city' => Helper::truncate($data['city']),
'zipcode' => Helper::truncate($data['zipcode']),
'vatnumber' => Helper::truncate($data['vatnumber']),
'validvat' => $isVatValid,
'website' => Helper::truncate($data['website']),
'phonenumber' => Helper::truncate($data['phonenumber']),
'bankname' => Helper::truncate($data['bank']),
'iban' => Helper::truncate($data['iban']),
'bicswift' => Helper::truncate($data['bicswift']),
'paymentremindermode' => $data['paymentremindermode'],
'invoicecreationremindermode' => 2,
'nettosavedmailmode' => 1,
'zombiemailsent' => 0,
'zombiemail180sent' => 0,
'nettosavedperinvoicmailemode' => 1,
'logo' => NULL,
'emailaccepted' => false,
'contractaccepted' => false,
'compercentagecreative' => $compercentagecreative,
'compercentagenotcreative' => $compercentagenotcreative,
'contractdate' => date("Y-m-d H:i:s"),
'creativepercent' => $creativepercent,
'activity' => $data['activity'],
'headjob' => $data['headjob'],
'template' => config('constants.templates.ORIGINAL'),
'freebtw' => isset($data['freebtw']) ? ($data['freebtw'] == "on" ? true : false) : false,
'refid' => Input::get('invite_id'),
'api_key' => Helper::generateRandomString(40),
'allowed_quotation' => true,
'send_bcc' => false
]);
}

Print_r doesn't return anything; var_dump shows NULL

I'm new to web dev and I'm experimenting with Braintree webhooks. I'm using their create submerchant example code to create a submerchant and then supposedly a notification is supposed to reach my server that says if it was successful or not.
My method: I refresh the submerchant.php page (I'm using Wordpress on a NameCheap server), which then echo's "Success!". Then I go to the webhooks.php page and refresh it. However, the var_dump's only return NULL NULL and the print_r's don't return anything. Why does print_r not show anything?
submerchant.php - this creates the submerchant when I set $one = 1 and set a new id for the submerchant
<?php
require_once(__DIR__ . '/../braintree/lib/Braintree.php');
Braintree_Configuration::environment('sandbox');
Braintree_Configuration::merchantId('A');
Braintree_Configuration::publicKey('B');
Braintree_Configuration::privateKey('C');
function fd_create_sm() {
$one;
$one = 1;
if($one=1) {
$merchantAccountParams = [
'individual' => [
'firstName' => 'Janez',
'lastName' => 'Doe',
'email' => 'jane#14ladders.com',
'phone' => '5553334444',
'dateOfBirth' => '1981-11-19',
'ssn' => '456-45-4567',
'address' => [
'streetAddress' => '111 Main St',
'locality' => 'Chicago',
'region' => 'IL',
'postalCode' => '60622'
]
],
'business' => [
'legalName' => 'Jane\'s Ladders',
'dbaName' => 'Jane\'s Ladders',
'taxId' => '98-7654321',
'address' => [
'streetAddress' => '111 Main St',
'locality' => 'Chicago',
'region' => 'IL',
'postalCode' => '60622'
]
],
'funding' => [
'descriptor' => 'Red Ladders',
'destination' => Braintree_MerchantAccount::FUNDING_DESTINATION_BANK,
'email' => 'funding#blueladders.com',
'mobilePhone' => '5555555555',
'accountNumber' => '1123581321',
'routingNumber' => '071101307'
],
'tosAccepted' => true,
'masterMerchantAccountId' => "na",
'id' => "green_ladders"
];
$result = Braintree_MerchantAccount::create($merchantAccountParams);
$result->success;
if($result->success) {
echo 'Success!';
} else {
print_r($result->errors);
$errordata;
echo '***********';
$BT_Errors = new Braintree_Error_ErrorCollection($errordata);
echo '***********';
$BT_Errors->deepAll();
echo '***********';
$BT_Errors->onHtmlField("transaction[amount]");
}
$result->merchantAccount->status;
$result->merchantAccount->id;
// "blue_ladders_store"
$result->merchantAccount->masterMerchantAccount->id;
// "14ladders_marketplace"
$result->merchantAccount->masterMerchantAccount->status;
// "active"
} else {
return;
}
}
fd_create_sm();
?>
webhooks.php
<?php
var_dump($_POST['bt_signature']);
var_dump($_POST['bt_payload']);
print_r($_POST['bt_signature']);
print_r($_POST['bt_payload']);
?>
Most likely, the outputted data is stored within some output buffer. If you're pretty sure you want to debug your code this way, try adding wp_die(); call right after you output data using print_r. That should help!
One more thing: sometimes some of the code (not this particular case) is actually never outputted due to more complex data flow. For this cases it might be a good idea to use some 3-rd party debugging tools or, if you're looking for simpler solution, you can write some of the output to some log file, and check the file afterwards.
Good Luck!
add die; after last line of print_r()

foreach loop not working with laravel queue

I am using sync (local driver) for pushing up a queue in a update method of EmailCampaignController, which uses another method of the same controller named emailQueue
like this
Queue::push('EmailNewsletterController#emailQueue', array('campaign_id' => $campaign_id));
The emailQueue uses a foreach loop which runs correctly for once after that it gives error as if the $campaign_id is undefined
here is the emailQueue method
public function emailQueue($job, $data) {
// Queue Starts here
$emailCampaign = EmailCampaign::find($data['campaign_id']);
$emailCampaign->status = 'In Progress';
$emailCampaign->last_activity = Carbon::now();
$emailCampaign->save();
$data = $emailCampaign->emailCampaignNewsletter;
$contacts = $emailCampaign->contactList->contacts;
foreach ($contacts as $contact) {
$emailBody = [
'message' => [
'subject' => $data['email_title'],
'html' => $data['email_body'],
'from_email' => $data['from_email'],
'to' => [['email' => $contact['email_address']]]
]
];
$response = Mandrill::request('messages/send', $emailBody);
EmailCampaignRecord::create([
'email_campaign_id' => $data['campaign_id'],
'mandrill_email_id' => $response[0]->_id,
'status' => $response[0]->status,
'to_email' => $contact['email_address']
]);
$contact->last_activity = Carbon::now();
$contact->save();
}
$emailCampaign->status = 'Sent';
$emailCampaign->save();
$job->delete();
// Ends here
}
What am I doing wrong here? why is it not working like a normal loop ?
The problem was with email_campaign_id to be null because $data['campaign_id'] was null the correct foreign key was $data['email_campaign_id'] that's what stopped the process - I should have tested it before putting it in the queue
after changing the code
EmailCampaignRecord::create([
'email_campaign_id' => $data['campaign_id'],
'mandrill_email_id' => $response[0]->_id,
'status' => $response[0]->status,
'to_email' => $contact['email_address']
]);
to
EmailCampaignRecord::create([
'email_campaign_id' => $data['email_campaign_id'],
'mandrill_email_id' => $response[0]->_id,
'status' => $response[0]->status,
'to_email' => $contact['email_address']
]);
the problem was solved

Categories