Paradox
12 ماه پیش توسط Paradox مطرح شد
0 پاسخ

بلاک و ریپورت کاربران

سلام وقت بخیر
من میخام از طریق لایووایر برای برنامه چت گزینه بلاک و انبلاک و ریپورت بزارم
به جدول ایجاد کردم به این شکل

    Schema::create('user_logs', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->foreignId('user_id')->constrained();
            $table->foreignId('blocked_user_id')->nullable()->constrained('users');
            $table->enum('action', ['blocked', 'unblocked', 'reported'])->default('unblocked');
            $table->timestamps();
        });

model user


    public function logs()
    {
        return $this->hasMany(UserLog::class);
    }

    public function blockedUsers()
    {
        return $this->hasMany(UserLog::class, 'user_id', 'id');
    }

model userlog

    protected $fillable = [
        'user_id',
        'blocked_user_id',
        'action',
    ];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function blockedUser()
    {
        return $this->belongsTo(User::class, 'blocked_user_id');
    }

و حالا تو component به این شکل نوشتم

    public $userId;

   public function blockUser()
    {
        $blockedUser = User::find($this->userId);

        if ($blockedUser) {
            $this->user->logs()->create([
                'user_id' => auth()->user()->id,
                'blocked_user_id' => $blockedUser->id,
                'action' => 'blocked',
            ]);
            event(new UserBlocked($blockedUser->id));
            session()->flash('message', 'User blocked successfully!');
        } else {
            session()->flash('message', 'Invalid user ID!');
        }
    }

ایونت

class UserBlocked
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $blockedUserId;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($blockedUserId)
    {
        $this->blockedUserId = $blockedUserId;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('user.'. $this->blockedUserId);

    }
}

تو بلید هم به این شکل

                    <button wire:click="blockUser({{ $userId->id }})">Block User</button>

اروری که دریافت میکنم

Attempt to read property "id" on null

لطفا راهنمایی کنید مشکل کارم کجاست این ایدی که تو بلید تعریف کردم رو نمیشناسه

                    <button wire:click="blockUser({{ $userId->id }})">Block User</button>

ثبت پرسش جدید

به همدیگه کمک کنیم

به Paradox کمک کنید تا مشکل خودش را حل کند؛ این‌طور می‌توانیم با هم پیشرفت کنیم.

برای ارسال پاسخ لازم است وارد شده یا ثبت‌نام کنید

ورود یا ثبت‌نام