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.
Related
This is a continuation of my last question.
I like to create a relationship between a user (with an account type that’s equal to a “profile”) and my job posts. What I did was create a relationship like this in my models (not sure if correct tho)
User.php
public function jobposts()
{
$this->hasMany(JobPost::class)->where('account_type', 'profile');
}
JobPost.php
public function userprofile()
{
$this->belongsTo(User::class)->where('account_type', 'profile');
}
JobPostController.php
public function store(Request $request)
{
$this->validate($request, [
'job_name' => 'required|max:100',
'describe_work' => 'required|max:800',
'job_category' => 'required|not_in:0',
'city' => 'required|not_in:0',
'state' => 'required|not_in:0',
'zip' => 'required|regex:/\b\d{5}\b/',
]);
dd(auth()->user()->jobpost()->job_name);
}
2021_11_20_211922_create_job_posts_table.php
Schema::create('job_posts', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->contrained()->onDelete('cascade');
$table->string('job_name');
$table->text('describe_work');
$table->string('job_category');
$table->timestamps();
});
Got 2 questions about what I can do in the JobPostController.php.
How do I dd() to test the output?
This seems wrong
dd(auth()->user()->jobpost()->job_name);
How do I add it correctly into the DB like this?
public function store(Request $request)
{
$request->user()
->jobpost()
->create([
'job_name' => $request->job_name
]);
}
I am using Laravel 8 and trying to get an application form to post to two tables in my database
From my 2 database migration files:
public function up() {
Schema::create('applicants', function (Blueprint $table) {
$table->id();
$table->string('apptitle');
$table->string('firstname');
$table->string('middlename')->nullable();
...
$table->timestamps();
});
}
public function up() {
Schema::create('applications', function (Blueprint $table) {
$table->id();
$table->integer('applicant_id');
$table->integer('user_id');
$table->integer('loanAmount');
$table->string('loanTerm');
...
$table->timestamps();
});
}
Models:
class Applicant extends Model {
use HasFactory;
protected $table = 'applicants';
protected $fillable = [
'apptitle', 'firstname', 'middlename'...
];
public function application() {
return $this->hasOne(Application::class);
}
}
class Application extends Model {
use HasFactory;
protected $table = 'applications';
protected $fillable = [
'applicant_id',
'user_id',
'loanAmount',
'loanTerm',
...
];
public function applicant() {
return $this->belongsTo(Applicant::class);
}
}
Controllers:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\Applicants\CreateApplicantRequest;
class ApplicantsController extends Controller {
...
public function store(CreateApplicantRequest $request) {
$applicant = Applicant::create([
'apptitle' => $request->apptitle,
'firstname' => $request->firstname,
'middlename' => $request->middlename,
...
]);
}
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Application;
use App\Models\Applicant;
use App\Models\User;
use App\Http\Requests\Applications\CreateApplicationRequest;
class ApplicationsController extends Controller {
...
public function store(CreateApplicationRequest $request) {
$application = Application::create([
'applicant_id' => $request->applicant_id,
'user_id' => 'required',
'loanAmount' => 'required',
'loanTerm' => 'required',
...
]);
}
}
Requests:
public function rules() {
return [
'apptitle' => 'required',
'firstname' => 'required',
'middlename',
...
];
}
public function rules() {
return [
'applicant_id' => 'required',
'user_id' => 'required',
'loanAmount' => 'required',
'loanTerm' => 'required',
...
];
}
web.php
Route::get('applicants','ApplicantsController#store');
Route::resource('applications', 'ApplicationsController');
Route::get('applications/{application}', 'ApplicationsController#show');
I am continually getting errors: The applicant id field is required. (If I make this field nullable the form does successfully post all other fields to the database.)
This is my first big Laravel project so any help would be greatly appreciated.
Update:
I have gone through the answers provided and am still getting the same error.
I feel the main issue is - when the form is filled out the applicant_id field for the newly created Applicant is not being captured and added to the applications table?
You can store data from one form into 2 tables like this.
Remove use App\Http\Requests\Applicants\CreateApplicantRequest; from your ApplicationsController and run the following cmd commands:
composer dump-autoload
php artisan cache:clear
php artisan config:clear
php artisan view:clear
php artisan route:clear
These commands clear all cache from your project.
Add nullable to your application migration applicant_id:
$table->integer('applicant_id')->nullable();
I finally was able to get my form posting correctly to both databases - a big thank you to all those that have helped me in this journey.
This is my updated store function in my ApplicationsController:
public function store(CreateApplicationRequest $request, Applicant $applicant)
{
$applicant = Applicant::create([
'apptitle' => $request->apptitle,
'firstname' => $request->firstname,
'middlename' => $request->middlename,
...
]);
$application = $applicant->application()->create([
'applicant_id' => $applicant->id,
'user_id' => auth()->id(),
'loanAmount' => $request->loanAmount,
'loanTerm' => $request->loanTerm,
...
]);
// redirect the user
return redirect(route('applications.index'));
}
I hope this answer helps someone else out!
I'm working on a larvel project where the user can create appointments. In addition I've created another model called clients so when a user creates an appointment the users "client" data is saved.
In my appointments controller I have the following: -
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required',
]);
//create appointment
$apt = new Appointment;
$apt->name = $request->input('name');
$apt->user_id = auth()->user()->id;
$apt->save();
//create client
$client = new Client;
$client->first_name = $request->input('name');
$client->user_id = auth()->user()->id;
$client->save();
return redirect('/appointments')->with('success', 'Appointment created');
}
When saving the data it works and stores the data in the clients table however I know this isn't the cleanest way of saving the data, but what is the "laravel" way of doing this?
There's nothing wrong with your code. It's totally fine keeping it that way.
I prefer to say Model::create() to create models in one statement.
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required',
]);
Appointment::create([
'name' => request('name'),
'user_id' => auth()->id()
]);
Client::create([
'first_name' => request('name'),
'user_id' => auth()->id,
]);
return redirect('/appointments')->with('success', 'Appointment created');
}
You can also use tap() function:
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required',
]);
tap(Appointment::create(['name' => request('name'), 'user_id' => auth()->id()]), function ($newAppoinment) {
Client::create([
'first_name' => $newAppoinment->name,
'user_id' => auth()->id,
]);
});
return redirect('/appointments')->with('success', 'Appointment created');
}
Or the best approach could be using model events:
class Appointment extends Model
{
public static function boot()
{
parent::boot();
static::created(function ($appointment) {
Client::create([
'user_id' => $appoinment->user_id,
'first_name' => $appoinment->name,
])
});
}
}
// in the validation section "alias" field should be unique so i need this NursingHome object id(primary key) to force validation to not to check for this id.
I have checked it with $nursinghome->getKey() method but no success.
public function update(Request $request, NursingHome $nursinghome)
{
$request->validate([
'name' => 'required|string|max:255',
'address' => 'nullable|string',
'alias' => 'required|string|unique:nursing_home,'.$nursinghome->id,
]);
$data = $request->all();
$data['updated_by'] = Auth::guard('api')->id();
$nursinghome->update($data);
return response()->json($nursinghome, 200);
}
There is a know issue disscussed in laravel github, that if your model has two words like NursingHome the it is not injected in controller:
public function update(Request $request, $id){
$nursinghome = NursingHome::find($id); //now you will get $nursinghome->id
$request->validate([
'name' => 'required|string|max:255',
'address' => 'nullable|string',
'alias' => 'required|string|unique:nursing_home,'.$nursinghome->id,
]);
$data = $request->all();
$data['updated_by'] = Auth::guard('api')->id();
$nursinghome->update($data);
return response()->json($nursinghome, 200);
}
If your model having two or more words, you have to use only small letters.
I am working on a laravel project with user login. The admin can create new users and edit existing users. I have got a password and a passwordConfirm field in the update-user-form. If the admin puts a new password in the form, it should check for validation and update the record in the db. If not, it shouldn't change the password (keep old one), but update the other user data (like the firstname).
If I try to send the form with an empty password and passwordConfirm field, it doesn't validate. I got a validation error, that the password must be a string and at least 6 characters long, but I don't know why. It seems like the first line of my update function will be ignored.
UserController.php
public function update(User $user, UserRequest $request) {
$data = $request->has('password') ? $request->all() : $request->except(['password', 'passwordConfirm']);
$user->update($data);
return redirect('/users');
}
UserRequest.php
public function rules() {
return [
'firstname' => 'required|string|max:255',
'lastname' => 'required|string|max:255',
'email' => 'required|string|email|max:255',
'password' => 'string|min:6',
'passwordConfirm' => 'required_with:password|same:password',
];
}
If you want to validate a field only when it is present then use sometimes validation rule in such cases.
Add sometimes validation to both password & passwordConfirm. Remove the $data line from update();
// UserController.php
public function update(User $user, UserRequest $request) {
$user->update($request->all());
return redirect('/users');
}
// UserRequest.php
public function rules() {
return [
'firstname' => 'required|string|max:255',
'lastname' => 'required|string|max:255',
'email' => 'required|string|email|max:255',
'password' => 'sometimes|required|string|min:6',
'passwordConfirm' => 'sometimes|required_with:password|same:password',
];
}
Reference - https://laravel.com/docs/5.4/validation#conditionally-adding-rules
I always do this in my projects:
//Your UserController file
public function update(User $user, UserRequest $request) {
$user->update($request->all());
return redirect('/users');
}
//Your UserRequest file
public function rules() {
$rules= [
'firstname' => 'required|string|max:255',
'lastname' => 'required|string|max:255',
'email' => 'required|string|email|max:255'
];
if($this->method()=="POST"){
$rules['password']='sometimes|required|string|min:6';
$rules['passwordConfirm']='sometimes|required_with:password|same:password';
}
return $rules;
}
So, as you can see if your method is POST it means that you want to add a new user so its going to ask for password and passwordConfirm but if your method is PATCH or PUT it means you don't need to validate password and passwordConfirm.
Hope it helps
Maybe you should try the following:
// ... more code
// Removes password field if it's null
if (!$request->password) {
unset($request['password']);
}
$request->validate([
// ... other fields,
'password' => 'sometimes|min:6'
// ... other fields,
]);
// ... more code
you should replace "has" with "filled" in your code
$data = $request->filled('password') ? $request->all() : $request->except(['password', 'passwordConfirm']);
and actually it's better if you use the expression like this
$request->request->remove('password_confirmation');
( ! $request->filled('password') ) ? $request->request->remove('password'):"";
( $request->has('password') ) ? $request->merge([ 'password' => Hash::make($request->post()['password']) ]):"";
//then you can use
$user->update($request->all());
Even better, however, you have to use separate request classes for create and update "php artisan make:request" for ex:
UserUpdateRequest.php and UserCreateRequest.php
for UserCreateRequest your rule is
'password' => 'required|confirmed|min:6',
for UserUpdateRequest your rule is
'password' => 'sometimes|nullable|confirmed|min:6',
and your controller head add this line
use App\Http\Requests\UserCreateRequest;
use App\Http\Requests\UserUpdateRequest;
and your update method must change
public function update(UserUpdateRequest $request, $id)
{
//
}
Standard way of doing this
UserRequest.php
first import Rule
use Illuminate\Validation\Rule;
in your rules array:
'password' => [Rule::requiredIf(fn () => $this->route()->method == "POST")]
Example:
public function rules()
{
return [
'name' => 'required',
'email' => ['required', 'email'],
'password' => [Rule::requiredIf(fn () => $this->route()->method == "POST"), 'confirmed'],
];
}
below php 7.4 use this way
'password' => [Rule::requiredIf(function(){
return $this->route()->method == "POST";
})]