danimohamadnejad's avatar

best way to supply views and rendrable portions with data

Helllo I am creating an ecommerce application using laravel and I want to be able to add filter options to my products. please consider following url:

locahost/filter-options?v=fo&with_search=1&with_insert=1

navigating browser to above url may display different views depending upon value of "v" in query string. but I also can specify which optional rendrable portions to be added to view using "with_*" query string param. these portions may also need to be supplied with different data. the problem is that I dont want to send all possible data from controller to views but I want to send only required data from controller since there may be many rendrable portions having their own data.maybe pulling data from controller instead of pushing??? what is best practice or a design pattern?? thank you in advance.

0 likes
9 replies
jlrdw's avatar

I would highly suggest watching some of Jefferies free video from scratch series, and learn some basic crud.

That would answer the majority of your questions.

Snapey's avatar

Nothing particularly hard here.

Just build it up and test at each stage.

One technique I use is the use of view()->share()to send things to the view rather than building it all up at the end of the controller method.

eg

public function filters(Request $request)
{

    if(request('with_search',0)==1){
        $this->addSearch($request);
    }


    if(request('with_insert',0)==1){
        $this->addInsert($request);
    }

    // etc


    return view('name_of_view');

}

private function addSearch(Request $request)
{

    // do stuff here to prepare data required for search

    view()->share('search',true);

    view()->share('searchData',$searchData);

    return;

}


private function addInsert(Request $request)
{

    // etc similar to search.  Push data to view

}

danimohamadnejad's avatar

thank you for reply. yes it seems good but I still have to fill my controller method with many if elseif statements which will be boring and annoying. what is your opinion about pulling data? isnt there any way?

jlrdw's avatar

Then figure out a way to not use if statements. I love if statements.

danimohamadnejad's avatar

hehe thank you. well every one loves if statements but this is not such good situation for using if statements since number of variable may increase. maybe I can use a pattern by which to merge essential data along with strategy pattern. I need to work more on it since this situation often happens in controllers

jlrdw's avatar

Work out multiple search scenarios and use some good query scopes, at least I've never had no trouble doing it that way. I am on mobile now, I will try to get you a screenshot of an example. Will have to boot up the desktop.

But what you are after does require more if statements. Think about it, if it's the most powerful thing in programming. You may as well come to love it. Without that Mighty little statement there would be no such thing as programming.

danimohamadnejad's avatar

I have already written a server side data filter component that filters data based on criiteria requesting by user but this is such a situation in which I want to be able to maybe to couple a particular request param with one or many rendrable portions. looks like a one-one or one-many state. I difintly use small classes extending from one base class and foreach request from user I will chain these objects and get data from them and store in $view_data array and send it to view.

jlrdw's avatar

You may even want to consider using ajax and javascript to achieve what you are after.

Me I would just change the url as required depending on current search, as example:

Notice if I click Search By Description, I can then search by beginning of field or whole field.

But if search by date, then I do not try to use the same parameters, I use a different set of parameters. I just try to keep things simple.

Same for amount, different parameters. Meaning different query string parameters.

Just noticed, I need to tweak my search div.

Remember, sometimes by the time you save a step, you create more steps to achieve that.

Please or to participate in this conversation.