Laravel您要求1件物品,但只有0件物品可用

时间:2017-10-19 08:08:48

标签: php laravel

我使用eloquent:

将row的列随机地添加到种子数据库中
$physician = SelectOption::where('select_option_group_id', 1)->pluck('name')->random();

如果select_options表中存在数据,则它有效。但如果它不存在,则会出错:

  

您申请了1项,但只有0项可用。

我想把它留空,如果它是空的。

2 个答案:

答案 0 :(得分:5)

在执行random()之前检查集合是否为空:

$collection = SelectOption::where('select_option_group_id', 1)->pluck('name');
if (!$collection->isEmpty()) {
    $physician = $collection->random();
} else {
    ...
}

答案 1 :(得分:1)

改为使用$physician = SelectOption::where('select_option_group_id', 1)->inRandomOrder()->first(); $name = is_null($physician) ? 'No data available' : $physician->name;

var score = 0;

function checkCollision(elm1, elm2) {
  var elm1Rect = elm1.getBoundingClientRect();
  var elm2Rect = elm2.getBoundingClientRect();

  return (elm1Rect.right >= elm2Rect.left &&
      elm1Rect.left <= elm2Rect.right) &&
    (elm1Rect.bottom >= elm2Rect.top &&
      elm1Rect.top <= elm2Rect.bottom);
}

function moveZombie(evt) {
  var key;

  var zombie = document.getElementById('zombie');
  var brain = document.getElementById('brain');
  var scoreDiv = document.getElementById('score');

  var x = parseInt(zombie.style.left);
  var y = parseInt(zombie.style.top);

  switch (evt.keyCode) {
    case 37:
      zombie.style.left = `${x+-15}px`;
      break;
    case 39:
      zombie.style.left = `${x+15}px`;
      break;
    case 38:
      zombie.style.top = `${y+-15}px`;
      break;
    case 40:
      zombie.style.top = `${y+15}px`;
      break;
  }


  setTimeout(function() {

    if (checkCollision(brain, zombie)) {
      scoreDiv.innerHTML = ++score;
    }
  }, 150);


}

window.addEventListener('keydown', moveZombie);