編輯:我用 dd() 進行了測驗,它到達了函式并且一切運行正常但沒有插入資料。
我有一個名為 ProductCustomer 的模型,我可以在測驗中使用 ProductCustomer::create() 函式保存這個模型,但我不能使用相同的方法從服務類創建它。它在嘗試保存但不插入資料時不會引發例外。
我的模型
class ProductCustomer extends Model
{
use HasFactory;
protected $fillable = [
'product_id',
'name',
'lastname',
'city',
'district',
'address',
'email',
'phone'
];
public function product()
{
return $this->belongsTo(Product::class);
}
}
我的模型的遷移。
public function up()
{
Schema::create('product_customers', function (Blueprint $table) {
$table->id();
$table->string("product_id");
$table->string("name");
$table->string("lastname");
$table->string("city");
$table->string("district");
$table->string("address");
$table->string("email");
$table->string("phone");
$table->timestamps();
$table->foreign('product_id')->references('id')->on('products')
->onDelete('cascade');
});
}
我的插入測驗通過了。
public function test_insert()
{
$product = Product::factory()->create();
$pc = ProductCustomer::create([
'product_id' => $product->id,
'name' => $this->faker->name,
'lastname' => $this->faker->lastName,
'city' => $this->faker->city,
'district' => $this->faker->city,
'address' => $this->faker->address,
'email' => $this->faker->email,
'phone' => $this->faker->phoneNumber,
]);
$this->assertModelExists($pc);
}
所以令人興奮的部分是這個測驗可以將資料插入資料庫。但在下面,沒有例外,沒有錯誤但沒有行動。
private function insertCustomer($request, $productId)
{
try {
return ProductCustomer::create([
'product_id' => $productId,
'name' => $request->name,
'lastname' => $request->lastname,
'city' => $request->city,
'district' => $request->state,
'address' => $request->address,
'email' => $request->mail,
'phone' => $request->phone,
]);
} catch (QueryException $e)
{
dd($e);
}
}
public function commitPayment(Request $request, $productId) {
$this->insertCustomer($request,$productId);
$response = $this->preparePayment($request,$productId)->save();
return $this->isPaymentSuccess($response);
}
您可以在下面看到 $request 和 $productId。沒有問題。
request: Symfony\Component\HttpFoundation\InputBag {#44 ▼
#parameters: array:9 [▼
"_token" => "lOI9cI4GQ9OWtciQgAWAQX3b48sw2KqcGmhKqXi2"
"productId" => "99ef04f9-612f-43f1-a39e-56f94a96edf7"
"name" => "John"
"lastname" => "Doe"
"city" => "Istanbul"
"state" => "Uskudar"
"address" => "Test address"
"mail" => "[email protected]"
"phone" => " 905555454545"
]
}
^ "99ef04f9-612f-43f1-a39e-56f94a96edf7"
編輯 2:為了測驗,我為 Product Customer::created 事件添加了 dd()。它被觸發了,所以這意味著 Product Customer::create() 作業正常。但在資料庫中,沒有與測驗相反的記錄。這是 dd($model) 結果。
App\Models\Payment\ProductCustomer {#1534 ▼
#connection: "mysql"
#table: "product_customers"
#primaryKey: "id"
#keyType: "int"
incrementing: true
#with: []
#withCount: []
preventsLazyLoading: false
#perPage: 15
exists: true
wasRecentlyCreated: true
#escapeWhenCastingToString: false
#attributes: array:11 [▼
"product_id" => "42d9e84e-7d4c-49c8-8ddf-2327300a3a64"
"name" => "John"
"lastname" => "Doe"
"city" => "HATAY"
"district" => "HASSA"
"address" => "test address"
"email" => "[email protected]"
"phone" => "4445556677"
"updated_at" => "2022-06-02 15:51:55"
"created_at" => "2022-06-02 15:51:55"
"id" => 12
]
#original: []
#changes: []
#casts: []
#classCastCache: []
#attributeCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
timestamps: true
#hidden: []
#visible: []
#fillable: array:8 [▼
0 => "product_id"
1 => "name"
2 => "lastname"
3 => "city"
4 => "district"
5 => "address"
6 => "email"
7 => "phone"
]
#guarded: array:1 [▼
0 => "*"
]
}
編輯3:最后我發現了問題。我從路線到結束跟蹤非測驗用例的方式。我注意到一DB:prepareCommit開始我有一個命令。最后,一切正常,但我有一個事件試圖獲取我試圖創建但尚未提交的 ProductCustomer DB:commit。因此事件失敗,因為記錄不存在并且資料庫回滾。
uj5u.com熱心網友回復:
最后我發現了問題。我從路線到結束跟蹤非測驗用例的方式。我注意到一DB:prepareCommit開始我有一個命令。最后,一切正常,但我有一個事件試圖獲取我試圖創建但尚未提交的 ProductCustomer DB:commit。因此事件失敗,因為記錄不存在并且資料庫回滾。
uj5u.com熱心網友回復:
嘗試使用Throwable捕獲所有錯誤。Throwable 是可以通過 throw 陳述句拋出的任何物件的基本介面,包括 Error 和 Exception。
<?php
try {
// your code
} catch (Throwable $e) {
echo 'Catch Exception and Error exceptions';
}
一個常見的錯誤是,當您預期 A 型別的錯誤但遇到 B 型別的錯誤并且無法理解為什么它沒有落入您預期 A 型別錯誤的 catch 塊中時。
uj5u.com熱心網友回復:
你不應該使用DB::commitand DB:prepareCommit,更好的方式來使用事務:
DB::beginTransaction();
try {
DB::insert(...);
DB::insert(...);
DB::insert(...);
DB::commit();
// all good
} catch (\Exception $e) {
DB::rollback();
// something went wrong
}
請參閱交易檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/485540.html
上一篇:如何在laravel的withCount中添加有子句?
下一篇:讓用戶和客戶端共享相同的身份驗證
