简体   繁体   中英

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

I am developing one api which is responsible for sending notification to mail this is working , now i want to update my Books table cart field all values should be zero(0) after successfully sending mail ,How to update a cart field values in my books table all fileds set to be 0 when the notification successfully send to my mail ,How to acheive this thing please help me to fix this issue..

这是我的数据库

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]);
  }



  }

you simply use update query inside if condition

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

You can do this through Eloquent:

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

Or DB Query Builder:

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

You can try to use Laravel Mass Updates:

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

Make sure the cart field in Books Model is included in your $fillable

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

Update:

Credit to the Accepted Answer:

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

First solution its simple just get the model whichever you want

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

Second If you want to create then you could use updateorcreate method simply

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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