I'm getting the error that $recipient variable is undefined in laravel.
public function sendEmail($input, $recipients){
foreach($recipients as $recipient){
$student = StudentInfo::where('email', '=', $recipient)->first();
Mail::queue('emails.outMail', array('letter' => $input['dept_message']), function($message){
$message->to($recipient, $student->first_name)->subject($input['dept_subject']);
});
}
}
In the closure function, you need to use $recipient.
function($message) use ($recipient){
//code
}
Updated Code
public function sendEmail($input, $recipients){
foreach($recipients as $recipient){
$student = StudentInfo::where('email', '=', $recipient)->first();
Mail::queue('emails.outMail', array('letter' => $input['dept_message']), function($message) use ($recipient) {
$message->to($recipient, $student->first_name)->subject($input['dept_subject']);
});
}
}
Related
I'm trying to save an array for checkout but when i print session it gives null
namespace App\Http\Livewire;
use App\Orders;
use Livewire\Component;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Cart;
use Carbon\Carbon;
public function setAmountForCheckout()
{
if(session()->has('coupon'))
{
session()->put('checkout',[
'discount'=>$this->discount,
'subtotal'=>$this->subtotalAfterDiscount,
'total'=>$this->totalAfterDiscount,
]);
}
else
{
session()->put('checkout',[
'discount'=>0,
'tax'=>Cart::instance('cart')->tax(),
'subtotal'=>Cart::instance('cart')->subtotal(),
'total'=>Cart::instance('cart')->total(),
]);
session()->save();
}
}
public function placeOrder(Request $request)
{
dd(session()->get('checkout'));
$this->validate([
'first_name' => 'required|min:4|string',
'Phone_number' => 'required|digits:11'
]);
$order=new Orders();
$order->user_id=Auth::id();
$order->cust_name=$this->first_name;
$order->phone=$this->Phone_number;
$order->subtotal=session()->get('checkout')['subtotal'];
$order->discount=session()->get('checkout')['discount'];
$order->total=session()->get('checkout')['total'];
$order->status='ordered';
$order->is_shipping=$this->haveShipping ? 1:0;
foreach(Cart::instance('cart')->content() as $items)
{
$orderItem= new OrderItems();
$orderItem->product_id=$items->id;
$orderItem->price=$item->price;
$orderItem->order_id=$order->id;
$orderItem->quantity=$item->qty;
$orderItem->save();
}
if($this->haveShipping)
{
$this->validate([
'Address'=>'required|min:4',
'shipping_fee'=>'required|numeric'
]);
$order->address=$this->Address;
$order->delivery=$this->shipping_fee;
}
$order->save();
Cart::instance('cart')->destroy();
session()->forget('checkout');
}
when i remove dd it gives me the error "Trying to access array offset on value of type null"
I'm trying to find what's wrong with the session.
also i have checked the cart if it works or not but i found that it works well and delivers data.
Remove
session()->save();
This is not needed as per https://laravel.com/docs/8.x/session#storing-data
I have a problem with finding results. I search the 'name' records in the translation table for the occurrence of any of the words.
//Bad result:
return Product::whereIn('id', $ids)->whereHas('translations', function ($query) use ($findTextWildcards) {
foreach ($findTextWildcards as $value) {
$query->orWhere('name', 'like', "%{$value}%");
}
});
//good result but difficult query
return Product::whereIn('id', $ids)->where(function ($query) use ($findTextWildcards) {
foreach ($findTextWildcards as $value) {
$query->whereHas('translations', function ($q) use ($value){
$q->where('name', 'like', "%{$value}%");
});
}
});
Try with this one. Probably the problem was that you didn't wrap this with additional where closure.
return Product::whereIn('id', $ids)
->whereHas('translations', function ($query) use ($findTextWildcards) {
$query->where(function($query) use ($findTextWildCards) {
foreach ($findTextWildcards as $value) {
$query->orWhere('name', 'like', "%{$value}%");
}
});
});
I am using hookActionProductUpdate. I am getting all data updated but not attributes.
This is the code inside hook function:
public function hookActionProductUpdate($params) {
$prestaObject = new ProductCore($params['id_product'], false, Context::getContext()->language->id);
$arrrs = $prestaObject->getFrontFeatures(1);
}
Everything else is updated but the front features I am getting are the older one. Any IDEA?
EDIT: I tried this too, here is my new function:
public function hookActionProductUpdate($params) {
$product = $params['product'];
$arrrs = $product->getFrontFeatures(1);
pr($arrrs);die("No updating :(");
}
You don't need to instantiate a new Object. The product Object should already be contained in $params['product'].
Here is the update() method of Product Class where this Hook is called:
public function update($null_values = false)
{
$return = parent::update($null_values);
$this->setGroupReduction();
// Sync stock Reference, EAN13 and UPC
if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT') && StockAvailable::dependsOnStock($this->id, Context::getContext()->shop->id)) {
Db::getInstance()->update('stock', array(
'reference' => pSQL($this->reference),
'ean13' => pSQL($this->ean13),
'upc' => pSQL($this->upc),
), 'id_product = '.(int)$this->id.' AND id_product_attribute = 0');
}
Hook::exec('actionProductSave', array('id_product' => (int)$this->id, 'product' => $this));
Hook::exec('actionProductUpdate', array('id_product' => (int)$this->id, 'product' => $this));
if ($this->getType() == Product::PTYPE_VIRTUAL && $this->active && !Configuration::get('PS_VIRTUAL_PROD_FEATURE_ACTIVE')) {
Configuration::updateGlobalValue('PS_VIRTUAL_PROD_FEATURE_ACTIVE', '1');
}
return $return;
}
You should then use this code instead:
public function hookActionProductUpdate($params) {
$product = $params['product'];
$arrrs = $product->getFrontFeatures(1);
}
Yeah I got it why, Its a bug in prestashop, It calls the hook AdminProductsController
Hook::exec('actionProductSave', array('id_product' => (int)$this->id, 'product' => $this));
Hook::exec('actionProductUpdate', array('id_product' => (int)$this->id, 'product' => $this));
from an update method which is called first then it executes the feature update code.
INSIDE processupdate function
I found this code
//this update method calls the HOOK and when this hook get executed it updates features in the database.
if ($object->update()) {
// If the product doesn't exist in the current shop but exists in another shop
if (Shop::getContext() == Shop::CONTEXT_SHOP && !$existing_product->isAssociatedToShop($this->context->shop->id)) {
$out_of_stock = StockAvailable::outOfStock($existing_product->id, $existing_product->id_shop_default);
$depends_on_stock = StockAvailable::dependsOnStock($existing_product->id, $existing_product->id_shop_default);
StockAvailable::setProductOutOfStock((int)$this->object->id, $out_of_stock, $this->context->shop->id);
StockAvailable::setProductDependsOnStock((int)$this->object->id, $depends_on_stock, $this->context->shop->id);
}
PrestaShopLogger::addLog(sprintf($this->l('%s modification', 'AdminTab', false, false), $this->className), 1, null, $this->className, (int)$this->object->id, true, (int)$this->context->employee->id);
if (in_array($this->context->shop->getContext(), array(Shop::CONTEXT_SHOP, Shop::CONTEXT_ALL))) {
if ($this->isTabSubmitted('Shipping')) {
$this->addCarriers();
}
if ($this->isTabSubmitted('Associations')) {
$this->updateAccessories($object);
}
if ($this->isTabSubmitted('Suppliers')) {
$this->processSuppliers();
}
if ($this->isTabSubmitted('Features')) {
$this->processFeatures();
}
I have to call a soap service using laravel and done so correctly. This soap service requires me to send a login request prior to sending any other request.
The code I'm using works, but I want to improve by removing the login from all the functions and creating one function.
I tried changing the following for one function:
public function getcard($cardid)
{
SoapWrapper::add(function ($service) {
$service
->name('IS')
->wsdl(app_path().'\giftcard.wsdl')
->trace(true);
});
$data = [
'UserName' => 'xxxx',
'Password' => 'xxxx',
];
$card = [
'CardId' => $cardid,
];
SoapWrapper::service('IS', function ($service) use ($data,$card) {
$service->call('Login', [$data]);
$cardinfo=$service->call('GetCard', [$card]);
dd($cardinfo->Card);
});
}
Into:
public function login()
{
SoapWrapper::add(function ($service) {
$service
->name('IS')
->wsdl(app_path().'\giftcard.wsdl')
->trace(true);
});
$data = [
'UserName' => 'xxxx',
'Password' => 'xxxx',
];
SoapWrapper::service('IS', function ($service) use ($data) {
return $service->call('Login', [$data]);
//$service->call('Login', [$data]);
//return $service;
});
}
public function getcard($cardid)
{
$this->login();
$card = [
'CardId' => $cardid,
];
$cardinfo=$service->call('GetCard', [$card]);
dd($card);
}
But this doesn't work. I also tried it with the commented out part, but that doesn't work. Both options result in an error that it didn't find 'service'.
I know it has something to do with oop, but don't know any other option.
I took this as an example, but I probably implemented it wrong?
So my question is: How do I reuse the login part for all other functions?
Your return statement in the login() method is within the scope of that closure. You need to return the result of the closure as well.
return SoapWrapper::service('IS', function ($service) use ($data) {
return $service->call('Login', [$data]);
});
EDIT:
To explain a little bit. You have a function:
SoapWrapper::service('IS' ,function() {}
Inside of a function : public function login()
If you need to return data from your login() method, and that data is contained within your SoapWrapper::service() method, then both methods need a return statement
Here is my code
/**
* Setting up the view component
*/
$di->setShared('view', function () use ($config) {
$view = new View();
$view->setViewsDir($config->application->viewsDir);
$view->registerEngines(array(
'.volt' => function ($view, $di) use ($config) {
$volt = new VoltEngine($view, $di);
$volt->setOptions(array(
'compiledPath' => $config->application->cacheDir,
'compiledSeparator' => '_'
));
$volt->getCompiler()->addFunction(
'paymentStatus',
function($key)
{
return #"Info::paymentStatus({$key})";
}
);
return $volt;
},
'.phtml' => 'Phalcon\Mvc\View\Engine\Php'
));
return $view;
});
And the error message (expected to be honest to say)
Strict Standards: Non-static method Info::paymentStatus() should not be called statically, assuming $this from incompatible context in /home/zxcvbnm/public_html/app/cache/_home_zxcvbnm_public_html_app_views_invoice_admininvoice.volt.php on line 49
How can I call method dynamically?
If you want to call your method is statically, you must change the method implementation to static:
public static function paymentStatus($key){
...code
}