douwen5584 2018-04-22 03:50
浏览 95
已采纳

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

I am trying to follow this error but I don't know where it is that I need to create purchases. If someone could please help me know how to follow this error I would appreciate it.

Here is my Migration

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

Here is my Model

<?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'));
  }
}

edit: I am trying to push all the above date to a mySQL database

Here is my controller store function

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条回答 默认 最新

  • dongzhi2014 2018-04-22 05:46
    关注

    As we've established in the comments, the error happens because $purchase variable in the controller is an instance of a Purchase query builder. And in your ->addPurchase() {...} method you're calling $this->purchase(), which is a nonexistant method on a query builder.

    Now how to make this work. There's a lot of ways.

    One would be to manually assign all properties to the model and call ->save() afterwards:

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

    Another would be to use mass assignment:

    public function store(Purchase $purchase)
    {
        // ... validation
    
        $purchase->forceCreate(request()->only([
            'fName', 'lName', 'address', 'city', 'state', 'zip', 'card',
        ]));
    
        return back();
    }
    

    Using forceCreate(...), which is same as ->create(...) except that it bypasses the $fillable array, which in this specific instance is OK, since a) we're manually telling it which fields are to be filled, with request()->only([fields]) b) you're performing validation before saving.

    And there are more ways to do this, most of them well documented.

    One last thing I would recommend is to perform validation with (technically) 1 line:

    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...
    }
    

    This way you would get all the errors (if something doesn't pass) in an array, rather than only the first one that didn't pass.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择