I am very new at Laravel. So, I hope you will make my day better.
I have two main Models like User
and Notification
. Also, I created migrations like notification_user
to connect both models in Many to Many relations. Inside notification_user
migration, I made column like $table=>boolean('is_read')->default(0);
Now,
Question: How can I set the is_read
column value to 1
from DashboardController
.
Here is my code:
User Model
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes; // add new
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Auth; // add new
class User extends Authenticatable
{
use HasFactory, Notifiable, SoftDeletes;
public function notifications()
{
return $this->belongsToMany('App\Models\Notification'); // Many to Many relation
}
.
.
// more lines
}
Notification Model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Notification extends Model
{
use HasFactory;
public function users()
{
return $this->belongsToMany('App\Models\User'); // Many to Many relation
}
}
notification-user Migrations:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersNotificationsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('notification_user', function (Blueprint $table) {
$table->id();
$table->integer('user_id');
$table->integer('notification_id');
$table->boolean('is_read')->default(0);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('notification_user');
}
}
DasboardController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\Notification; // add new
use Illuminate\Support\Facades\Auth; // add new
use Carbon\Carbon; // add new
class DashboardController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
*
*/
public function markAllNotificationAsRead($id)
{
// $id is Auth::user()->id
foreach (Auth::user()->notifications as $notification) {
$notification->is_read = 1;
$notification->save();
}
return back()->withInput();
}
.
.
// more lines
}
You need withPivot
method:
public function notifications()
{
return $this->belongsToMany('App\Models\Notification')->withPivot('is_read');// Many to Many relation
}
then to update it:
public function markAllNotificationAsRead($id)
{
foreach (Auth::user()->notifications as $notification) {
$notification->pivot->is_read = 1;
$notification->pivot->save();
}
return back()->withInput();
}
or can bulk update using:
$user = Auth::user();
$user->notifications()
->newPivotStatement()
->where('user_id', $user->id)
->update(array('is_read' => 1);
alternative solution create UserNotification model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class UserNotification extends Model
{
protected $table = 'user_notification';
}
and add hasMany
relation to User model:
public function userNotifications()
{
return $this->belongsToMany('App\Models\UserNotification');
}
then to update it use like this
$user = Auth::user();
$user->userNotifications()
->update(array('is_read' => 1);