P
P
photosho2016-01-24 19:48:46
Laravel
photosho, 2016-01-24 19:48:46

How to count part of comment text in Eloquent?

Hello. In general, the question is described in the title, here I will just explain it a little. There is an "Article" model associated with a "Comment" model (one-to-many):

public function comments() {
    return $this->hasMany('App\Comment')->orderBy('id', 'DESC');
}

But comments can be very long, despite the fact that there are quite a lot of them on the page. The task is the following: to read only a part of the comment with further display of the "Entire comment" button, which loads its full version via Ajax. How can this be done?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene, 2016-01-24
@Nc_Soft

public function comments() {
    return $this->hasMany('App\Comment')->selectRaw('id, name, SUBSTRING(comment, 30) as comment_limit')->orderBy('id', 'DESC');
}

S
Stanislav Pochepko, 2016-01-24
@DJZT

Why upload only a part? Make a Js handler, by clicking on the show link, another div will open, or the entire comment from a variable in Js or stored in a hidden div will be added there. And if you want Ajax, Eloquent has nothing to do with it. Just put substr() on the output

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question