繁体   English   中英

如何更新 laravel-8 中的特定列值?

[英]How to update a particular column values in laravel-8?

我正在开发一个 api,它负责向邮件发送通知这是有效的,现在我想更新我的 Books 表购物车字段,成功发送邮件后,所有值都应为零(0),如何更新我书中的购物车字段值当通知成功发送到我的邮件时,表所有字段设置为 0,如何实现此功能,请帮助我解决此问题..

这是我的数据库

Books Migration table

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateBooksTable extends Migration
{
  
    public function up()
    {
        Schema::create('books', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('author');
            $table->integer('price');
            $table->integer('quantity');
            $table->string('file')->nullable();
            $table->longText('description');
            $table->enum('cart', [0, 1])->default(0);
            $table->unsignedBigInteger('user_id');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->timestamps();
        });
    }

}

CustomersController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Customers;
use App\Models\Orders;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\DB;
use App\Models\User;
use App\Http\Resources\Books;
use App\Models\Books as ModelsBooks;
use Illuminate\Support\Facades\Auth;
use App\Notifications\orderSuccessfullNotification;
use PhpParser\Node\Expr\AssignOp\Mod;

class CustomersController extends Controller
{
  public function orderSuccessfull(Request $request){
      $cust=new Customers();
      $cust->user_id = auth()->id();
      $cust_id=Customers::where('user_id',$cust->user_id)->value('user_id');
     
      $user_email=User::where('id',$cust_id)->value('email');      
      $order = User::where('email', $user_email)->first();
      
      $ord = Orders::create(        
        [
            'orderNumber' => $order->orderNumber=rand(11111111,99999999),
            'customer_id'=>$order->id,
            'order_date'=>$order->order_date=Carbon::now(),      
        ]
    );

    $bookgetter1 = DB::table("Books")->select('name')->where('cart',['1'])->get();
    $bookgetter2 = DB::table("Books")->select('price')->where('cart',['1'])->get();
  
 
    if($order && $ord){
    $order->notify(new orderSuccessfullNotification($ord->orderNumber,$bookgetter1,$bookgetter2));
    //Here i want to update my books database cart field values should be 0
    //Books::where('cart')->values('0);
    }
      return response()->json(['message'=>'order created successfully','orderID'=>$ord->orderNumber]);
  }



  }

您只需在 if 条件中使用更新查询

if($order && $ord){
    $order->notify(new orderSuccessfullNotification($ord->orderNumber,$bookgetter1,$bookgetter2));
  
    ModelsBooks::Where('cart','1')->update((['cart'=>'0']));
  }

你可以通过 Eloquent 做到这一点:

ModelsBooks::where('cart','1')->update(['cart'=>'0']);

或数据库查询生成器:

DB::table('models_books')->where('cart', 1)->update(['cart' => '0']);

您可以尝试使用 Laravel 批量更新:

ModelsBooks::where('id',1)->update(['cart'=>0]);

确保Books模型中的cart字段包含在您的$fillable

class Books extends Model
{
 /**
  * The attributes that are mass assignable.
  *
  * @var array
  */
  protected $fillable = ['cart'];
}

更新:

归功于已接受的答案:

ModelsBooks::where('id',1)->update((['cart'=>0]));

第一个解决方案很简单,只需获取您想要的模型

$book = Book::where("condition")->first();
$book->fill(['book_price'=>25 // just example])
$book->update();

第二如果你想创建那么你可以简单地使用 updateorcreate 方法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM