方法Illuminate \ Database \ Query \ Builder :: purchase不存在

时间:2018-04-22 03:50:21

标签: php laravel query-builder

我正在尝试关注此错误,但我不知道我需要创建购买的位置。如果有人可以帮助我知道如何遵循这个错误,我将不胜感激。

这是我的迁移

public function up()
{
    Schema::create('purchases', function (Blueprint $table) {
      $table->increments('id');
      $table->string('product');
      $table->string('fname');
      $table->string('lname');
      $table->string('address');
      $table->string('city');
      $table->string('state');
      $table->integer('zip');
      $table->string('card');
      $table->timestamps();
    });
}

这是我的模特

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Purchase extends Model
{
  public function addPurchase($body)
  {
    $this->purchases()->create(compact('fName'));
    $this->purchases()->create(compact('lName'));
    $this->purchases()->create(compact('address'));
    $this->purchases()->create(compact('city'));
    $this->purchases()->create(compact('state'));
    $this->purchases()->create(compact('zip'));
    $this->purchases()->create(compact('card'));
  }
}

编辑:我试图将所有上述日期推送到mySQL数据库

这是我的控制器存储功能

public function store(Purchase $purchase)
{
   $this->validate(request(), ['fName' => 'required|min:3']);
   $this->validate(request(), ['lName' => 'required|min:3']);
   $this->validate(request(), ['address' => 'required']);
   $this->validate(request(), ['city' => 'required']);
   $this->validate(request(), ['state' => 'required']);
   $this->validate(request(), ['zip' => 'required']);
   $this->validate(request(), ['card' => 'required']);

   $purchase->addPurchase(request('fName'));
   $purchase->addPurchase(request('lName'));
   $purchase->addPurchase(request('address'));
   $purchase->addPurchase(request('city'));
   $purchase->addPurchase(request('state'));
   $purchase->addPurchase(request('zip'));
   $purchase->addPurchase(request('card'));

   return back();
}

1 个答案:

答案 0 :(得分:3)

正如我们在注释中建立的那样,错误发生是因为控制器中的$purchase变量是Purchase查询构建器的实例。在->addPurchase() {...}方法中,您正在调用$this->purchase(),这是查询构建器上的一种不存在的方法。

现在如何使这项工作。有很多方法。

一种方法是手动将所有属性分配给模型,然后调用->save()

public function store(Purchase $purchase)
{
    // ... validation

    // Assign the properties
    $purchase->fname = request('fName');
    $purchase->lname = request('lName');
    $purchase->address = request('address');
    $purchase->city = request('city');
    $purchase->state = request('state');
    $purchase->zip = request('zip');
    $purchase->card = request('card');
    $purchase->save(); // Save to the database

    return back();
}

另一种方法是使用mass assignment

public function store(Purchase $purchase)
{
    // ... validation

    $purchase->forceCreate(request()->only([
        'fName', 'lName', 'address', 'city', 'state', 'zip', 'card',
    ]));

    return back();
}

使用forceCreate(...),它与->create(...)相同,只是它绕过了$fillable数组,在这个特定实例中是正常的,因为a)我们手动告诉它哪些字段要填写,request()->only([fields]) b)你在保存前进行验证。

还有更多方法可以做到这一点,其中大部分都是well documented

我建议的最后一件事是使用(技术上)1行进行验证:

public function store(Purchase $purchase)
{      
    $this->validate(request(), [
        'fName' => 'required|min:3',
        'lName' => 'required|min:3',
        'address' => 'required',
        'city' => 'required',
        'zip' => 'required',
        'card' => 'required',
    ]);

    // Save the model...
}

这样你就可以得到数组中的所有错误(如果没有通过),而不只是第一个没有通过的错误。