如何从 DashboardController 将 "is_read" 列值设置为 1?

How can I set the "is_read" column value to 1 from DashboardController?

我是 Laravel 的新手。所以,我希望你能让我的一天更美好。

我有两个主要模型,如 UserNotification。此外,我创建了 notification_user 之类的迁移,以连接多对多关系中的两个模型。在 notification_user 迁移中,我制作了像 $table=>boolean('is_read')->default(0);

这样的专栏

现在,

问题:如何将 is_read 列值从 DashboardController.

设置为 1

这是我的代码:

用户模型

<?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
    
}

通知模型:

<?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
    }
}

通知用户迁移:

<?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
}

你需要withPivot方法:

public function notifications()
{
    return $this->belongsToMany('App\Models\Notification')->withPivot('is_read');// Many to Many relation
}

然后更新它:

public function markAllNotificationAsRead($id)
{
    foreach (Auth::user()->notifications as $notification) {
        $notification->pivot->is_read = 1;
        $notification->pivot->save();
    }
    return back()->withInput();
}

或者可以使用以下方式进行批量更新:

$user = Auth::user();
$user->notifications()
    ->newPivotStatement()
    ->where('user_id', $user->id)
    ->update(array('is_read' => 1);


替代解决方案创建 UserNotification 模型:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class UserNotification extends Model
{
    protected $table = 'user_notification';
}

并添加 hasMany 与用户模型的关系:

public function userNotifications()
{
    return $this->belongsToMany('App\Models\UserNotification');
}

然后像这样更新它

$user = Auth::user();
$user->userNotifications()
    ->update(array('is_read' => 1);