![](/img/trans.png)
[英]how to get latest record from database table against each value in column 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.