有没有办法使用雄辩模型更新Laravel中的记录,只要对该记录进行了更改?我不希望任何用户一遍又一遍地请求数据库没有正当理由,只需按下按钮即可保存更改.我有一个javascript功能,根据页面中是否有更改,启用和禁用保存按钮,但我想知道是否可以确保在服务器端执行此类功能.我知道我可以自己完成它(意思是:没有吸引框架的内部功能)只是通过检查记录是否有变化,但在这样做之前,我想知道Laravel雄辩的模型是否已经处理好了那,所以我不需要重新发明轮子.
这是我用来更新记录的方式:
$product = Product::find($data["id"]);
$product->title = $data["title"];
$product->description = $data["description"];
$product->price = $data["price"];
//etc (string values were previously sanitized for xss attacks)
$product->save();
Run Code Online (Sandbox Code Playgroud) 我有一个表单,并希望拦截表单提交以使用bootbox显示确认对话框.
如果用户点击OK,则表单应该提交,如果不是,则应该保留在页面上.
我试过这个:
$('#myForm').submit(function() {
return bootbox.confirm("Are you sure?");
});
Run Code Online (Sandbox Code Playgroud)
但是,bootbox.confirm()立即返回,再次隐藏确认对话框.
然后我注意到有一个回调参数bootbox.confirm().但是,如果我$('#myForm').submit()要从回调中调用,这显然会再次显示确认对话框.
那么确认表单提交的正确方法是什么?
我正试图在JUnit和EasyMock中设置测试,我遇到了一个小问题,我似乎无法解决这个问题.我希望有人可以提供帮助.
这是我正在尝试测试的方法的简化版本:
public void myMethod() {
//(...)
Obj myObj = this.service.getObj(param);
if (myObj.getExtId() != null) {
OtherObj otherObj = new OtherObj();
otherObj.setId(myObj.getExtId());
this.dao.insert(otherObj);
}
//(...)
}
Run Code Online (Sandbox Code Playgroud)
好的,所以使用EasyMock我已经嘲笑了这个service.getObj(myObj)电话,并且工作正常.
我的问题出现在JUnit遇到dao.insert(otherObj)调用时.EasyMock抛出了*Unexpected Method Call*它.
我不介意在我的测试中嘲笑那个dao并使用expectLastCall().once();它,但是假设我在"otherObj"上有一个句柄,它在插入时作为参数传递...当然我没有,因为它是有条件的在被测试方法的上下文中创建.
任何人都曾经不得不处理这个并以某种方式解决了它?
谢谢.
尽管我不使用和严格的模拟,但我的EasyMock的预期方法被认为是意外的,并且该方法已在声明之前进行了声明。
测试在以下代码行中失败:
Intent batteryIntent = context.getApplicationContext().registerReceiver(null,
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Run Code Online (Sandbox Code Playgroud)
测试:
@Before
public void setUp() {
mocksControl = createControl();
contextMock = mocksControl.createMock(Context.class);
//(...)
}
@Test
public void test() {
expect(contextMock.getApplicationContext()).andReturn(contextMock).anyTimes();
expect(contextMock.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)))
.andReturn(someIntent1).once();
expect(contextMock.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)))
.andReturn(someIntent2).once();
mocksControl.replay();
//(...) tested method is invoked on class under the test
}
Run Code Online (Sandbox Code Playgroud)
错误我得到:
java.lang.AssertionError:
Unexpected method call Context.registerReceiver(null, android.content.IntentFilter@c009614f):
Context.registerReceiver(null, android.content.IntentFilter@c009614f): expected: 1, actual: 0
Context.registerReceiver(null, android.content.IntentFilter@c009614f): expected: 1, actual: 0
Run Code Online (Sandbox Code Playgroud) 几个月前,我将 Xamarin 更新为 4.5.0.476(Xamarin Android 7.3.1.2),今天更新应用程序时,我发现我无法在发布模式下运行我的 apk,因为它在运行时崩溃。我不明白为什么。
我试图在 android manifest 上设置所有类型的权限。
链接器设置仅是SDK 程序集,但我也尝试过None。
我清理和重建了好几次。
没有可能在启动时失败的条件代码。
没什么,打开主要活动时应用程序总是崩溃。
def rect_extend(x):
m, n = 1
while 1 < x:
m = m + 1
n = n + 1
return m, n
Run Code Online (Sandbox Code Playgroud)
这个简单的函数返回:
'int'对象不可迭代
iPython中的错误.我不知道它为什么这样做,while功能不起作用 - 条件似乎是true.
(虽然条件是故意简化的;原始代码没有它)
我想创建一个包含几个城市的区域。所以我决定使用 jQuery Select2
这是我的创建表单多项选择
<div class="form-group {{ $errors->has('cities') ? 'has-error' : '' }}">
<label>Tentukan Kota</label>
<select name="cities[]" class="city form-control" data-placeholder="Pilih Kota" style="width: 100%;" multiple="multiple">
@foreach($cities as $city)
<option value="{{ $city->id }}">{{ $city->name }}</option>
@endforeach
</select>
</div>
Run Code Online (Sandbox Code Playgroud)
我可以像在文档中一样进行多项选择。
这是我的控制器,它处理显示创建表单
public function zone_create()
{
$cities = City::where('zone_id', null)->get();
return view('backend.admin.pricings.zone_create', compact('cities'));
}
Run Code Online (Sandbox Code Playgroud)
这种关系是一个区域有多个城市。
class Zone extends Model
{
protected $fillable = [
'name',
];
public function cities(){
return $this->hasMany(City::class);
}
}
Run Code Online (Sandbox Code Playgroud)
城市所属区域
class City extends Model
{
protected $fillable = [
'zone_id', …Run Code Online (Sandbox Code Playgroud)