I have a question - is it possible to pass or share property from controller to model in laravel. Here is some code example of "problem".
Basically I have a model method which is getting product price in given currency.
class Product extends Model
{
public function getPrice()
{
return number_format($this->price_retail / $this->sessionHelper->getCurrentCurrency()->conversion_rate, 2);
}
}
sessionHelper is separate class which is providing information about current currency. I would like to remove this part and use property from controller
In project my productController has access to global variables extended from baseController:
class ProductController extends BaseController
{
protected $product;
public function __construct(Product $product)
{
parent::__construct();
$this->product = $product;
$this->currentCurrency //gives current currency info which i need in model
}
//test function
public function showFirstProductPrice(){
$this->product->first()->getPrice();
}
}
I could do something like passing variable through function like this:
$this->product->first()->getPrice($Variable);
But in view every time i will need to pass $variable. Currently I call model method directly which is calling helper for currency conversion rate and it's working but I guess there is better way to do that.
Have somebody any ideas?
Of course you can pass a variable.
class Product extends Model
{
public function getPrice($variable)
{
return number_format($this->price_retail /$variable, 2);
}
}
in your controller you can do this
class ProductController extends BaseController
{
protected $product;
public function __construct(Product $product)
{
parent::__construct();
$this->product = $product;
$this->currentCurrency //gives current currency info which i need in model
}
//test function
public function showFirstProductPrice(){
$this->product->first()->getPrice($variable);
}
}