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

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条)

报告相同问题?

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝