Mysql 如何在laravel中使用IFNull
我有一个问题,当表中没有数据时,customer_id为空。我知道有一个函数IFNULL,我可以使用它将customer\u id null更改为0。这是我的问题,它不起作用。检查了堆叠流程中解决的许多相关问题,但我自己找不到解决方案,如果有人能帮助我,他会很好。 它告诉我这个错误 “消息”:“正在尝试获取非对象的属性” 客户控制器代码为Mysql 如何在laravel中使用IFNull,mysql,laravel,Mysql,Laravel,我有一个问题,当表中没有数据时,customer_id为空。我知道有一个函数IFNULL,我可以使用它将customer\u id null更改为0。这是我的问题,它不起作用。检查了堆叠流程中解决的许多相关问题,但我自己找不到解决方案,如果有人能帮助我,他会很好。 它告诉我这个错误 “消息”:“正在尝试获取非对象的属性” 客户控制器代码为 public function store(Request $request) { // try {
public function store(Request $request)
{
//
try {
$this->validate($request,[
'name'=>'required',
'contact'=>'required|unique:Customers',
// 'contact'=>'required',
'address'=>'required',
'email'=>'required|string|email|max:191|unique:Customers',
]);
$getId = DB::table('Customers')->select('*', DB::raw('ifnull(id,0)'))->first();
$getfirst = $getId->id;
if($getfirst == 0)
{
$getfirst = 1;
$incId = $getfirst;
}
else{
$incId = $getfirst+1;
}
// $lastInsertedId= $Customer->id;
$Customer= Customer::create([
'name'=>$request['name']."-". $incId ,
'contact'=>$request['contact'],
'address'=>$request['address'],
'email'=>$request['email']
]);
return response()->json($Customer);
}
catch (Exception $e) {
return response()->json($e->getMessage(), 500);
}
}
public function up()
{
Schema::create('customers', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name')->default("مشتری");
$table->integer('contact')->unique();
$table->string('address');
$table->string('email')->unique();
$table->softDeletes();
$table->timestamps();
});
}
客户表为
public function store(Request $request)
{
//
try {
$this->validate($request,[
'name'=>'required',
'contact'=>'required|unique:Customers',
// 'contact'=>'required',
'address'=>'required',
'email'=>'required|string|email|max:191|unique:Customers',
]);
$getId = DB::table('Customers')->select('*', DB::raw('ifnull(id,0)'))->first();
$getfirst = $getId->id;
if($getfirst == 0)
{
$getfirst = 1;
$incId = $getfirst;
}
else{
$incId = $getfirst+1;
}
// $lastInsertedId= $Customer->id;
$Customer= Customer::create([
'name'=>$request['name']."-". $incId ,
'contact'=>$request['contact'],
'address'=>$request['address'],
'email'=>$request['email']
]);
return response()->json($Customer);
}
catch (Exception $e) {
return response()->json($e->getMessage(), 500);
}
}
public function up()
{
Schema::create('customers', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name')->default("مشتری");
$table->integer('contact')->unique();
$table->string('address');
$table->string('email')->unique();
$table->softDeletes();
$table->timestamps();
});
}
我想你可以这样写:
DB::raw('IFNULL(id, 0)')
我想你可以这样写:
DB::raw('IFNULL(id, 0)')
试试这个
$getId = DB::table('Customers')->selectRaw(['*', 'IFNULL(id,0)'])->first();
试试这个
$getId = DB::table('Customers')->selectRaw(['*', 'IFNULL(id,0)'])->first();
IFNULL
用于检查字段是否可为null
因此,它不用于检查记录是否存在
您可以使用empty()
检查对象是否存在
$getId=DB::table('Customers')->first();
$getfirst=空($getId)?0:$getId->id;
IFNULL
用于检查字段是否可为空
因此,它不用于检查记录是否存在
您可以使用empty()
检查对象是否存在
$getId=DB::table('Customers')->first();
$getfirst=空($getId)?0:$getId->id;
selectRaw()必须是数组类型,字符串给定,称为inselectRaw()必须是数组类型,字符串给定,调用inplz post yourcustomers
table结构该表中是否有主键?是的,I-do id设置为主键PLZ post yourcustomers
table结构该表中是否有主键?是的,I-do id设置为主键