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