Azoruk's avatar

Loading more eloquent hasMany relation queries through ajax in Laravel?

On my site, users can post "statuses", and other users can reply/comment on those statuses. On the front page of my site, for example, I have the most liked statuses of the past 3 days displayed.

In my HomeController.php:

$statuses = Status::where('is_image', 1)
    ->where('is_mature', 0)
    ->where('created_at', '>=', Carbon::now()->subHours(168))
    ->orderBy('like_count', 'desc')
    ->take(6)
    ->get();

Statuses can also have replies. Replies use the same table as the status table. Replies are pretty much just statuses with a "parent" status, and regular statuses are just statuses without a "parent".

From the model status.php:

public function scopeNotReply($query) {
    return $query->whereNull('parent_id');
}

public function replies() {
    return $this->hasMany('CommendMe\Models\Status', 'parent_id');
}

So back to my home page, here's how the statuses are loaded on the home page:

home.blade.php:

@if ($statuses->count())
    @foreach ($statuses as $status)
      <div class="post-container">
       
        ...
        
        blah blah blah
        
        ...
       
        @foreach ($status->replies->reverse()->slice(0, 3) as $reply)
            <div class="status-reply-content">
                ...
                
                blah blah blah
                
                ...
            </div>
        @endforeach
                
        @if (count($status->replies) > 3)
            <a href="#">
                    View More Comments ({{ count($status->replies) }})
            </a>
        @endif
        
      </div>
    @endforeach
@endif

What I want is for the "View More Comments" button to load another set of 3 comments.

This would be simple if these were being paginated. In fact, elsewhere on my site I load more images through ajax. Like so:

if($request->ajax()) {
    return [
        'images' => view('browse.partials.fullview_partial')->with('images', $images->appends(request()->only('query')))->render(),
        'next_page' => $images->nextPageUrl()
    ];
}       

however this relies on loading in the next set of pagination through ajax. Whereas the $status->replies relationship is not being paginated.

So how could I load more replies in this scenario?

0 likes
6 replies
Azoruk's avatar

An alternate solution would be accepted as well

jlrdw's avatar

You could load 3 more by using the skip and take technique, just have to manually keep track of where you are. Example form docs

$users = DB::table('users')->skip(10)->take(5)->get();

The orm has all querybuilder methods also.

In above the 10 and the 5 of course would be passed in as variables you calculate.

Azoruk's avatar

How could I load them through ajax though?

jlrdw's avatar

You'd have to work that out, I was just trying to give an idea. Look at the docs at the skip and take.

jlrdw's avatar

Just a thought, build 3 more rows in jquery ajax, see some of the examples on the web on inplace table editing. You don't need whole table, just 3 more entries. The table / data is "built".

Please or to participate in this conversation.