defining routes with parameters in laravel - 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

Related

Keep filter data in laravel

I have a page with table of some data (let's say posts). I have a form there which leads to the same page but with get parameters. So when I choose December in dropdown list form will lead to posts/index?month=december. Controller index method makes eloquent query using query string parameters to filter rows. After that I click edit post(or add new post) and change something through edit form. Then update or store method redirects back to index as usually. What is the best way to come back to index?month=december page? Sessions? How should I do it? Set some values from query to session in Controller index method when I come there first time and get them also in Controller method from session after editing/creating?
Try to append all request parameters in the redirect:
return redirect()->route('route_name',['month' => request()->get('month')]);
You can read more about this in this section in the docs.

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

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();

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 1.7 filter catalog products in view

I want to filter a category product collection in frontend, and replace the products with the result collection i.e. the user is filtering by "blue" i want to filter the collection like addAttributeToFilter.
I want to do this with an ajax call, but i don't get what url i should use and where to perform this filtering.
Is the catalog list index controller accessable from url? Then i could work with get parameters here...
Or has somebody another solution how to manage this? I didn't really get how i.e. the sorter works and manipulates my collection, so i'm not able to adapt this behaviour in the moment.
I finally solved my problem with an ajax request out of list.phtml to the current URL adding parameters as data.
In Mage/Catalog/Block/Product/List.php i modified the function _getProductCollection()
and added:
if($_POST['name']){
$this->_productCollection->addAttributeToFilter('name', array('like' => '%'.$_POST['name'].'%'));
}
After the ajax request the products list table gets replaced with the filtered content.

Validating unique field with HABTM join table

I'm struggling to work out how to validate a field in Model A that should be unique when combined with a field from Model B.
Here's an example to clarify my question:
Page hasMany SitesPage
SitesPage belongsTo Page, belongsTo Site
Page has a slug field which should be unique across a site. Pages can be attached to any site.
Page.id
Page.slug
SitesPage.id
SitesPage.site_id
SitesPage.page_id
I have a checkUniqueSlug() custom validation method in my Page model but can't validate the slug is unique to the site as the site_id is stored in SitesPage which isn't available in the Page model validate method ($this->data only contains Page model data).
I can't do the validation in the SitesPage model as SitesPage doesn't have a slug field and I can't see the Page post is SitesPage.
How do I create a custom validation to check the slug is unique to the site?
One solution is to move the slug into the SitesPage model but we need all shared pages to have the same slug. i.e. A shared "About Us" page must have an "about_us" slug irrespective of which site the page is attached to.
Another solution is to perform the validation in the controller before I save which would work but that feels wrong as the validation should be done in the model.
As there have been no answers and someone else might be looking for help, here's how I ended up with a solution:
As the controller is the only place where both model data is present in the data array, the call to validate has to be done there.
I offloaded the validation code to the model and called it with the following:
if (!empty($this->request->data)) {
if ($this->Model->specialMultiModelValidate($this->request->data) && $this->Model->save($this->request->data)) {
// model has validated and saved
}
else {
// model has failed to validate and save
  }
}
Hope someone finds this useful.

Resources