tutorials
June 10, 2022

LARAVEL - NOTIFICATIONS

Loyihani yaratish davomida foydalanuvchilarga loyihadagi o'zgarishlar va yoki qandaydur ma'lumotlarni foydalanuvchiga bildirishga to'g'ri keladi. Foydalanuvchiga yangi tovar qo'shilgani, tovarga to'lov muvaffaqiyatli amalga oshgani hullas bir so'z bilan aytganda qandaydur xabarnoma jo'natish kerak.

Bu vazifani bajarish uchun bizzga kanallar kerak. Ya'ni qaysidur kanal orqali foydalanuvchiga ma'lumot beramiz. Laravelda quyidagi kanallar mavjud:

  1. Mail
  2. Sms
  3. Slack
  4. Database

Bunga qoshimcha tarzda comunity tomonidan ham juda ko'plab packagelar yaratilgan.

Bu postimizda kichik blog yasab har bir post qo'shilganda foydalanuvchiga post qo'shilganligi haqida kichik notifikatsia jo'natamiz.

1. Post Table va Model.

php artisan make:model Post -m

  return new class extends Migration
  {
    /**
     * Run the migrations.
     *
     * @return void
     */
     public function up()
     {
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->text('body');
            $table->string('category_id');
            $table->timestamps();
        });
     }
     /**
     * Reverse the migrations.
     *
     * @return void
     */
     public function down()
     {
         Schema::dropIfExists('posts');
     }
   };

Endi Post table uchun model yaratib olamiz:

<?php 
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Post extends Model
{
   use HasFactory;
   protected $fillable = [
      'title',
      'body',
      'category_id'
   ];
   
   public function category()
   {
      return $this->belongsTo(Category::class);
   }
}

2. Category Table va Model

php artisan make:model Category -m

<?php 

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
     public function up()
     {
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->timestamps();
        });
     }
     /**
     * Reverse the migrations.
     *
     * @return void
     */
     public function down()
     {
         Schema::dropIfExists('categories');
     }
 };

Endi model yaratib olamiz

<?php
namespace App/Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Category extends Model
{
   use HasFactory;
   protected $fillable = [
      'name',
   ];
   
   public function post()
   {
      return $this->hasMany(Post::class);
   }

3.Notificationni projectga qo'shish

php artisan make:notification NewMessage

Terminalga shu codene yozganimizdan so'ng app/Notifications/NewMessage.php

class yaratiladi. Endi bu class ichida nimalar bo'layotganiga e'tibor beramiz.

public functionpublic function via($notifiable)
{
    return ['mail']
}

Via methodi ichida arrayda notifikatsia driverlari qaytariladi. Qanday driverlar ishlatish odatda client hohishiga qarab bo'ladi.