gpt4 book ai didi

php - Laravel isEmpty 不起作用

转载 作者:行者123 更新时间:2023-12-05 00:40:22 25 4
gpt4 key购买 nike

当我尝试使用 isEmpty 函数检查模型是否为空时,Laravel 5.6 出现错误。

型号:

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(),
]);
}

查看:

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

我的想法是我重用 edit.blade View 文件来创建和更新数据,所以我需要检查模型是否为空以在 View 文件中进行适当的更改。

但在这种情况下,Laravel 5.6 会抛出错误:

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

最佳答案

另一种方法是使用 Model exists 变量。例如:

//Case 1
$project = new Projects;
$project->exists; //Has false

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

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

关于php - Laravel isEmpty 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50024294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com