How to add view count for post in laravel - php

I am performing a Blog application using laravel.. I want to track the count of the post view whenever a user view a particular blog post it should increment only once whether it is a registered user or non-registered users. ... And also want to display the most viewed blogs according to the view count.. Can anyone assist with the logic.
/ Table
Schema::create('posts', function(Blueprint $table)
{
$table->increments('id');
$table->text('body')
$table->integer('count');
...
$table->timestamps();
});
public function show($id)
{
$post = Post::find($id);
Post::update([
'count' => $post->count + 1
]);
return View::make('posts', compact('post'));
}

public function show($id)
{
$post = Post::find($id);
$post->update([
'count' => $post->count + 1
]);
return View::make('posts', compact('post'));
}

You have two requirements:
To increase view count for every view.
public function show($id)
{
$post = Post::find($id);
Post::update([
'count' => $post->count + 1
]);
return View::make('posts', compact('post'));
}
To display the most viewed blogs according to the view count
$post = Post::orderBy('count', 'DESC')->first();
Additionaly, To display the most viewed blogs list according to the view count,
$post = Post::orderBy('count', 'DESC')->get();

Related

Laravel incrementing the db value by 2 times

This is function that is returning the view
// show individual post
public function showPost($id){
$targetPost = Post::findorFail($id);
$targetPost->increment('post_view_count');
$post = [
'post' => $targetPost,
'related_posts' => Post::all()->sortByDesc("id")->take(2)// load some related posts too
];
return view('post_single', $post);
}
What I am wanting here is to update the value by 1 when someone visits the page, the posts are in a list view to some other page, user are supposed to click that link.
Everything is working, but instead of incrementing the value by 1, it is incrementing by 2 even if I hit the reload button.
What I can assume is that the page is requested twice or loading via some kinda middleware first. That's why the request is being sent twice. How do I fix this?
Such a basic counter will not suit your needs. What you can do is create a migration for your Post Views:
Schema::create("post_views", function(Blueprint $table)
{
$table->unsignedInteger("id");
$table->unsignedInteger("id_post");
$table->string("session_id");
$table->string("user_id");
$table->string("ip");
$table->string("agent");
$table->timestamps();
});
Next create a model that will handle the views for you
class PostsViews extends \Eloquent {
protected $table = 'posts_views';
public static function createViewLog($post) {
$postsViews= new PostsViews();
$postsViews->id_post = $post->id;
$postsViews->titleslug = $post->titleslug;
$postsViews->url = \Request::url();
$postsViews->session_id = \Request::getSession()->getId();
$postsViews->user_id = \Auth::user()->id;
$postsViews->ip = \Request::getClientIp();
$postsViews->agent = \Request::header('User-Agent');
$postsViews->save();
}
Now use this in your showPost function():
public function showPost($id){
$targetPost = Post::findorFail($id);
PostsViews::createViewLog($targetPost);
$post = [
'post' => $targetPost,
'related_posts' => Post::all()->sortByDesc("id")->take(2)
];
return view('post_single', $post);
}
Now it will log all views but you can filter it out when you need it on User agent, IP address or session. You can also use that kind of filter when logging a view to just log 1 view per post per viewer.

Querying MorphToMany Relation Laravel

The thing I want to get from the database is that get all the posts with the data which will identify whether the post is liked by the auth()->user() or not. Most probably via count.
App\Post
public function likes()
{
return $this->morphToMany('App\User', 'likeable');
}
App\User
public function likePosts()
{
return $this->morphedByMany('App\Post', 'likeable')->withTimestamps();
}
Likeables Table
Likeables table has ('user_id', 'likeable_id', 'likeable_type')
I tried using orWhereHas
$posts = Post::with( ['user', 'tags', 'category'])->orwhereHas('likes', function($q) {
$q->where('user_id', auth()->id());
})->latest()->withoutTrashed()->paginate(10);
But with about query I am only getting those posts which the user has liked. I want to get all posts and a check whether the post is liked by the user or not
I came across whereHasMorph but it was only for morphTo and not for morphToMany.
#m____ilk I was able to solve this but creating a mutator:
public function isLiked()
{
return $this->likes()->where('user_id', auth()->id())->count() > 0;
}
I ran a loop on the posts and attached a custom attribute to a single post based on the mutator.
$posts = Post::with( ['user', 'tags', 'category', 'post.user', 'post.tags', 'post.category'])->latest()->withoutTrashed()->paginate(10);
foreach ($posts as $post) {
// Mutator Condition
if ($post->is_liked) {
// Custom Attribute
$post->isLiked = 1;
} else {
$post->isLiked = 0;
}
}
return $posts;
In laravel 9 I did something like:
$posts = Post::with( ['user', 'tags', 'category', 'post.user', 'post.tags', 'post.category'])
->withCount([
'likes',
'likes as is_liked' => function($q) {
$q->where('user_id', auth()->id());
}
])->latest()->withoutTrashed()->paginate(10)

function static::created does not save properly Laravel

So I have 2 tables: Item and Product. An Item hasMany Products and a Product belongsTo an Item.
Products migration:
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('hashed_id')->nullable()->unique();
$table->bigInteger('item_id')->unsigned();
$table->bigInteger('user_id')->unsigned();
$table->integer('state')->default(1);
$table->decimal('price');
$table->string('slug')->nullable()->unique();
$table->timestamps();
$table->foreign('item_id')->references('id')->on('items')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
For the hashed_id I use the following package: https://packagist.org/packages/hashids/hashids to create a hashed id to show in the url.
Product.php
public static function boot()
{
parent::boot();
static::created(function ($product) {
$productId = $product->id;
$hashids = new Hashids("", 10, 'abcdefghijklmnopqrstuvwxyz1234567890');
$hashedId = $hashids->encode($productId++);
$slug = Str::slug($product->item->slug . '-' . $hashedId);
$product->hashed_id = $hashedId;
$product->slug = $slug;
});
}
ProductsController.php
public function createSelfProduct(Request $request)
{
$product = auth()->user()->products()->create([
'item_id' => $request->item_id,
'user_id' => auth()->user()->id,
'price' => $request->price,
]);
// create the product and show seller info
return new ProductResource($product->load('user'));
}
What I'm trying to do is that when a user creates a new product, it should get the slug from the item model, put the $hashedId behind it and save that to the db. Now, when I do a post request via Postman, I get the desired result, as hashed_id and slug are saved. But when I check the database, both hashed_id and slug are NULL. Only the item_id, user_id and price are saved. What am I doing wrong and how can I fix this?
The created event means the Model has already been created. This is not before save, but after it has been saved. If you alter anything at this point you will need to save the record again.
Simply: You forgot to call save on your model instance to save the changes after you altered it.
Laravel has a convenient way of handling this with Observers
https://laravel.com/docs/5.8/eloquent#observers
php artisan make:observer ProductObserver
Then in Observers/ProductObserver.php
public function created(Product $product) {
$product = ''; // whatver you need to do here. $product is an instance of Product model
// Dont forget to save your model after modifying
$product->save();
}

Field 'user_id' doesn't have a default value

I'm building a web app with laravel.
First Question:
There's a simple form on users dashboard to fill and save.
Here's the model:
class Salon extends Model
{
protected $table = 'salons';
protected $fillable = [
'salonname', 'saloncity', 'salonaddress', 'salontel', 'salonmob', 'salonsite', 'saloncat', 'salonkhadamat', 'salonkhadamatprice', 'salondesc', 'saloninsta', 'salontelegram', 'salontags'
];
public $timestamps = false;
}
and here is the controller :
public function store(Request $request)
{
$user_id = Auth::user()->id;
Salon::create([
'user_id' => $user_id,
'salonname' => $request['salonname'],
'saloncity' => $request['saloncity'],
'salonaddress' => $request['salonaddress'],
'salontel' => $request['salontel'],
'salonmob' => $request['salonmob'],
'salonsite' => $request['salonsite'],
'saloncat' => $request['saloncat'],
'salonkhadamat' => $request['salonkhadamat'],
'salonkhadamatprice' => $request['salonkhadamatprice'],
'salondesc' => $request['salondesc'],
'saloninsta' => $request['saloninsta'],
'salontelegram' => $request['salontelegram'],
'salontags' => $request['salontags']
]);
return 'done!';
}
And the routes:
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/salons/add', function () {
return view('add_salon');
})->middleware('auth');
Route::post('salons', 'SalonsController#store');
Route::get('salons', function () {
return 'Hi';
});
When I complete the form and hit send button, it returns this error :
"SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into salons (salonname,...
Where am I doing wrong?
I created a table migration as :
public function up()
{
Schema::create('Salons', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->string('salonname');
$table->string('saloncity');
$table->string('salonaddress');
$table->integer('salontel');
$table->integer('salonmob');
$table->string('salonsite');
$table->string('saloncat');
$table->string('salonkhadamat');
$table->integer('salonkhadamatprice');
$table->string('salondesc');
$table->string('saloninsta');
$table->string('salontelegram');
$table->string('salontags');
$table->timestamps();
});
}
user_id is using a foreign reference from users table.
let me explain the process, consider we have some users registered on our app, some of them want to add their salons on our website, so we want to use the user_id from the users table on salons table, so we can return salons with the user's data (profile) on our homepage.
Second question:
If a salon have two separate telephone numbers, How can I store them in this table separately? I mean, people can add many telephone-numbers as they want. Or as many addresses as they have, in separate fields.
Third question:
For creating a portfolio section for each salon, Should I create a new table such as attachments to have pictures addresses and salon id to return them on their respective page later?
Add user_id to the fillable array too:
protected $fillable = ['user_id', 'salonname', 'saloncity', 'salonaddress', 'salontel', 'salonmob', 'salonsite', 'saloncat', 'salonkhadamat', 'salonkhadamatprice', 'salondesc', 'saloninsta', 'salontelegram', 'salontags'];
Or use the relationship if it is defined:
$salon = auth()->user()->salons()->create([....
If it's not defined:
public function salons()
{
return $this->hasMany(Salon::class);
}

dd function not able to fetch content from database

I am building a post system in which user can post on his wall.Posts are created successfully and are saving into database but it can't be fetched from the database.
I've also relationship between user and posts.
my postcontroller:
public function getmyplace()
{
$posts=Post::where(function($query){
return $query->where('user_id',Auth::user()->id)
->orWhere('user_id',Auth::user()->friends()->lists('id')
);
})
->orderBy('created_at','desc')->get();
dd($posts);
$accounts=Account::orderBy('updated_at','desc')->limit(1)->get();
return view('myplace',['posts'=>$posts,'accounts'=>$accounts]);
}
public function postCreatePost(Request $request)
{
Auth::user()->posts()->create([
'body' => $request->input('body'),
]);
return redirect()->route('myplace');
}
my database:
output:
"Return" statement is not applyed in nested query builder and Collection::lists() method return array. Try this:
$posts = Post::where(function($query){
$query->where('user_id',Auth::user()->id)
->orWhereIn('user_id',Auth::user()->friends()->lists('id'));
})
->orderBy('created_at','desc')->get();
dd($posts);

Categories