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 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上