vendredi 2 mars 2018

How to only get the first 10 characters from a database field when querying with Eloquent Relationships?

The situation: In the User's Profile, I am fetching all of the user's posts.

A post consists of a column called "body" which is like the text in a blog article.

I only want to get the first 10 characters of the "body". Currently the code below gets the entire body which will be wasteful because I only need the first 10 characters.

public function show(User $user) 
{
    // This fetches all of the user's posts. Also it gets the entire body, which I don't want. I only want 10 characters from the "body".
    $posts = $user->posts;

    return view('users.user', compact('user', 'posts'));
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire