my id field in a database named des_id is there a specific way to change from table_name.id to table_name.des_id?
Route::post('Specific/uri', function (Request $request) {
$request->validate([
'destination_id' => 'required|exists:Database Name.Table Name,des_id',
'from' => 'required|numeric|min:0',
'to' => 'required|numeric|min:0',
]);
$destination_id = Destination::findOrFail($request->destination_id);
$from = $request->from;
$to = $request->to;
dispatch(new testJob($destination_id, $from, $to));
return response()->json([
'status' => true
]);
});
CodePudding user response:
You won't be able to use the find or findOrFail if you don't use the standard naming convention for the identifier as laravel expects which is id. findOrFail expects the primary key to be named "id".
Because your primary key is named "des_id", for you to have the same exception behavior, you should use the where clause then end it with the firstOrFail() method which will offer a similar behavior to findOrFail.
You can learn more about findOrFail and firstOrFail here.
This is how you should perform that call to have the same Not Found Exception handling as findOrFail.
$destination_id = Destination::where('des_id','=',$request->destination_id)->firstOrFail();