【发布时间】:2023-03-23 03:10:02
【问题描述】:
我正在尝试跟踪此错误,但我不知道我需要在哪里创建购买。如果有人可以帮助我知道如何遵循此错误,我将不胜感激。
这是我的迁移
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();
}
【问题讨论】:
-
期望的行为是什么?您收到错误是因为在模型
Purchase中您调用的是不存在的$this->purchases()。模型上没有方法purchases。 -
$body 是一个包含所有表单值的数组,对吧?
-
@devk 我认为这就是这条线所做的。 Schema::create('purchases', function (Blueprint $table) { 它说 'purchases' 不是变量吗?
-
不,它说方法
purchases()在查询生成器上不存在。模型上下文中的$this将返回一个查询构建器,除非您实例化一个模型(使用类似Model::first()的东西)。编辑您的问题并解释所需的行为是什么以及$body变量是什么,我们可以为您提供帮助。 -
它在那里定义,但你在其他地方调用它。可能在控制器中。
标签: php laravel query-builder