Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guide needed #23

Closed
robertnicjoo opened this issue Dec 14, 2017 · 9 comments
Closed

Guide needed #23

robertnicjoo opened this issue Dec 14, 2017 · 9 comments

Comments

@robertnicjoo
Copy link

Hi,

  1. Do I have need any controller method in order to save data or no?

  2. these codes

// Return total visits of the article
$article->page_visits
$article->page_visits_formatted

// Return total visits of last 24 hours
$article->page_visits_24h

// Store a new visit into the database
$article->addVisit();

// Store a new visit into the database with an expiry date
$article->addVisitThatExpiresAt(Carbon::now()->addHours(3));

are just to show visits numbers or what exactly? (i mean frontend such as this post visited 1.000 times like that?)

Thanks.

@robertnicjoo
Copy link
Author

anyone can help?

@owenvoke
Copy link

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.

@robertnicjoo
Copy link
Author

@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?

@owenvoke
Copy link

owenvoke commented Dec 15, 2017

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>

@cyrildewit
Copy link
Owner

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.

@robertnicjoo
Copy link
Author

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"?

@cyrildewit
Copy link
Owner

cyrildewit commented Dec 17, 2017

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

@robertnicjoo
Copy link
Author

@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?

@cyrildewit
Copy link
Owner

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;.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants