Coder Social home page Coder Social logo

Guide needed about eloquent-viewable HOT 9 CLOSED

cyrildewit avatar cyrildewit commented on May 13, 2024
Guide needed

from eloquent-viewable.

Comments (9)

owenvoke avatar owenvoke commented on May 13, 2024 2

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.

cyrildewit avatar cyrildewit commented on May 13, 2024 2

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.

owenvoke avatar owenvoke commented on May 13, 2024 1

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.

robertnicjoo avatar robertnicjoo commented on May 13, 2024 1

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.

robertnicjoo avatar robertnicjoo commented on May 13, 2024

anyone can help?

from eloquent-viewable.

robertnicjoo avatar robertnicjoo commented on May 13, 2024

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

cyrildewit avatar cyrildewit commented on May 13, 2024

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.

robertnicjoo avatar robertnicjoo commented on May 13, 2024

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

cyrildewit avatar cyrildewit commented on May 13, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.