Laravel isEmpty没有工作

时间:2018-04-25 14:01:37

标签: php laravel

当我尝试使用isEmpty函数检查模型为空时,Laravel 5.6中存在错误。

型号:

namespace App\Models\Projectmanagement;

use Illuminate\Database\Eloquent\Model;

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

控制器:

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视图文件来创建和更新数据,因此我需要检查模型是否为空以在视图文件中进行适当的更改。

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

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

4 个答案:

答案 0 :(得分:3)

另一种方法是使用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

答案 1 :(得分:1)

isEmpty()不是您班级的有效功能。

你的意思是使用php函数empty()吗?

在这种情况下,请尝试以下方法:

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

请注意,与$ project === null相比,empty($ project)的优化程度较低。

答案 2 :(得分:1)

void statements (void);
    void printweightsstatements(void);
void coping (void);
void printweightscoping(void);
void advice ();
int cumweightstatement();
int cumweightcoping();

void main()
{
 statements ();
 coping ();
**if (int cumweightstatement + int cumweightcoping>=20)** 

 printf ("You are very stressed and at a high risk for depression. Please 
see a doctor or psychiaqtrist as soon as possible\n");
 else if (cumweightstatement + cumweightcoping>=15 && int cumweightstatement + int cumweightcoping<20)
printf ("You are quite stressed and at a moderate risk for depression. Please see your school guidance counselor\n");
else if (cumweightstatement + cumweightcoping<=14)
printf ("You are mildly stressed and at a low risk for depression, but you could still benefit from using positive coping strategies\n")
}

void printweightsstatements()
{
 printf("1:Strongly disagree\n");
 printf("2:Disagree\n");
 printf("3:Neither agree nor disagree\n");
 printf("4:Agree\n");
printf("5:Strongly agree\n");

}


void statements(void)

{
 int weight1;
 int weight2;
 int weight3;
 int weight4;

 printf ("How much do you agree with the following statements?\n");

 printf ("Statement 1: I have trouble concentrating on my homework for 1 hour straight\n");
 printweightsstatements;
 printf("1:Strongly disagree\n");
 printf("2:Disagree\n");
 printf("3:Neither agree nor disagree\n");
 printf("4:Agree\n");
 printf("5:Strongly agree\n");

printf ("Statement 2: I do not have much control over events in my life\n");
printweightsstatements;
printf("1:Strongly disagree\n");
printf("2:Disagree\n");
printf("3:Neither agree nor disagree\n");
printf("4:Agree\n");
printf("5:Strongly agree\n");
scanf("%d", &weight2);
printf ("Statement 3: During this school year, I have been stressed a lot\n");
printweightsstatements;
printf("1:Strongly disagree\n");
printf("2:Disagree\n");
printf("3:Neither agree nor disagree\n");
printf("4:Agree\n");
printf("5:Strongly agree\n");
scanf("%d", &weight3);
printf ("Statement 4: Although I try very hard, there is always some schoolwork too difficult for me\n");
printf("1:Strongly disagree\n");
printf("2:Disagree\n");
printf("3:Neither agree nor disagree\n");
printf("4:Agree\n");
printf("5:Strongly agree\n");
scanf("%d", &weight4);
printweightsstatements;

int cumweightstatement=weight1+weight2+weight3+weight4;
}


void coping (void);
{
  int weight1;
  int weight2;
  int weight3;
  int weight4;
  int weight5;

printf ("How often do you use each of the following coping strategies?\n");
printf ("Strategy 1: Negative self-talk (for example, I can’t do this,this is too hard for me, I’ll never get good at this)\n");
printweightscoping;
scanf("%d", &weight1);
printf ("Strategy 2: Overusing Internet/social media/phone for non-school-related activities (over 1 hour/day)\n");
printweightscoping;
scanf("%d", &weight2);
printf ("Strategy 3: Extended sleep or naps (above 8-9 hours of regular sleep)\n");
printweightscoping;
scanf("%d", &weight3);
printf ("Strategy 4:Procrastination or avoiding the task (leaving your schoolwork to the last minute, or not doing it at all)\n");
printweightscoping;
scanf("%d", &weight4);
printf ("Strategy 5:Denial (ignoring your problems and keep doing things the same way)\n");
printweightscoping;
scanf("%d", &weight5);
int cumweightcoping=weight1+weight2+weight3+weight4;
}

void printweightscoping(void);
{
 printf("1:Never\n");
 printf("2:Rarely(less than weekly) \n");
 printf("3:Sometimes(once a week)\n");
 printf("4:Often (several times a week)\n");
 printf("5:Always (about daily")
}

我以相同的方式使用表单 - 一个用于创建和更新的视图,但在put / post中没有区别。并使用value =“{{old('name',$ item-&gt; name)}}” 点击此处查看:https://github.com/LaraModulus/admin-pages/tree/master/src

答案 3 :(得分:0)

<强>控制器

这样做

$project = Projects::all();

$ project-&gt; isEmpty()将起作用

这样你就可以为模型初始化一个对象,而不是从模型中获取记录。

$project = new Projects;