maximbureac's avatar

dynamically load views partials laravel

Hello i have an issue when it comes to display my creating product form. The main problem is that i have some fields different from one category to other and i want to display it just with what is needed for a specific category so what i thought is how i can load a partial based on select list. The workflow is: 1. I am selecting the product type 2. I am selecting category 3. the form partial is displayed. Yes i could have done this with jquery hiding and toggling classes with fadeIn and fadeOut but is there a way how this could be performed from the laravel part ?

0 likes
3 replies
igorblumberg's avatar
Level 7

You have a front-end issue and the best way to handle it without a front-end framework (like vue, react or angular) would be loading the entire page and hide/show with jQuery.

IF you would like to use laravel to handle it you could set up some routes so you can post to it with ajax and use laravel to return the view, like this:

public function echoMyView()
{
    return View("partials.categoryB")->render();
}

I don't recomend it, since you can end with a lot of http requests which can get slow.

1 like
rleecrewmp's avatar

You're trying to solve a front-end problem with back-end tools. Short of doing a complete reload of the page, Laravel (more importantly PHP) cannot help you manipulate the DOM after the page has been loaded. It can however, assist front-end tools, such as jQuery so that you can still use a view partial, but jQuery would need to fetch the contents of it. But as @igorblumberg said, it can create a lot of http requests.

3 likes
leoalmar's avatar

You can use:

/* Return view dynamically */
Route::get('view/{name_view}', function ($name_view) {
    return view($name_view);
});

2 likes

Please or to participate in this conversation.