How to add a custom reason message for a reward action ?
I have created :
$customerId = 1303177;
$points = 10;
$customer = Mage::getModel('customer/customer')->load($customerId);
$reward = Mage::getModel('enterprise_reward/reward')
->setCustomer($customer)
->setWebsiteId(2)
->loadByCustomer();
$reward->setPointsDelta($points)
->setAction(Enterprise_Reward_Model_Reward::REWARD_ACTION_ADMIN)
->setComment('Added programmatically')
->updateRewardPoints();
i like to add something like
$reward->setReason('bonus point');
that would be visible in the reason column of the customer reward history ( back office )
If reason column already exists in the Rewards database table, then all you need is to use
$reward->setReason('bonus point');
$reward->save();
to save the values.
But if reason column doesn't exist then first create a new column reason in the database and then use the above code to save the values in that field.
Related
I have a settings table which i am trying to update. My table is empty and i need to insert some data into it.
I am using eloquent to insert
$s = new Settings;
$s->language = request('language');
$s->sitename = request('sitename');
$s->user_id = Auth::id();
$s->save();
return redirect('settings');
I have come across this function
$se = Settings::findOrNew($id); // if exist then update else insert
The findOrNew requires me to know the id before i can save.
How can insert or update without knowing the id(which may not even exist in the first place).
Well in that cause you can use firstOrNew
since you said your basis is you wanted to check if that user_id already exist in table then you use it as condition instead of id
first -> you use firstOrNew
this function do is
check if condition exist it then if it exist it will just return the first existing data
if not just insert a new data and return the new insert data
$s = User::firstOrNew(array('user_id' => Auth::id()));
Then -> after that you can now use that object and do what you wanted to do on it
$s->language = request('language');
$s->sitename = request('sitename');
$s->save();
i've been building a basic laravel CRUD system, with that i've made a basic create funciton but i want to make it so that if a record already exists in the database and u create the same record again that the Integer u put in the create form add's up by the already existing record example: u have a stock system with a product that has the ammount of 50, and u create the same product with the ammount of 40, and that the 40 add's up with the 50 so that in the database it will say 90. im not entirely sure how to do so.
This is the store function i've made for my application:
public function store(Request $request)
{
// Aantal = ammount (int)
//Producten_Id = foreign_key from the Producten database table
//Locaties_Id = foreign_key from the locaties database table
Voorraad::Create($request->only(['aantal', 'Producten_Id', 'Locaties_Id']));
//var_dump($request);
return redirect(Route('voorraad.index'));
}
if u need more information just let me know and i'll add it to the question
Assuming that you set up default value for this field in database structure, its as simple as:
$voorraad = Voorraad::firstOrCreate($request->only(['Producten_Id', 'Locaties_Id']);
$voorraad->increment('aantal', $request->aantal);
If you want to set default amount for new products in the controller do this instead:
$voorraad = Voorraad::firstOrCreate($request->only(['Producten_Id', 'Locaties_Id']);
if (! $voorraad->aantal) {
$voorraad->aantal = 123; // default
}
else {
$voorraad->aantal += $request->aantal;
}
$voorraad->save();
Generally speaking firstOrCreate method fetches from database record with passed attributes if it exists already or otherwise creates it. See in Laravel docs.
If you have any questions just ask in comments, I'll be happy to explain.
I have a product table which has column name unit_id, I have approximate 15 units data. And I stored a custom key of unit to the product as you can check in the screenshot.
And the Units data is stored in settings table, in a single row and meta_value holds all the units data in json format
Now the problem is I have to run two queries every time for products, need help to know more convenient.
// For Products
Product::where('id', 1)->first();
// For settings
Settings::where('meta_key', 'product_units')->first();
Is there any way so that I attach the Settings data in Product model, the problem we get for multiple products not single.
You can try to use a mutator inside the Products model https://laravel.com/docs/5.1/eloquent-mutators.
public function getProductUnitsAttribute() {
return Settings::where('meta_key', 'product_units')->first();
}
so you can do this to get the settings
$product = Product::where('id', 1)->first();
$product->product_units;
but i don't think that this is a good practice.
I have 3 tables, products, images and product_image. The 3rd one is a pivoting table. Other than product_id and image_id in product_image table, I also have 2 extra fields in this pivoting table: position and type, now for an existing product model A, how do I attach an image to A and at the same time set up its position and type value in that pivoting record? Thanks.
You may try something like this:
$product = Product::find(1);
// if you need also to save the image
$image = new Image(array('foo' => 'bar'));
$product->images()->save($image,array('position'=>'foo', 'type'=>'bar' ));
// if you know image id
$product->images()->attach([$imageId => ['position'=>'foo', 'type'=>'bar']]);
You need just add a array to attach method as second parameter
//for example:
$product->images()->attach($newImage, ['foo'=>'bar']);
It is more eloquent solution.
I have a table RECEIPT and the ID that i created is
REC-201290001 = "REC-"+"YEAR"+"MONTH"+"0001"
I create the number with a String.Format but in order to create the next one i need the last ID i inserted so i can increase it.
Thanks very much for your help.
$query = mysql_query("select value from config where name = 'next_receipt_id'");
$row = mysql_fetch_assoc($query);
$receiptId = "REC-"+"YEAR"+"MONTH"+$row['value'];
$insert_query = mysql_query("INSERT into order (`id`,...) values($receiptId,...)");
$updateReceiptId = mysql_query("update config set value = value+1 where name = 'next_receipt_id');
You need some kind of persistent storage for a counter. You generate fresh IDs using that counter as a seed.
You can use a simple file, or, more elegant, a database for the purpose of the persistent storage. There are hundreds of tutorials in the internet about this.