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.

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

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料