我正在使用"查询"构建器插入数据所有字段都已插入,但是时间戳如created_at和updated_at未插入它们都具有默认的0:0:0值我的插入查询是
$id = DB::table('widgets')
->insertGetId(array(
'creator' => Auth::user()->id,
'widget_name' => $request->input('widget_name'),
'pages' => json_encode($request->input('pages')),
'domain' => $request->input('domain'),
"settings" => $settings,
));
Run Code Online (Sandbox Code Playgroud) 我正在使用"Laravel中的Eloquent ORM"执行数据库操作.我只想在数据库中获取最后一个插入ID(不是最大id).
我在laravel Eloquent ORM中搜索获取最后一个插入ID,我得到以下链接(Laravel,使用Eloquent获取最后一个插入ID),这是指从后面的函数" $data->save()" 获取最后一个插入ID .
但我需要得到
" Model::create($data)";
我的查询:
GeneralSettingModel::create($GeneralData);
User::create($loginuserdata);
Run Code Online (Sandbox Code Playgroud)
如何检索最后插入的ID?
标题大多是不言自明的.Eloquent有一个叫做的方法
updateOrCreate()
记录在这里:https://laravel.com/docs/5.5/eloquent#other-creation-methods
在某些情况下,这非常有用.但是在执行之后,updateOrCreate()我需要更新/创建的对象或其主键或其ID.
当然,我可以做,MyModel::where(...)->first()并再次提供所有这些数据,但这是笨拙的,可能是一些昂贵的要求.
但是updateOrCreate()只返回true或false.
有任何想法吗?
这是我的代码
$users = DB::table('users')->insert(array(
'email_id' => $email_id,
'name' => $name,
));
$lastInsertedID = $users->lastInsertId();
return $lastInsertedID;
Run Code Online (Sandbox Code Playgroud)
我想为特定记录获取最后插入的id.我尝试过使用Eloquent ORM.但它不起作用.
任何帮助将不胜感激.
谢谢.
我正在使用Laravel Auth让用户能够注册.我现在正在尝试的是:在用户注册后(如果他们选择了特殊角色),另一行插入另一个表(然后是用户),其中包含相关的用户ID.这是它的代码:
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use App\Complaint;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after login / registration.
*
* @var string
*/ …Run Code Online (Sandbox Code Playgroud) 我知道要获取我可以使用的最后一个ID,insertGetId()但是我想知道如何通过save()方法获取最后一个插入的ID 。
order = new Store_order;
$order->invoice_id = $signed['invoice_id'];
$invoice = $order->save()['so_id'];
Run Code Online (Sandbox Code Playgroud)
我用这个但是它返回null值
我目前有一个仅在数据库中创建用户记录的功能
$user->name;
$user->address;
$user->save();
Run Code Online (Sandbox Code Playgroud)
完成此操作后,我在用户表中为其创建了一个ID。
我知道我可以返回$ user,但是具体来说,我该如何从中获取ID和名称,以便在这样的调用中使用:
$user->save();
//return $user id and $user name
$update = new UpdateTable($id,$name);
Run Code Online (Sandbox Code Playgroud)