dpcnm2132 2018-04-25 14:01
浏览 1348
已采纳

Laravel isEmpty不起作用

There is an error in Laravel 5.6 when I try to check is a model empty with isEmpty function.

Model:

namespace App\Models\Projectmanagement;

use Illuminate\Database\Eloquent\Model;

class Projects extends Model
{
    protected $table = 'projects';
    public $timestamps = false;
}

Controller:

public function create()
{
    $project = new Projects;
    return view('pages.projectmanagement.projects.edit', [
        'project' => $project,
        'companies' => Company::all(),
    ]);
}

View:

<form method="post">
  {{ csrf_field() }}
  @if($project->isEmpty())
      @method('PUT')
   @endif
</form>

The idea is that I reuse edit.blade view file for creating and updating data, so I need to check if a model is empty to make proper changes in the view file.

But in this case, Laravel 5.6 throws an error:

Method Illuminate\Database\Query\Builder::isEmpty does not exist. (View: /var/www/resources/views/pages/projects/edit.blade.php)
  • 写回答

4条回答 默认 最新

  • doucong3048 2018-04-25 14:16
    关注

    Another way to get this is using Model exists variable. For example:

    //Case 1
    $project = new Projects;
    $project->exists; //Has false
    
    //Case 2
    $project = Projects::first();
    $project->exists; //Has true
    

    Ref: https://laravel.com/api/5.6/Illuminate/Database/Eloquent/Model.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?
  • ¥15 QTOF MSE数据分析
  • ¥15 平板录音机录音问题解决
  • ¥15 请问维特智能的安卓APP在手机上存储传感器数据后,如何找到它的存储路径?