Laravel是否有某种功能允许我在Table-A
中插入行,但必须插入Table-B
,否则会失败?
Table-B
表是多态关系。
答案 0 :(得分:1)
正如@ C2486在答案中所说,你应该使用交易。但是,您无需使用try
和catch
。在Laravel文档的database transactions部分中,您可以看到以下示例:
DB::transaction(function () {
DB::table('users')->update(['votes' => 1]);
DB::table('posts')->delete();
});
此方法将自动为您提交,如果在执行期间发生异常,它也会自动为您回滚。
答案 1 :(得分:0)
使用beginTransaction
确保运行所有查询。
function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms));}
async function exec(){
const u = await getCurrUser(action.w); //get current user login data from indexeddb io
await sleep(2000);
return u;
};
exec()
.then(u => {
console.log(u);
})
.catch(e => {
// deal with the error -- you MUST always either handle
// rejections or return the promise chain to something that will
});
export function getCurrUser(window) {
return new Promise((resolve, reject) => {
// ...
_req.onsuccess = function(event) {
let cursor = event.target.result;
//confirm in this line that user object does has some value
if (cursor) {
resolve(Object.assign({}, cursor);
} else {
reject(new Error(/*...*/));
}
};
});
};
对于模特,您可以查看this answer