cyrildewit / eloquent-viewable

Associate views with Eloquent models in Laravel
MIT License
803 stars 104 forks source link

cannot record new views instance #274

Closed fh32000 closed 2 years ago

fh32000 commented 2 years ago

Description:

Migrations i Used uuidMorphs

    public function up()
    {
        //drop old table
        Schema::dropIfExists('views');
        $this->schema->create($this->table, function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->uuidMorphs('viewable');
            $table->text('visitor')->nullable();
            $table->string('collection')->nullable();
            $table->timestamp('viewed_at')->useCurrent();
        });
    }

Model Article

class Article extends Model implements HasMedia, Viewable
{
    use Uuids;
    use HasFactory;
    use LogsActivity;
    use InteractsWithMedia;
    use DefaultOrderBy;
    use SoftDeletes;
    use Cachable;
    use InteractsWithViews;

    protected static $orderByColumn = 'created_at';

    protected static $orderByColumnDirection = 'desc';

    protected static $logFillable = true;

    protected static $logOnlyDirty = true;
    /**
     * Indicates if the IDs are auto-incrementing.
     *
     * @var bool
     */
    public $incrementing = false;

    protected $removeViewsOnDelete = true;
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'articles';

    /**
     * The database primary key value.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    protected $keyType = 'string';

    /**
     * Attributes that should be mass-assignable.
     *
     * @var array
     */
    protected $fillable = [
        'title',
        'body',
        'active',
        'can_comment',
        'user_id',
    ];

    protected $casts = [
        'active' => 'boolean',
        'can_comment' => 'boolean',
    ];

    protected $hidden = ['deleted_at'];

}

route

 Route::get('articles/{article}', [ArticleController::class, 'show'])->name('articles.show');

ArticleController

    public function show(Request $request, Article $article)
    {
        $is_record = views($article)->record();
        return ['is_record' => $is_record, 'views' => views($article)->count(), '$article' => $article];
     } 

Expected behavior:

Expected add new record to views table

Actual behavior:

no record added

More Debug

add log to helper

<?php

use CyrildeWit\EloquentViewable\Contracts\Viewable;
use CyrildeWit\EloquentViewable\Contracts\Views;
use Illuminate\Container\Container;

if (! function_exists('views')) {
    /**
     * Construct a new views instance.
     *
     * @param  \CyrildeWit\EloquentViewable\Contracts\Viewable|string
     * @return \CyrildeWit\EloquentViewable\Contracts\Views
     */
    function views($viewable): Views
    {
        \Illuminate\Support\Facades\Log::alert('line1');

        $builder = Container::getInstance()->make(Views::class);
        \Illuminate\Support\Facades\Log::alert($viewable);
        \Illuminate\Support\Facades\Log::alert('line2');

        if (is_string($viewable)) {
            $model = Container::getInstance()->make($viewable);
            \Illuminate\Support\Facades\Log::alert('line3');

            if ($model instanceof Viewable) {
                \Illuminate\Support\Facades\Log::alert('line4');

                $viewable = $model;
            }
        }
        \Illuminate\Support\Facades\Log::alert('line5');

        return $builder->forViewable($viewable);
    }
}

show log

[2022-07-03 14:16:10] local.ALERT: line1  
[2022-07-03 14:16:10] local.ALERT: {"id":"9b5dab30-e107-11ec-8c2d-ed9279b4b2f3","title":"\u0627\u0644\u0628\u0627\u0628 \u0627\u0644\u0645\u0641\u062a\u0648\u062d","body":"<p><span style=\"color: rgb(38, 38, 38);\">\u0633\u064a\u0627\u0633\u0629 \u0627\u0644\u0628\u0627\u0628 \u0627\u0644\u0645\u0641\u062a\u0648\u062d \u062a\u0639\u0646\u064a \u0627\u0646\u0643 \u0645\u0631\u062d\u0628 \u0628\u0643\u0644 \u0634\u062e\u0635 \u0641\u064a \u0627\u064a \u0648\u0642\u062a \u0648\u0627\u064a \u0645\u0643\u0627\u0646 \u0648\u0644\u0623\u064a \u0633\u0628\u0628 \u0645\u0647\u0645\u0627 \u0643\u0627\u0646\u062a \u0638\u0631\u0648\u0641\u0643 <\/span><\/p><p><br><\/p><p><span style=\"color: rgb(38, 38, 38);\">\u062a\u062e\u064a\u0644 \u0645\u062f\u064a\u0631 \u064a\u0624\u0645\u0646 \u0628\u0633\u064a\u0627\u0633\u0629 ( \u0627\u0644\u0628\u0627\u0628 \u0627\u0644\u0645\u0641\u062a\u0648\u062d ) \u0633\u064a\u0628\u0642\u0649 \u0637\u0648\u0627\u0644 \u064a\u0648\u0645\u0647 \u064a\u0633\u062a\u0642\u0628\u0644 \u0643\u0644 \u0627\u0644\u0645\u0648\u0638\u0641\u064a\u0646 \u0641\u064a \u0627\u064a \u0648\u0642\u062a \u0648\u0644\u0623\u064a \u0645\u0648\u0636\u0648\u0639\u060c \u0648\u0639\u0644\u064a\u0647 \u0644\u0646 \u064a\u0643\u0648\u0646 \u0645\u062f\u064a\u0631 \u0644\u0647\u0645 \u0628\u0644 \u0645\u0648\u0638\u0641 \u062f\u0627\u0639\u0645 \u0644\u0627\u0633\u062a\u0645\u0631\u0627\u0631 \u0645\u0634\u0627\u0643\u0644\u0647\u0645<\/span><\/p><p><br><\/p><p><span style=\"color: rgb(38, 38, 38);\">\u0641\u064a \u0639\u0644\u0627\u0642\u0627\u062a\u0643 \u0639\u0644\u064a\u0643 \u0627\u0646 \u062a\u0639\u0631\u0641 \u0647\u0644 \u0627\u0646\u062a \u062a\u0639\u0637\u064a \u0625\u0634\u0627\u0631\u0629 \u0628\u0623\u0646 ( \u0627\u0644\u0628\u0627\u0628 \u0645\u0641\u062a\u0648\u062d )\u061f<\/span><\/p><p><span style=\"color: rgb(38, 38, 38);\">\u0648\u0628\u0627\u0644\u062a\u0627\u0644\u064a \u062a\u062c\u0630\u0628 \u0627\u0644\u0645\u0632\u064a\u062f \u0645\u0646 \u0627\u0644\u0645\u0633\u062a\u063a\u0644\u064a\u0646<\/span><\/p><p><br><\/p><p><span style=\"color: rgb(38, 38, 38);\">\u0641\u064a \u0648\u0642\u062a \u0645\u0646 \u0627\u0644\u0623\u0648\u0642\u0627\u062a \u0643\u0627\u0646 \u0645\u0646 \u0627\u0644\u0645\u0645\u0643\u0646 \u0627\u0646 \u0627\u062a\u0639\u0631\u0641 \u0639\u0644\u0649 \u0634\u062e\u0635 \u0641\u064a \u0627\u0644\u0634\u0627\u0631\u0639 \u062b\u0645 \u0627\u062d\u0648\u0644\u0647 \u0627\u0644\u0649 \u0635\u062f\u064a\u0642 \u060c \u0641\u064a \u0648\u0642\u062a \u0645\u0646 \u0627\u0644\u0623\u0648\u0642\u0627\u062a \u0643\u0627\u0646 \u0645\u0646 \u0627\u0644\u0645\u0645\u0643\u0646 \u0627\u0633\u062a\u0642\u0628\u0644 \u0634\u062e\u0635 \u063a\u0631\u064a\u0628 \u0641\u064a \u0628\u064a\u062a\u064a \u060c \u0641\u064a \u0648\u0642\u062a \u0645\u0646 \u0627\u0644\u0623\u0648\u0642\u0627\u062a \u0633\u0645\u062d\u062a \u0644\u0634\u062e\u0635 \u0644\u0627 \u0645\u0633\u0624\u0648\u0644 \u0627\u0646 \u064a\u0633\u062a\u062e\u062f\u0645 \u0645\u0646\u0632\u0644\u064a \u0643\u0641\u0646\u062f\u0642 \u0644\u0647 \u060c \u0641\u064a \u0648\u0642\u062a \u0645\u0646 \u0627\u0644\u0623\u0648\u0642\u0627\u062a \u0633\u0645\u062d\u062a \u0644\u0639\u0644\u0627\u0642\u0629 \u0628\u0623\u0646 \u062a\u0633\u062a\u0645\u0631 \u0628\u0639\u062f \u0623\u0646 \u0628\u062f\u0623\u062a \u0628\u0637\u0631\u064a\u0642\u0629 \u062e\u0627\u0637\u0626\u0629<\/span><\/p><p><br><\/p><p><span style=\"color: rgb(38, 38, 38);\">\u0643\u0644 \u0647\u0630\u0647 \u0627\u0644\u0639\u0644\u0627\u0642\u0627\u062a \u0643\u0627\u0646\u062a \u0646\u062a\u064a\u062c\u0629 ( \u0627\u0644\u0628\u0627\u0628 \u0627\u0644\u0645\u0641\u062a\u0648\u062d ) \u0648\u0627\u0644\u0646\u064a\u0629 \u0627\u0644\u063a\u064a\u0631 \u0648\u0627\u0636\u062d\u0629 <\/span><\/p><p><span style=\"color: rgb(38, 38, 38);\">\u0643\u0644 \u0647\u0630\u0647 \u0627\u0644\u0639\u0644\u0627\u0642\u0627\u062a \u0643\u0627\u0646\u062a \u0627\u0633\u062a\u063a\u0644\u0627\u0644\u064a\u0629 \u0648 \u0641\u0648\u0636\u0648\u064a\u0629 <\/span><\/p><p><span style=\"color: rgb(38, 38, 38);\">\u0643\u0644 \u0647\u0630\u0647 \u0627\u0644\u0639\u0644\u0627\u0642\u0627\u062a \u0627\u0646\u062a\u0647\u062a \u0646\u0647\u0627\u064a\u0627\u062a \u063a\u064a\u0631 \u0633\u0639\u064a\u062f\u0629  <\/span><\/p><p><br><\/p><p><strong style=\"color: rgb(38, 38, 38);\">\u0648\u0627\u0644\u0633\u0624\u0627\u0644 \u0627\u0644\u0622\u0646:<\/strong><\/p><p class=\"ql-align-right\"><strong style=\"color: rgb(38, 38, 38);\">\u0647\u0644 \u0628\u0627\u0628\u0643 \u0645\u0641\u062a\u0648\u062d\u061f \u0648\u0647\u0644 \u062a\u0639\u0637\u064a \u0625\u0634\u0627\u0631\u0629 \u0644\u0630\u0644\u0643 \u061f<\/strong><\/p>","active":true,"can_comment":true,"meta":null,"user_id":"158e72ff-9a1d-42f9-8345-99300fee6313","created_at":"2022-05-31 17:32:12","updated_at":"2022-05-31 21:41:32"}  
[2022-07-03 14:16:10] local.ALERT: line2  
[2022-07-03 14:16:10] local.ALERT: line5  
fh32000 commented 2 years ago

@dmyers @Livijn @ludo237 @atmonshi

fh32000 commented 2 years ago

Sorry for disturbing you. The problem with using postman need to 'ignore_bots' => false,