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

forteirp's avatar

How to get a excerpt from the input data from the body?

What I want to do is to use the first 100 words that have been inputted from my body input as the excerpt input on my database... I think I need to modify my store function to do this? But I'm not sure how to do that correctly. I looked up the "only" and "except" methods, but I am not sure how it works if I only want the first 100 words of the body input?

This is my store function

public function store(Request $request)
    {
        $this->validate($request, array(
            'title'         => 'required|max:255',
            'slug'          => 'required|alpha_dash|min:5|max:255|unique:posts,slug',
            'category_id'   => 'required|integer',
            'body'          => 'required'
            
        ));
        //create Post
        $post = new Post;
        
        $post->title = $request->title;
        $post->slug = $request->slug;
        $post->category_id = $request->category_id;
        $post->body = $request->body;
        $post->author_id = auth()->user()->id;
        
        $post->save();
        
        return redirect('/posts')->with('success', 'Your post created created successfully');
    }

and here's my phpMyadmin to excerpt column... https://i.stack.imgur.com/segmG.jpg

Let me know if you need more information from me, to help was this issue...

0 likes
6 replies
Nakov's avatar

I don't see in your code where you are setting the excerpt column, so there are couple of ways to achieve this, you can add it here like this:

$post->excerpt = str_limit($request->body, 100);

Make sure that in your Post model you've got the excerpt in the $fillable array.

The second approach is to use setter in your model:

public function setExcerptAttribute($value)
{
    $this->attributes['excerpt'] = str_limit($value, 100);
}

But then again in your controller you will have to add this:

$post->excerpt = $request->body;
forteirp's avatar

That worked, but now the new entry into the data table is being put on top of the old data, but it used to add the new entries at the bottom of the table. Can I fix that? ...

Seen here: https://i.stack.imgur.com/vciG0.jpg

Also... Let me know if I have to begin a new discussion for this one? And My body data is including the HTML when it's input to my phpMyAdmin...

Here's what I'm talking about ... https://i.stack.imgur.com/4uw9b.jpg

My body form in my create.php has "{!!" at the being and "!!}", I was under the impression that the "{!!" and "!!}" care of that for me.

create.blade.php

<div class='form-group'>
        {{ Form::label('body', 'Body')}}
        {!! Form::textarea('body', '', ['id' => 'article-ckeditor', 'class' => 'form-control space', 'placeholder' => 'Body Text', 'required' =>'',])!!}
                                </div>

and here's my store function

public function store(Request $request)
    {
        $this->validate($request, array(
            'title'         => 'required|max:255',
            'slug'          => 'required|alpha_dash|min:5|max:255|unique:posts,slug',
            'category_id'   => 'required|integer',
            'body'          => 'required'
            
        ));
        //create Post
        $post = new Post;
        
        $post->title = $request->title;
        $post->slug = $request->slug;
        $post->category_id = $request->category_id;
        $post->excerpt = str_limit($request->body, 100);
        $post->body = $request->body;
        $post->author_id = auth()->user()->id;
        
        $post->save();
        
        return redirect('/posts')->with('success', 'Your post created created successfully');
Nakov's avatar

@FORTEIRP - If you are talking about the {!! !!} around the text area, no it won't escape the html tags, you can escape html tags when you are printing the content, so {!! "<p>test</p>" !!} the paragraph will be escaped.

$post = new Post;

You are creating new post each time this way, so your post with id 1 should not be deleted.

I hope I helped with this reply now, but after this you can accept the answer that helped you and create a new discussion in order to get a response to your problem quicker.

forteirp's avatar

I know I'm asking a few different questions in this discussion... and will stop doing that.

But right now, I wanted to know if you knew "how to fix the way that I'm receiving data now?"...

I think I was the one that deleted the id 01, so I don't think the new posts are replacing the old ones. But what I've noticed is now my new entry are being put on top of the old data.

If you look here: https://i.stack.imgur.com/wmlGt.jpg

You can see that id 14 and 15 have been added on the top of the table and that started after I added your suggestion... $post->excerpt = str_limit($request->body, 100);

I think I might have to refresh my seed sample data and start fresh and try your suggestion?

Nakov's avatar

@FORTEIRP - Start fresh it won't hurt. I don't know why it shows on top, but at least all the others are here.

Please or to participate in this conversation.