Comments (9)
For example with a resource:
Post.php
<?php
use Illuminate\Database\Eloquent\Model;
use Cyrildewit\PageVisitsCounter\Traits\HasPageVisitsCounter;
class Post extends Model
{
use HasPageVisitsCounter;
}
PostController.php
<?php
class PostController
{
public function show(Post $post)
{
$post->addVisit();
return view(
'posts.show',
[
'post' => $post,
]
);
}
}
posts/show.blade.php
<p>This post has been viewed {{ $post->page_visits }} times.</p>
from eloquent-viewable.
Hi,
As a non native English speaker, it's a little bit hard to write an answer very fast but I see that @pxgamer has already answered your questions. Thanks for the help @pxgamer 😀.
Also! To get the formatted number, you should use $post->page_visits_formatted
.
this post has been visited {{ $post->page_visits_formatted}} times.
from eloquent-viewable.
It normally will take people a while to respond. 😄
Anyway, regarding your first question. What would your alternative be to a controller? You should be able to use this with route closures, but it's highly recommended to use controllers instead.
Regarding your second question, the example code you've mentioned is for managing the view count. The comments for each line are pretty self-explanatory.
If you wanted to display this post has been visited 1.000 times.
you could use the following in your blade template.
this post has been visited {{ $post->page_visits }} times.
from eloquent-viewable.
First of all I would like thank you so much @pxgamer & @cyrildewit , and as my last question:
I want to get this data in chart as google analytics does such as:
prduct 1 50 visits
product 30 30 visits
product 10 11 visits
//and so on
well I'm using ChartJs package and there to load this package database I have issue which is normally like
$chart = Charts::database(User::all(), 'bar', 'highcharts')
//....
but in this case my database name is page-visits
with -
in between, now how can I call that in order to get data out of it"?
from eloquent-viewable.
anyone can help?
from eloquent-viewable.
@pxgamer hi, would you mind help me with controller method? I couldn't find any sample code for that matter.
What should I do to store data of each posts visits?
from eloquent-viewable.
What does Charts::database()
do? Is it a package? Could provide a link to the documentation?
I think you could do someting like:
$productViewsChart = Charts::database(Product::all(), 'bar', 'highcharts')
Edit: Is this the correct package that you're using: https://github.com/ConsoleTVs/Charts
from eloquent-viewable.
@cyrildewit exactly that's the package,
I do not have any issue with charts package but my issue was with your package database naming, as you see Product::all()
but this package has -
in between pages-visits
and that's why I couldn't get info out of it.
anyway I fixed it by making model for it and adding
protected $table = 'page-visits';
now I need to get my products title as value for those counted visits but I can't.
here is my method to that:
$chartjson = Pagevisit::all()->toJson();
$chartjsontitle =
DB::table('products')
->join('page-visits', function ($join) {
$join->on('products.id', '=', 'page-visits.visitable_id');
})->get();
$value = collect($chartjsontitle)->toArray();
$chart4 = Charts::database($chartjson, 'bar', 'highcharts')
->elementLabel("Total")
->dimensions(1000, 500)
->responsive(false)
->values('visitable_id')
->labels($value);
PS: I know this question mostly related to chartjs package but the reason I'm asking it here is if you think i should put some relation in my created model for page-visits
in order to get that info out of it?
from eloquent-viewable.
If I'm right you want to have a bar chart that shows the total page views per product?
X-axis: Product name
Y-axis: Total page views
I would do something like this:
$products = Product::all(); // Get all the products (included with ->page_views)
$productViewsChart = Charts::database('bar', 'highcharts')
->elementLabel('Total')
->dimensions(1000, 500)
->responsive(false)
->values($products->pluck('page_views'))
->labels($product->pluck('name'));
To use the package provided Eloquent model for the page views table: Cyrildewit\PageVisitsCounter\Models\PageVisit;
.
from eloquent-viewable.
Related Issues (20)
- give point to post author on unique post view HOT 1
- how can I get user post view count HOT 1
- how to get total number of views of all posts HOT 1
- Getting Column not found when using ->orderByViews() HOT 10
- Laravel 9 support HOT 6
- `touch` the parent timestamp if the view has been recorded HOT 3
- Error : Cannot record a view for a viewable type HOT 1
- How to add some fields to table views HOT 1
- cannot record new views instance HOT 2
- Is there a way to record a view on cyrildewit / eloquent-viewable with the user_id not just the viewable_id HOT 1
- local.ERROR: Undefined index: lifetime_in_minutes
- Class "Period" not found HOT 2
- How can I check if an auth user or a visitor has already viewed a post? HOT 4
- Laravel 10 Support HOT 2
- PHP 8.2 support HOT 2
- CyrildeWit\EloquentViewable\Views::forViewable(): Argument #1 ($viewable) must be of type HOT 1
- How to extend the package to also count other things HOT 2
- User show the post twice HOT 1
- [Question] Seperate records by type
- Sort OutType Error:
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from eloquent-viewable.