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

shoaibali's avatar

How to add a SAVE POST/BLOG functionality with laravel 9

Hello Everyone. I'm hope you're all having a great day. I'm working on a project where a user can post something. I'm trying to add a functionality in which a user can also SAVE that particular post. That post will shown in user accounts with a separate menu of ALL SAVE POSTS.

Can anyone help me out with it ? How can i achieve this ? Would be very thankful if you can provide me with the work if you have done already.

i think we have to done it with something like to create a column in users table with name save_posts and adds a foreign key with a reference of posts table id.

Or maybe we have to apply a join . I'm much confused.

0 likes
10 replies
tykus's avatar

You might want to rephrase your question; because it is not clear what exactly you are struggling with.

I'm trying to add a functionality in which a user can also SAVE that particular post

Is this something different from a basic store/update operation that you would have in a basic CRUD application?

Would be very thankful if you can provide me with the work if you have done already.

What have you actually done; are you trying to outsource your (home)work?

shoaibali's avatar

I have implement a social platform in which an authenticated user can post something with everyone. I want to implement a functionality in which the other authenticated user can save that post he likes.

Means, If a user like some post/blog he can bookmarked/Save it. All those save/bookmarked posts will be shown in a separate section inside the user profile with a name All Save Posts.

Sinnbeck's avatar

@shoaibali so add a table with a user_id and a post_id column. Something like a FavoritePost model

tykus's avatar
tykus
Best Answer
Level 104

@shoaibali In that case, I would suggest you implement a many-to-many relationship where the foreign keys are user_id and post_id; whenever the User likes(/saves) a Post you attach across the relationship:

php artisan make:migration CreateSavedPostsTable --create=post_user

Implement then relationship

public function savedPosts()
{
    return $this->belongsToMany(Post::class);
}

Define an endpoint for saving the Post; and corresponding Controller:

Route::post('posts/{post}/save', [SavedPostController::class, 'store'])->name('saved-posts.store');

Save the Post

public function store(Post $post)
{
	auth()->user()->savedPosts()->attach($post->id);
    return back();
}

Then you can get the saved posts from the relationship using the User instance:

@foreach(auth()->user()->savedPosts as $post)
    // link and title, or something 
@endforeach

Otherwise, consider any up-to-date favoriting package, e.g. https://github.com/overtrue/laravel-favorite

3 likes
shoaibali's avatar

@tykus Hello, How can we achieve this with Query Builder approach? Would be thankful for the response. I've created a post with Query Builder approach. So, want to do it with Query Builder not the Eloquent one.

tykus's avatar

@shoaibali it should be no different to insert a record into the pivot table using the Query Builder compared with creating the Post, assuming you have the authenticated user's ID and the Post ID:

public function store(Post $post)
{
	DB::table('saved_posts')-> upsert([
        'user_id' => auth()->id(),
        'post_id' => $post->id,
    ], [
        'user_id',
        'post_id',
    ]);

    return back();
}
1 like
dmytroshved's avatar

@tykus What if user wants to unsave post?

Which option is more accurate for such scenario: delete() (delete record) or detach()?

Best regards

tykus's avatar

@Dmytro_Shved there is toggle method available on BelongsToMany relationships; I would suggest using that method to handle the save/unsave behaviours in single Controller action, e.g.

public function store(Post $post)
{
	auth()->user()->savedPosts()->toggle($post->id);
    return back();
}
1 like
dmytroshved's avatar

@tykus

I have a question about naming relationship in Post model

You added to User model:

public function savedPosts()
{
    return $this->belongsToMany(Post::class);
}

but how can I name the relationship in Post model? Is savedBy or savedByUsers would be nice?

I am asking because in my situation I have relationship with User in my Post model with name user() and If I will name other relationship using name users() it will be weird imho.

Would be grateful for your opinion about it

tykus's avatar

You don't have to strictly name the relationship according to the related model's name; for example, the Post might have followers or subscribers. Similarly, rather than user, it might have an author or creator

Please or to participate in this conversation.