我想用laravel(5.3)能言善辩的$casts属性一样protected $casts = ["example" => "object"]与getExampleAttribute访问,但因为它似乎访问丢弃的$casts行为。这对我来说至关重要,因为我想将 JSON 对象存储在数据库中并为其设置默认值,例如:
public function getExampleAttribute($value) {
if($value === NULL)
return new \stdclass();
return $value
}
Run Code Online (Sandbox Code Playgroud)
所以我永远不会在我的视图中得到 NULL 值。有没有办法比在访问器和修改器中显式实现强制转换逻辑更容易?
在带有 MySQL 的 laravel 5.5 中,我使用\Illuminate\Support\Facades\DB这种方式创建事务:
DB::transaction(function() {
...
});
Run Code Online (Sandbox Code Playgroud)
这种事务的隔离级别是什么,有没有办法显式设置它?
我正在尝试 Ionic 框架。我需要在 Android SQLite 中存储一些数据。所以,我想打开或创建一个数据库,如果数据库以前不存在,用一些数据填充它。
这里说create即使数据库已经存在我也使用方法:https :
//ionicframework.com/docs/native/sqlite/
所以我写了一个类:
export class Database {
private sqlite: SQLite;
private database: SQLiteObject = null;
constructor() {
this.sqlite = new SQLite();
}
private open_db() {
return this.sqlite.create({
name: 'data.db',
location: 'default'
})
.then((db: SQLiteObject) => {
this.database = db;
return db.sqlBatch([...some sql inserts and create tables...]);
});
});
public all() {
return this.open_db().then(() => this.database.executeSql("SELECT ...something...", []));
}
Run Code Online (Sandbox Code Playgroud)
现在,如何仅在实际创建数据库时调用 db.sqlBatch ,而不仅仅是打开?是的,这段代码会在没有合理解释的情况下抛出随机异常,可以在 logcat 中看到,任何建议将不胜感激。