Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ligonsker's avatar

How can I make my server fetched partials more reusable?

Hello,

I started using server fetched partials so I am returning the same view, with the same data passed to that view from several controller methods.

For example:

// In the controller
public function create(Request $request)
{
    // do some create stuff   

    // then get the partial data and return it
    $data = DB::table('table')->....->get();
	return view('_some_partial', ['data' => '$data']);
}

public function edit(Request $request)
{
    // do some edit stuff   

    // then get the partial data and return it
    $data = DB::table('table')->....->get();
	return view('_some_partial', ['data' => '$data']);
}

public function delete(Request $request)
{
    // do some delete stuff   

    // then get the partial data and return it
    $data = DB::table('table')->....->get();
	return view('_some_partial', ['data' => '$data']);
}

I could do something like that - create a function inside the same controller that calls only the part related to the partial and call it from within the relevant controller methods(didn't try yet):

public function return_some_partial()
{
    $data = DB::table('table')->....->get();
	return view('_some_partial', ['data' => '$data']);
}

and call it from the other methods:

public function create(Request $request)
{
    // do some create stuff   
    
    $this->return_some_partial();
}

Is it OK? Will it work? Or there is a better way to do it? It just feels that such function does not "belong" to a Controller

Thanks

0 likes
15 replies
jlrdw's avatar

Look at this post where I gave an example of using one for a lookup table. For me it's best when a server fetched partial sits in an object (similar to iframe). This way you have everything like a search, pagination, etc.

https://laracasts.com/discuss/channels/laravel/can-i-redirect-a-get-request-to-a-post-request-for-inertia-partial-reloads

The trick is learning how to pass data from child to parent (if needed), which isn't hard.

Edit: In the example I happened to use a modal also.

2 likes
Ligonsker's avatar

@jlrdw can you explain more please? It looks like this is what I'm doing I also have pagination and search ( I place the necessary data - table, pagination and anything else I need and do innerHtml to a target container div )

But even with that, in my case I have it in 3 different places in the controller and if I need for example to change the query that fetches the data, I'll need to change it in 3 different places

jlrdw's avatar

@Ligonsker one place should work, look at the last example here from @snapey :

https://laracasts.com/discuss/channels/laravel/laravel-query-builder-5

Basically start your query and chain on as needed. I think his answer there will point you in the correct direction. Note when chaining some if statements may be needed depending on condition.

I will try to find an example I have in one of my projects and put it here as well.

Edit:

        $petsearch = $petsearch . "%";         // passed parameter
        $query = Pet::query();
        $query->where('petname', 'like', $petsearch);
        if (Auth::userRole('admin') === false) {
            $userid = Auth::user()->id;
            $query->where('owner_id', '=', $userid);
        }
        $results = $query->orderBy('petname', 'asc')->paginate(5);
        return $results;

The part in the if is not used if it's an admin.

  • user only sees their data
  • admin can see all.

So just write strategic queries and or use query scopes as needed.

I know I can also use Auth::user()->id direct, that code is older code.

2 likes
Ligonsker's avatar

@jlrdw Thanks, So the difference in your code from my example, is that your function only returns the data, not the view. So I'll need to have a function that returns the necessary data, instead of my example which both gets the data and returns the view, something like that:

// instead of "return_some_partial()"
public function get_some_partial_data() 
{
    // some more conditions here
    // maybe if..else or Auth-related conditions
    $data = DB::table('table')->....->get();
	return $data;
}

// use it:
public function create(Request $request)
{
    // do some create stuff   
    
    $data = $this->get_some_partial_data();
    return view('_some_partial', ['data' => '$data']);
}

But, is it OK to have a "normal" function in a controller? I just know that the point of Laravel's organization is to give every component (controller/view etc..) a specific role.

Here I just create a function that is a normal PHP function and not some route-related controller function

Will it make sense to create a app/Partials/ folder and have a php file for maybe each controller so that any partial-related function I'm using will be there? Or that's OK to keep such functions in the controller?

jlrdw's avatar

@Ligonsker my example returns another views data inside an object (the view). I happened to use a modal as well.

Just returning a table only, you loose it's pagination. So a well written partial will have all that's needed as though it was an entire view. At least that's the way I use them.

You could just show a small table if that's all that's needed, each case is different.

I mainly use for long lookup tables with search/ I.e., you need to lookup a customer.

Don't confuse a view partial with a server fetched partial, they are two different things.

1 like
Ligonsker's avatar

@jlrdw I do something similar regarding pagination, I get the page number from the query parameters (or post parameters):

$page_number = $request->get('some_query_page_param') === "null" ? 1 : $request->get('some_query_page_param');

$data = DB::table('table')->....some query....->paginate($perPage = 100, $columns = ['*'], $pageName = 'the_page_name', $pageNumber = $page_number)->withQueryString();

(The variables insde the paginate() are explicit on purpose for this example)

Is that what you mean by "object"? At first I got confused when you said object because I didn't understand what you are referring to

Ligonsker's avatar

@jlrdw I never used an iframe, but isn't it basically an html element that acts like a wrapper for my content? Also I just noticed this part in your previous message:

Don't confuse a view partial with a server fetched partial, they are two different things.

But looking at my code, and comparing to the videos from here: https://laracasts.com/series/javascript-techniques-for-server-side-developers/episodes/1

Isn't my code a server-fetched partial? Because I also get some data from the backend?

jlrdw's avatar

@Ligonsker in the video look at minute 6:19 he is using fetch js (which I also use) to fetch that data into another page.

I just like embedding it in an object. And yes I normally embed the whole view, but not in all cases.

Basically the same technique except I add the object to what he is doing.

1 like
Ligonsker's avatar

@jlrdw ohhh!! Sorry I didn't mention, but that's also what I do! Every one of the methods above in my controller, are called using fetch from my frontend with JS as well! Then I use the fetch call to get that partial and place it with innerHTML into a container div.

jlrdw's avatar

@Ligonsker You are on the right track, just figure out your queries so some of them are reusable.

1 like
Ligonsker's avatar

@jlrdw thanks! And where should I place the functions that return the data? i.e. this snippet:

public function get_some_partial_data() 
{
    // some more conditions here
    // maybe if..else or Auth-related conditions
    $data = DB::table('table')->....->get();
	return $data;
}

Because it feels like it doesn't belong inside a Controller, even though it works (using $this->get_some_partial_data();)

jlrdw's avatar

@Ligonsker the code is no different than code returning a regular view. However if only returning data then you return json, whereas a view in an object is different.

1 like
Ligonsker's avatar

@jlrdw thank you, I just mean in general:

Since I have this code that I need multiple times (either the data part, or the entire view part):

    $data = DB::table('table')->....->get();
	return $data;

and I want to reuse it in the controller. Where is a good place to put it there? Would you just place these functions inside the controller and use them with $data = $this->get_some_partial_data();, or place these "utility" sort of functions in a dedicated folder? (because they are not really a Controller method, just a piece of code that I want to reuse so that I don't have to copy it in multiple places in the controller)

jlrdw's avatar

@Ligonsker generally if some code is short, I may just put in in the controller, however I try to put longer code that returns data in the model with a return statement which then the controller loads the view. In other words I try to stay with the basic MVC.

But you should also look at using query scopes.

But really there is no right answer, preference has a lot to do with it.

Please or to participate in this conversation.