Cakephp 3.6. How to call a helpers query from a controller? - ajax

I have on my homepage a small search form with a list of categories.
I use a helper to query the database to get all categories. This is fine and works great.
Now I want to update the categories select with an updated list of categories based on a performed search.
That means, I would need to call query inside the helper with a given $variable to run a new query and to return new data.
But this is my concern now.
I can't call a helper from within the PagesController, right?
So what is the right way to
- have a helper call to use on the fly in all views selecting DB Data?
- address an ajax call to a controller and to use the query from the helper?
Any advice will be highly appreciated!
Thanks!

Short answer:
You code needs to be moved in a Table class and not in a Helper class, if you need it both in the Controller and the View.
Long Answer:
The code you have created in your helper needs to be moved in the relevant Table class and that will fetch the data.
Then in both your view and the controller you will use the new method you created in your Table class.
For example in your CategoriesTable class you can add a method called:
public function fetchCategoryData(){
//TODO: Custom code that will fetch the category data like...
//return $this->find();
}
public function updateCategoyData($args){
//TODO: Custom code to update your category data
}
Now, in either the View or the Controller you can use this
$Categories = TableRegistry::get('Categories');
$Categories->fetchCategoryData();
//or
$Categories->updateCategoryData();

Related

defining routes with parameters in laravel

in Laravel i want to do a page with a search box and a form (the route could be /products)
I want to retrieve information using search box typing id from a db and populate the form.
I request datas with the route for example /products/{id}
But in the controller i use the same function products($Request request) with if the id exists do something if no do other things, or there are two different functions?
Thx
Please go through the Laravel Resource Controllers.
To show list of products i.e. /products, create a index() method. To show a specific product i.e. /product/{id}, create show() method.
Probably is better use the same function, if id exist, return the page with the values filling the form, if not returns the same page but with a message showing that product doesn't exist

Where to process data in Laravel

Back when I was using CodeIgniter I had functions in my models like
public function GetArticlesFormatted($inactive = FALSE)
and then in my controller I could have had
$articles->GetArticlesFormatted(true);
And so on.
How should I achieve the same with Laravel 5.4? The database of the app I'm building is already built and full and is a mess so most of the "automated" super-restrictive things that Laravel has don't work out of the box.
For example there is a Country Code that I'm retrieving and I need it as is, but in some instances I need it converted in a Country Name which I had in another table.
Right now I just have in my controller wherever I am retrieving data from the model:
$countryResult = Country::where('country_code', $item['country_code'])->first();
$countryArray = $countryResult->toArray();
$item['country'] = $countryArray['country_name'];
Can I somehow achieve that in a more elegant way?
I tried accessors but for some reason couldn't get anything to work for my purposes.
A select query can be used to limit selection to a particular column.
$countryName = Country::select('country_name')
->where('country_code', $item['country_code'])
->first();

is it possible to use single view for multiple methods and controllers with different data?

I need to use single view multiple time with different methods as well controllers all methods are sending different data and on submit it effect to different table of database.
suppose i have two methods, method1 and method2
method1 has data like {123,456,789} and go to view with this data and on submit, need to make changes in table1 of my database.
method2 has data like{321,654,987} and go to view with this data and on submit, need to make changes in table2 of my database.
i don't have any idea about to reuse the view with different data, if it is possible then please help me.
Surely you can use same view for different Laravel methods which will not make any difference what so ever.
Eg.
In UserController#login method you can return view('login');
and the same view can be returned in CustomerController#login method also.
you just need to change the Form Action in login.blade.php based on the Controller who is returning the view.
public function func1(data){
return view("document",[data]);
}
public function func2(data){
return view("document",[data]);
}

Laravel multiple models in controller

This question regards the structure of applications when using laravel.
I have a view for making a sale/purchase from a company. This single view contains a client search, product list, service list and a staff list. Each of the items listed above has their own model. In the view, i would say search for a client which will call a function within the controller and populate a list. Same for the products and services etc etc.
What confuses me is that for the client search i would click a button that would fire to a url like /clients/search/search string, which would return the array of clients to display on the page. This function seems as if it would be appropriate within the client controller. I am unsure as to how i would be able to maintain the information from the client search and other parts of the sale to then submit it all together under one single controller (let's call it InvoiceController).
Can controllers share the functions from other controller? Do i simply store the information in a session variable? Do i simply put all relative functions to this sale under the InvoiceController?
Thanks for any help!
I would do a search for clients against the client controller, then when you select the client add this (client_id) to your invoice form. then save this with other invoice info via the invoice controller
After much deliberation, it appeared that AJAX was the simplest answer. In this case i used Angular JS and had that running a controller to fetch each segment of the Invoice, then use the main InvoiceController in Laravel to fire the final update.

Magento: Where would I place that functionality?

I have a Magento form with a button and some fields. The button click calls a controller and in the controller I query the DB (using fetchAll) and create a csv file from the results.
Whats the best place to store the DB action and the CSV file creation? In a model maybe?
Thanks!
I would create a model, a resource model and a resource model collection for the data you need to export. In the collection I would define functionality that queries data from one or many (if necessary) tables . In the model itself I would create a function that fetches that collection and saves it into a CSV file. The controller action would contain only the model initialization and a call to the export function.

Resources