OctoberCMS - How to set dynamin value for repeater in filterFields? - laravel

In my OctoberCMS application, I want to change a repeater value depend on another field value change. But it's not working in the filterFields model method or formExtendRefreshFields controller method.
I am trying like bellow,
public function filterFields($fields, $context = null)
{
$fields->item_details->value = [my prepared array value];
}
all other property like label, comments working fine in filterFields as well.

Related

Conditionally assign Laravel's controller values on Construct method

I work with a Laravel porject that uses an array of Model attributes to show it on the view. Unlike most Laravel prokects where do you send a set of data to the view and you choose how to show it on our project that's defined on a Controller's variable.
This variable ($showFields) expects an array of Model's atributes and the values are shown on the view.
The thing is that I need to adapt the shown atributes based on one atrubute's id (vendor_id). My idea is, based on the vendor_id assign one array of attributes or another.
I've been trying with the Controller's constructor method but it's not working as when it's called seems like it does not have the values yet. Is this even possible?
This is the controller's code:
protected $showFields = []; // This is the variable that tells the view what attributes to show
protected $vendorInformation = [...];
protected $noVendorInformation = [...];
public function __construct(QuoteService $quoteService)
{
parent::__construct();
$this->quoteService = $quoteService;
if($this->vendor_id === xx){
array_push($this->showFields, $this->noVendorInformation);
} else {
array_push($this->showFields, $this->vendorInformation);
}
}
I'm working with Laravel 5.7

why is $this->field not always returning a value inside laravel custom attribute model

I have a custom attribute inside my model.
if I try to get the value of i.e $this->id it works but if I add a new column to that model and call it $this->new_column it returns empty even though it has a value. So in order to retrieve it, I have to then initialise the model again inside the custom attribute.
$model = MyModel::find($this->id);
just so I can access $model->new_column
public function getCustomColumnDataAttribute(){
Log::info($this->id); //returns the value
Log::info($this->new_column); //doesn't return the value
$model = MyModel::find($this->id);
Log::info($model->new_column); //returns the value
}
I find it a waste to always initialise the model again just so I can access the values. Or am I missing something here with how $this works inside the model function?

How do i get data once in model and use the same data in multiple views passing through controller in laravel 8.x

For example, I want data from 3 models to MyModel4.
$model1data = MyModel1::getList();
$model2data = MyModel2::getList();
$model3data = MyModel3::getList();
Purpose of getting these data is to prepare my create view, edit view and show view based on above models data.
Now the issue I am facing is I can get the data using MyModel1::getList(); and pass it to my controller's create method. The same data I want for my controller's show and edit method. And for that again I have to get data using MyModel1::getList();. I want to optimize my code for less interaction with database/reduce redundant processing.
So is there any shorter method by which I can write it in my controller or in my model to get these data once and use it multiple times without getting data repeatedly from another model?
I have tried defining the separate method in the model but unable to access using a variable.
public static function getRequiredData()
{
$model1data = MyModel1::getList();
$model2data = MyModel2::getList();
$model3data = MyModel3::getList();
}
public static function prepareCreate()
{
return (object)array("model1" => PublishPlan::getRequiredData().$model1data, "viewModes" => ViewMode::getList(), "planSchemes" => PlanSchemes::getList());
}
I am getting an error while accessing $model1data in the above method(prepareCreate).
I think you should use a constructor for your issue.
public function __construct()
{
$model1data = MyModel1::getList();
$model2data = MyModel2::getList();
$model3data = MyModel3::getList();
}
with the constructor, you can access the lists in all methods of the same controller.
but if you want to access the same list in all controllers, you should define a static variable in Model1, Model2, and Model3.

Kendo datasource model - difference between _data[0] and get(0)

I would like to know the difference between
$("#uploadedFile").val(e.files[0].name);
var model = $("#blueprint_listview").data("kendoListView").dataSource.get(0);
model.set("filename", $("#uploadedFile").val());
And
$("#uploadedFile").val(e.files[0].name);
var model = $("#blueprint_listview").data("kendoListView").dataSource._data[0];
model.set("filename", $("#uploadedFile").val());
I am having an editable listview with a upload.
And the above code is written on the success event on the kendo upload.
The second code works fine for insert and update.
However, the first code works fine for insert, but for update it is showing an error which says - "The model is not defined"
I was wondering what could be the reason?
As stated in the documentation, get retrieves a record with the corresponding id. This way, when a new record is inserted it seems that it has the default id of 0, that's why get(0) === _data[0] but when you are updating the listview, a "real" id (>=1) is given to your new line and there is no longer an item with id=0, so model is then null.
On the other side, the internal method _data is an array with all the lines of your list view put in the order of their position in the listview. But if you want to access to this property, the equivalent "public" method is at :
$("#blueprint_listview").data("kendoListView").dataSource._data[0] ===
$("#blueprint_listview").data("kendoListView").dataSource.at(0); // allways true

Enum unbinds from the model on ajax call

I am reading an enum value from the db then bind it to the model. When i post the form with ajax, somehow the enum is unbound or the model property in null or zero but it display properly on the view. I have posted code below. Im using entityframework and mvc3
//model code constructor
public CarModel(Car car)
{
State=(CarState)car.State;
//car.State comes in as an int
//etc setting other variables
}
//CarState property
public CarState {get;set;}
//model code
#Html.DisplayFor(m=>m.CarState)
//Controller code()
Save(CarModel car)
{
//I have code that saves the changes
}
The minute i get to "car", CarState has no value.
It's not quite clear how you are passing this value to the controller action. You have only shown some #Html.DisplayFor(m=>m.CarState) but obviously this only displays a label in the view. It doesn't send anything back to the server. If you want to send some value back you will have to use an input field inside the form.
For example:
#Html.EditorFor(m => m.CarState)
or use a HiddenFor field if you don't want the user to edit it.
In any case you need to send that value to the server if you expect the model binder to be able to retrieve it. The model binder is not a magician. He cannot invent values. He binds values to your model from the Request.

Resources