Field 'vaccine_id' doesn't have a default value - laravel

why i'm getting this error?
First view
` #foreach ($appointments as $appointment)
<div class="card m-2 p-2">
<div class="card-body">
<h6 class="card-title">Vaccine: {{ $appointment->vaccine }}</h6>
<div class="mb-6" style="float: right; margin-top: 10px">
<form action="/appointment/{{ $appointment->id }}">
<input type="submit" class="bg-black text-white rounded py-2 px-4 hover:bg-black"
value="Schedule Now">
</form>
</div>
</div>
</div>
#endforeach`
First view Controller
` public function showAppointment()
{
return view('appointmentForm');
}`
Second View
` <form action="/appointment/create" enctype="multipart/form-data" method="POST">
#csrf
<div class="name">
<label for="name" class="form-label">Name:</label>
<input type="text" name="name" id="name" placeholder="Enter name">
</div>
</form>`
Route
`Route::get('/appointment/{id}', [AppointmentController::class, 'showAppointment'])->middleware('guest');`
` public function showAppointment()
{
return view('appointmentForm');
}`
i have two migration file
` Schema::create('appointments', function (Blueprint $table) {
$table->id();
$table->string('vaccine');
$table->timestamps();
});`
` Schema::create('usersappointments', function (Blueprint $table) {
$table->id();
$table->foreignId('vaccine_id')->constrained('appointments')->onUpdate('cascade')->onDelete('cascade');
$table->string('name');
$table->timestamps();
});`
Model
` protected $fillable = [
'vaccine_id', 'name'
];`
Controller
` public function createAppointment(Request $request)
{
$data = $request->validate([
'name' => 'required'
]);
usersappointments::create($data);
return redirect('/');
}`
This is how it works; the user select vaccines he want then he will redirect to a page which he will input his name but i'm getting this error

You need to set input vaccine_id in second view in order to send data to controller
<form action="/appointment/create" enctype="multipart/form-data" method="POST">
#csrf
<div class="name">
<label for="name" class="form-label">Name:</label>
<input type="hidden" name="vaccine_id" value="" /> // here get the vaccine_id in value which is selected in first view
<input type="text" name="name" id="name" placeholder="Enter name">
</div>
</form>

Related

I set default value on my migration but When I am create and not giving any value it's showing error

I set commission default as 0.00. But when I insert the data but don't give any on commission. it's giving an error that commission can't be null.
*Here is my migration code:*
public function up()
{
Schema::create('pc_lists', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name')->nullable();
$table->decimal('commission')->default('0.00');
$table->tinyInteger('status')->default('1');
});
}
*My blade code is here:*
<div class="card-body">
<form action="" method="POST"
enctype="multipart/form-data">
#csrf
<div class="form-row">
<div class="col">
<label for="commission">Commission (%)</label>
<input type="number" min="0" name="commission"
class="form-control" >
</div>
<div class="form-row">
<div class="col" style="margin-top: 29px !important;">
<input type="submit" class="btn btn-success"
value="Add Personal" >
</div>
</div>
</form>
</div>
You are setting string to integer and decimal don't use '' on default.
public function up()
{
Schema::create('pc_lists', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name')->nullable();
$table->decimal('commission')->default(0.00);
$table->tinyInteger('status')->default(1);
});
}

Create not saving to database laravel

For a school assignment we are to have a site that lets you Create Update Edit and Delete a player and Add a Country.
I am having trouble with my create as it is not saving to the database and returns no error. I have a feeling it is because of my foreign key and I've been looking all over stackoverflow and laravelforums as to how to do this or why it isn't saving to my database.
(As a note all my inputs are text for now until I get it working or get an error i can work with)
Player Model
protected $primaryKey = 'Id';
protected $fillable =['name','age','role','batting','bowling','image','odiRuns','countries_id'];
public function country()
{
return $this->belongsTo('App\Country','countries_id');
}
Store Fuction
public function store(Request $request)
{
//
$player = new Player;
$player->name = $request->name;
$player->age = $request->age;
$player->role = $request->role;
$player->batting = $request->batting;
$player->bowling = $request->bowling;
$player->image = $request->image;
$player->odiRuns = $request->odiRuns;
$player->countries_id = $request->countries_id;
$player->save();
return redirect('index');
}
Form
<form action="{{ route('player.store') }}" method=“post”>
#csrf
<div class="form-group">
<label for="name">Name </label>
<input type="text" class="form-control" name="name" id="name" placeholder="First and Last" >
</div>
<div class="form-group">
<label for="age">Age </label>
<input type="text" class="form-control" name="age" id="age" placeholder="Age" >
</div>
<div class="form-group">
<label for="role">Role </label>
<input type="text" class="form-control" name="role" id="role" placeholder="Role" >
</div>
<div class="form-group">
<label for="batting">Batting </label>
<input type="text" class="form-control" name="batting" id="batting" placeholder="Batting">
</div>
<div class="form-group">
<label for="Bowling">Bowling</label>
<input type="text" class="form-control" name="bowling" id="bowling" placeholder="Bowling">
</div>
<div class="form-group">
<label for="odiRuns"> OdiRuns </label>
<input type="number" class="form-control" name="odiRuns" id="odiRuns" value="odiRuns" placeholder="OdiRuns" required>
</div>
<div class="form-group">
<label for="image">Add Image</label>
<input type="file" name="image" class="form-control-file" id="InputFile" value="image">
</div>
<div class="form-group">
<label for="Country">Country</label>
<input type="text" class="form-control" name="countries_id" id="countries" placeholder="country">
</div>
<button type=“submit” class=“btn btn-primary”>Create</button>
</form>
Player Database
public function up()
{
Schema::create('players', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('age');
$table->string('role');
$table->string('batting');
$table->string('bowling');
$table->string('image');
$table->string('odiRuns');
$table->integer('countries_id')->unsigned();
$table->foreign('countries_id')->references('id')->on('countries');
$table->timestamps();
});
}
Your form is posting a GET request instead of POST request
It's a bit difficult to notice but method=“post” should be method="post"
double quotes instead of that MS word weird character
Specify that your form can post files such as images like so
<form action="{{ route('player.store') }}" method="post" enctype="multipart/form-data">
Otherwise it won't post the image and it's not nullable in your migration
change:
$player = new Player();
and why you don't use 'select' for countries_id
like :
<select name="countries_id">
<option value=""></option>
</select>
method="post"

Contact form not doing anything in laravel 5.7 even not showing any error

This was my contact form and here is the form action
<form action="{{ route('contact.send')}}" method="post" class="contactForm">
<div id="sendmessage">Your message has been sent. Thank you!</div>
<div id="errormessage"></div>
<div class="row">
<div class="span4 form-group">
<input type="text" name="name" id="name" placeholder="Your Name"data-rule="required" data-msg="Please enter at least 4 chars" />
<div class="validation"></div>
</div>
<div class="span4 form-group">
<input type="email" name="email" id="email" placeholder="Your Email" data-rule="required" data-msg="Please enter a valid email" />
<div class="validation"></div>
</div>
<div class="span4 form-group">
<input type="text" name="phone" id="phone" placeholder="Your Phone"data-rule="required" data-msg="Please enter at least 4 chars" />
<div class="validation"></div>
</div>
<div class="span6 form-group">
<input type="text" name="subject" id="subject" placeholder="Subject" data-rule="required" data-msg="Please enter at least 8 chars of subject" />
<div class="validation"></div>
</div>
<div class="span6 form-group">
<input type="text" name="dateandtime" id="dateandtime" placeholder="Date and Time" data-rule="required" data-msg="Please Select a EST time and Date." />
<div class="validation"></div>
</div>
<div class="span12 margintop10 form-group">
<textarea name="message" rows="12" data-rule="required" data-msg="Please write something for us" placeholder="Message"></textarea>
<div class="validation"></div>
<p class="text-center">
<button class="btn btn-large btn-theme margintop10" type="submit">Submit message</button>
</p>
</div>
</div>
</form>
Bellow is the route and controller
Route::post('/contact','ContactController#sendMessage')->name('contact.send');
and the controller was
public function sendMessage(Request $request)
{
$this->validate($request,[
'name'=>'required',
'phone'=>'required',
'email'=>'required|email',
'subject'=>'required',
'messase'=>'required'
]);
$contact =new \App\contact();
$contact->name = $request->name;
$contact->email = $request->email;
$contact->phone = $request->phone;
$contact->subject = $request->subject;
$contact->date_and_time = $request->dateandtime;
$contact->message = $request->message;
$contact->status = false;
$contact->save();
Toastr::success('You Message Sent Successfully We will contact you soon!','Success',["positionClass" => "toast-top-center"]);
return redirect()->back();
}
And then the migration was as bellow
public function up()
{
Schema::create('contacts', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('phone');
$table->string('email');
$table->string('date_and_time');
$table->text('message');
$table->boolean('status');
$table->timestamps();
});
}
But when I clicking on my submit button after filling up the form that not showing any error or not doing anything. Please help me to solve this problem.
If you are using laravel 5, here is what you need to do for showing success message:
In your controller file:
return redirect()->back()->with('success', 'your message,here');
In your blade file:
#if (\Session::has('success'))
<div class="alert alert-success">
<ul>
<li>{!! \Session::get('success') !!}</li>
</ul>
</div>
#endif
If you want to show error message than in your controller:
return Redirect::back()->withErrors(['msg', 'The Message']);
In your blade file:
#if($errors->any())
<h4>{{$errors->first()}}</h4>
#endif

Laravel : no errors and no update on database happens after saving the edit

I have two tables, user and technicien, with a one to one relation. After editing technicien information through edit form and saving, no update happens on database and no errors as well.
Here is my code:
controllers
public function edit($id)
{
$technicien=technicien::find($id);
$user = $technicien->user;
return view('technicien.edit',['technicien'=>$technicien])->with('user',$user);
}
public function update(Request $request, $id)
{
// do some request validation
$technicien=technicien::find($id);
$technicien->update($request->all());
$technicien->user->update($request->get('user'));
$user->nom = $request->update('nom');
return redirect('/technicien');
}
View
#extends('Layouts/app')
#extends('Layouts.master')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-10">
<h1>Modifier Technicien</h1>
<form action="{{ route('technicien.update', $technicien->technicien ) }}" method="update">
{{csrf_field()}}
{{ method_field('PATCH') }}
<div class="form-group">
<label for="nom">Nom</label>
<input id="nom" type="text" class="form-control" name="user[nom]" value="{{$user->nom}}" >
</div>
<div class="form-group">
<label for="prenom">Prenom</label>
<input id="prenom" type="text" class="form-control" name="user[prenom]" value="{{$user->prenom}}" >
</div>
<div class="form-group">
<label for="prenom">Email</label>
<input id="prenom" type="text" class="form-control" name="user[email]" value="{{$user->email}}" >
</div>
<div class="form-group">
<label for="">moyenne Avis</label>
<input type="text" name="moyenne_avis" class="form-control" value ="{{$technicien->moyenne_avis}}" >
</div>
<div class="form-group">
<label for="">Etat Technicien</label>
<input type="text" name="actif" class="form-control" value ="{{$technicien->actif}}" >
</div>
<div class="form-group">
<input type="submit" value="enregistrer" class="form-control btn btn-primary">
</div>
</div>
</form>
</div>
</div>
#endsection
route.php
Route::get('/technicien/{id}/edit', 'TechnicienController#edit');
Route::patch('/technicien/{id}', 'TechnicienController#update')-
>name('technicien.update');
You just need to pass parameters to update function.
Read docs
public function update(Request $request, $id)
{
$technicien=technicien::find($id);
$technicien->update($request->all());
$technicien->user->update([
'nom' => $request->nom,
'premon' => $request->premon,
'email' => $request->email
]);
return redirect('/technicien');
}
Also from the docs
You should define which model attributes you want
to make mass assignable. You may do this using the $fillable property
on the model.

Update on "user" and "technicien" with one to one connection

I have two tables user and technician with one to one connection, technician inherits from user. After editing technician information through edit form and saving no update happens on tables user and technician. and no errors as well.
Here is my code:
Controllers
public function edit($id)
{
$technicien=technicien::find($id);
$user = $technicien->user;
return view('technicien.edit',['technicien'=>$technicien])->with('user',$user);
}
public function update(Request $request, $id)
{
// do some request validation
$technicien=technicien::find($id);
$technicien->update($request->all());
$technicien->user->update($request->get('user'));
$user->nom = $request->update('nom');
return redirect('/technicien');
}
View
#extends('Layouts/app')
#extends('Layouts.master')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-10">
<h1>Modifier Technicien</h1>
<form action="{{ route('technicien.update', $technicien->technicien ) }}" method="update">
{{csrf_field()}}
{{ method_field('PATCH') }}
<div class="form-group">
<label for="nom">Nom</label>
<input id="nom" type="text" class="form-control" name="user[nom]" value="{{$user->nom}}" >
</div>
<div class="form-group">
<label for="prenom">Prenom</label>
<input id="prenom" type="text" class="form-control" name="user[prenom]" value="{{$user->prenom}}" >
</div>
<div class="form-group">
<label for="prenom">Email</label>
<input id="prenom" type="text" class="form- control" name="user[email]" value="{{$user->email}}" >
</div>
<div class="form-group">
<label for="">moyenne Avis</label>
<input type="text" name="moyenne_avis" class="form-control" value ="{{$technicien->moyenne_avis}}" >
</div>
<div class="form-group">
<label for="">Etat Technicien</label>
<input type="text" name="actif" class="form-control" value ="{{$technicien->actif}}" >
</div>
<div class="form-group">
<input type="submit" value="enregistrer" class="form-control btn btn-primary">
</div>
</div>
</form>
</div>
</div>
#endsection
Route.php
Route::get('/technicien/{id}/edit', 'TechnicienController#edit');
Route::patch('/technicien/{id}', 'TechnicienController#update')->name('technicien.update');
Model1
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class technicien extends Model
{
protected $fillable = [
'moyenne_avis', 'actif',
];
use SoftDeletes;
protected $guarded = [];
protected $dates = ['deleted_at'];
public function zoneintervention()
{
return $this->belongsToMany('App\zoneintervention','technicien_zone','technicien_id','zoneintervention_id');
}
public function metier()
{
return $this->belongsToMany('App\metier','technicien_metier','technicien_id','metier_id');
}
public function user()
{
return $this->belongsTo('App\User');
}
public function tarificationtache()
{
return $this->belongsToMany('App\tarificationtache','technicien_tarificationtache','technicien_id','tarificationtache_id');
}
}
Model2
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
public function technicien()
{
return $this->hasOne('App\technicien');
}
use Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'email', 'password','nom','prenom','tel','mobil','role',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
controler
public function edit($id)
{
// better to use findOrFail (it will throw an exception about missing
objects)
$technicien = technicien::findOrFail($id);
return view('technicien.edit', compact('technicien'));
}
public function update(Request $request, $id)
{
$technicien=technicien::findOrFail($id);
$technicien->user->update($request->get('user'));
$technicien->update($request->get('technicien'));
return redirect('/technicien');
}
and the view
#extends('Layouts/app')
#extends('Layouts.master')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-10">
<h1>Modifier Technicien</h1>
<form action="{{ route('technicien.update', $technicien ) }}"
method="post">
{{csrf_field()}}
{{ method_field('patch') }}
<div class="form-group">
<label for="nom">Nom</label>
<input id="nom" type="text" class="form-control"
name="user[nom]" value="{{$technicien->user->nom}}" >
</div>
<div class="form-group">
<label for="prenom">Prenom</label>
<input id="prenom" type="text" class="form-control"
name="user[prenom]" value="{{$technicien->user->prenom}}" >
</div>
<div class="form-group">
<label for="prenom">Prenom</label>
<input id="prenom" type="text" class="form-control"
name="user[tel]" value="{{$technicien->user->tel}}" >
</div>
<div class="form-group">
<label for="prenom">Prenom</label>
<input id="prenom" type="text" class="form-control"
name="user[mobil]" value="{{$technicien->user->mobil}}" >
</div>
<div class="form-group">
<label for="prenom">Prenom</label>
<input id="prenom" type="text" class="form-control"
name="user[role]" value="{{$technicien->user->role}}" >
</div>
<div class="form-group">
<label for="prenom">Email</label>
<input id="prenom" type="text" class="form-control"
name="user[email]" value="{{$technicien->user->email}}" >
</div>
<div class="form-group">
<label for="prenom">Email</label>
<input id="prenom" type="text" class="form-control"
name="user[password]" value="{{$technicien->user->password}}" >
</div>
<div class="form-group">
<label for="">moyenne Avis</label>
<input type="text" name="technicien[moyenne_avis]"
class="form-control" value="{{$technicien->moyenne_avis}}" >
</div>
<div class="form-group">
<label for="">Etat Technicien</label>
<input type="text" name="technicien[actif]"
class="form-control" value="{{$technicien->actif}}" >
</div>
<div class="form-group">
<input type="submit" value="enregistrer" class="form-
control btn btn-primary">
</div>
</form>
</div>
</div>
</div>
#endsection
Try to do with One to One relation.
Make relation for user table and technician table, then try to do update.
Try this in controller
$user = User::with('technicien')->find($id);
$data['id'] = $id;
$data = $this->validate($request, [
'moyenne_avis' => 'required',
]);
$user->technicien()->whereUserId($data['id'])->update(['moyenne_avis' => $data['moyenne_avis']
]);
return redirect('/technicien')->with('Success', 'Records updated');
Also change form method as below in ur view.blade
<form action="{{ action('TechnicienController#update', $user->id) }}" method="post">
also, instead of {{ method_field('PATCH') }} use this <input name="_method" type="hidden" value="PATCH">
Note: Table name should be plural for controller and model name.
eg: Table name: users
Controller name: UserController
Model name: User
Make sure this same in urs too.

Resources