I'm having an issue with my contact form. All the fields are required except for one field. Normally I would in migration insert nullable, but apparently, it doesn't work. I have tried to make a nullable in validation, but this doesn't work either. So I'm a bit confused.
public function up()
{
Schema::create('kontaktforms', function (Blueprint $table) {
$table->increments('id');
$table->string('navn');
$table->string('mobilnr');
$table->string('fastnetnr')->nullable();
$table->string('mail');
$table->string('emne');
$table->text('beskrivelse');
$table->timestamps();
});
}
public function store(Request $request)
{
$this->validate($request, [
'navn' => 'required',
'mobil' => 'required',
'email' => 'required',
'emne' => 'required',
'beskrivelse' => 'required'
]);
$kontakt = new Kontaktform([
'navn' => $request['navn'],
'mobilnr' => $request['mobil'],
'fastnetnr' => $request['fastnetnr'],
'mail' => $request['email'],
'emne' => $request['emne'],
'beskrivelse' => $request['beskrivelse']
]);
$kontakt->save();
Session::flash('success', 'Vi har nu modtaget din besked');
return redirect()->route('kontakt.create');
}
Form
<form id="form-contact" action="{{route('kontakt.store')}}" method="POST">
#csrf
<h1 class="display-4">Kontakt os</h1>
<div class="form-group">
<input name="navn" type="text" class="form-control" placeholder="Dit navn...">
</div>
<div class="form-group">
<input name="mobil" type="text" class="form-control" placeholder="Din mobil">
</div>
<div class="form-group">
<input name="fastnetnr" type="text" class="form-control" placeholder="Evt fastnetnr">
</div>
<div class="form-group">
<input name="email" type="email" class="form-control" placeholder="Din email">
</div>
<div class="form-group">
<input name="emne" type="text" class="form-control" placeholder="Emne">
</div>
<div class="form-group">
<textarea name="beskrivelse" class="form-control" placeholder="Skriv din besked her" rows="4"></textarea>
</div>
<br>
<input type="submit" class="btn btn-primary btn-block" value="Send">
<hr>
</form>
Do migration for nullable field as
$table->string('fieldname')->nullable();
and during validation either by using Validator or FormRequest confirm that you haven't added a required attribute
'fieldname' => 'required|integer'
you must have only
'fieldname' => 'integer'
I am not sure what are you trying to do but the table kontaktforms does not have any field called fastnetnr which you are trying to enter from your controller.
Maybe add the field in the migration, run migration again after rolling back and then try?
Related
I tried to store data but data not store to database, the field in database and form input already match but still can't store data, and there is no actual message error. please help.
this is my controller:
public function store(Request $request)
{
$validatedData = $request->validate([
'kabupaten' => ['required'],
'provinsi' => ['required'],
'unit' => ['required'],
'satuan_kerja' => ['required'],
'nama_area' => ['required'],
'kode_area' => ['required']
]);
Area::create($validatedData);
return redirect('/dashboard/areas')->with('success','Area baru telah ditambahkan!');
}
this is the form input:
<form action="/dashboard/areas" method="POST">
#csrf
<div class="mb-3">
<label for="provinsi" class="form-label">Provinsi</label>
<input type="text" class="form-control" id="provinsi" name="provinsi" value="Jawa Tengah">
</div>
<div class="mb-3">
<label for="kabupaten" class="form-label">Kabupaten</label>
<input type="text" class="form-control" id="kabupaten" name="kabupaten" value="Brebes">
</div>
<div class="mb-3">
<label for="unit" class="form-label">Unit</label>
<input type="text" class="form-control" id="unit" name="unit" value="Pemerintah Kabupaten Brebes">
</div>
<div class="mb-3">
<label for="satuan_kerja" class="form-label">Satuan Kerja</label>
<input type="text" class="form-control" id="satuan_kerja" name="satuan_kerja" value="Pemerintah Desa Dumeling">
</div>
<div class="mb-3">
<label for="nama_area" class="form-label">Nama Area</label>
<input type="text" class="form-control" id="nama_area" name="nama_area">
</div>
<div class="mb-3">
<label for="kode_lokasi" class="form-label">Kode Lokasi</label>
<input type="text" class="form-control" id="kode_lokasi" name="kode_lokasi">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
And this is my area model:
class Area extends Model
{
use HasFactory;
protected $primaryKey = 'id_area';
protected $guarded = [
'id_area'
];
public function aset(){
return $this->hasMany(Aset::class, 'id_area');
}
}
Thank you if there anyone can help me with this problem, I really appreciate it.
So most likely your validation is failing, what you need to do is to display the results of the failed validation error messages, and you can do so in your blade file:
#if ($errors->any())
#foreach ($errors->all() as $error)
<div>{{$error}}</div>
#endforeach
#endif
You may read more on how to display the errors here: https://laravel.com/docs/9.x/validation#quick-displaying-the-validation-errors
You can as well display it per input field or change the class of the input method, etc.. check the #error directive from here: https://laravel.com/docs/9.x/validation#the-at-error-directive
when i try to update my the section name i am having this error: The PUT method is not supported for this route. Supported methods: GET, HEAD.
there is the form:
<form action="sections.update" method="POST" autocomplete="off">
#csrf
#method('PUT')
<div class="form-group">
<input type="hidden" name="id" id="id" value="">
<label for="recipient-name" class="col-form-label">section name</label>
<input class="form-control" name="section_name" id="section_name" type="text">
</div>
<div class="form-group">
<label for="message-text" class="col-form-label">description</label>
<textarea class="form-control" id="description" name="description"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">confirm</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">close</button>
</div>
</form>
and that is the update controller
public function update(Request $request)
{
$id = $request->id;
$this->validate($request, [
'section_name' => 'required|max:255|unique:sections,section_name,' . $id,
'description' => 'required',
], [
'section_name.required' => 'section name is required',
'section_name.unique' => 'section_name should be unique',
'description.required' => 'description is required',
]);
$sections = sections::find($id);
$sections->update([
'section_name' => $request->section_name,
'description' => $request->description,
]);
session()->flash('edit', 'the section is edited successfully');
return redirect('/sections');
}
you have to pass the id/parameter in your form action when using put method but u re fetching id from form , so you can pass some dummy data on it. Change the form to
<form action="action="{{ route('sections.update', ['section' => '1']) }}" method="POST" autocomplete="off">
I am working through creating a CRUD app in Laravel, I am using the default created_at and updated_at columns that seem to populate automatically when I insert or update to the database. When changing from using the edited entry's ID to pulling in the actual model entry as a parameter in my update method, I stumbled upon the exception "InvalidArgumentException
A four digit year could not be found Data missing" when calling $installer->update(['my field 1', ...]);
I have tried dd'ing my request and the $installer object that is pulled in and everything looks correct, but updating this way seems to break the update on the updated_at field. Below are the relevant snippets:
My model class:
class Installer extends Model
{
protected $guarded = [];
}
My update method:
public function update(Installer $installer) {
request()->validate([
'FirstName' => 'required',
'LastName' => 'required',
'Position' => 'required',
'Status' => 'required',
'EmpId' => 'required'
]);
$installer->update(['FirstName', 'LastName', 'Position', 'Status', 'EmpId']);
return redirect('/installers');
}
The relevant part of my edit form:
<form method="POST" action="/installers/{{ $installer->id }}" style="margin-bottom: 1em">
#method('PATCH')
#csrf
<div class="field">
<label class="label" for="FirstName">First Name</label>
<div class="control">
<input type="text" class="input" name="FirstName" placeholder="First Name" value="{{ $installer->FirstName }}" required>
</div>
</div>
<div class="field">
<label class="label" for="LastName">Last Name</label>
<div class="control">
<input type="text" class="input" name="LastName" placeholder="Last Name" value="{{ $installer->LastName }}" required>
</div>
</div>
<div class="field">
<label class="label" for=Position">Position</label>
<div class="control">
<input type="text" class="input" name="Position" placeholder="Position" value="{{ $installer->Position }}" required>
</div>
</div>
<div class="field">
<label class="label" for="Status">Status</label>
<div class="control">
<input type="text" class="input" name="Status" placeholder="Status" value="{{ $installer->Status }}" required>
</div>
</div>
<div class="field">
<label class="label" for="EmpId">Employee ID</label>
<div class="control">
<input type="text" class="input" name="EmpId" placeholder="Employee ID" value="{{ $installer->EmpId }}" required>
</div>
</div>
<div class="field">
<div class="control">
<button type="submit" class="button is-link">Update Installer</button>
</div>
</div>
</form>
How do I modify this so that eloquent will continue to update the updated_at field automatically?
Your update method is wrong
One easy way is try something like below:
public function update(Installer $installer) {
$validatedData = request()->validate([
'FirstName' => 'required',
'LastName' => 'required',
'Position' => 'required',
'Status' => 'required',
'EmpId' => 'required'
]);
$installer->update($validatedData); //or $installer->update([$validatedData]);
return redirect('/installers');
}
hope this is helpful
You are not including an array to the update method
Must be something like this:
$installer->update([
'FirstName' => $request->FirstName,
'LastName' => $lastNameVar,
'ColName' => $colValue,
...
]);
Where $request is the data from the
So I just made a syntax error but I figured I would leave this up if anyone has a similar issue. In my update method:
$installer->update(['FirstName', 'LastName', 'Position', 'Status', 'EmpId']);
should read:
$installer->update(request(['FirstName', 'LastName', 'Position', 'Status', 'EmpId']));
i have this validation for registering new users but every time I submit it just reloads and stays in the page, I am lost, but when I use the Registration::create($request->all()) without validation from the top it pushes through and saves the data. please help
my controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Registration;
class RegistrationsController extends Controller
{
public function index(){
return view('registrations.create');
}
public function store(){
request()->validate([
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
]);
$validatedUser = Registration::create([
'name' => request('name'),
'email' => request('email'),
'password' => bcrypt(request('password'))
]);
return redirect()->home();
}
}
here is my create.blade
<form action="{{ route('registrations.store') }}" method="POST">
{{ csrf_field() }}
<div class="form-group">
<label for="">Full Name</label>
<input type="text" class="form-control" name="name" id="name" aria-describedby="helpId" placeholder="Juan Dela Cruz">
<small id="helpId" class="form-text text-muted">Ex. Juan Dela Cruz</small>
</div>
<div class="form-group">
<label for="">Password</label>
<input type="password" class="form-control" name="password" id="password" placeholder="type password here..">
</div>
<div class="form-group">
<label for="">Confirm password</label>
<input type="password" class="form-control" name="password_confirm" id="password_confirm" placeholder="type password here..">
</div>
<div class="form-group">
<label for="">Email address</label>
<input type="email" class="form-control" name="email" id="email" aria-describedby="emailHelpId" placeholder="juandelacruz#gmail.com">
<small id="emailHelpId" class="form-text text-muted">Must be valid email address</small>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
for my route i just used Route::resource
thank you
This is probably because your password confirmation fails.
Your confirmation field must be named : password_confirmation and not password_confirm as the documentation say :
The field under validation must have a matching field of foo_confirmation. For example, if the field under validation is password, a matching password_confirmation field must be present in the input.
In my project that I'm working on I have a table of users which i have another table that contains project assigned to them. I'm Using this code:
<form method="POST" action="/User/{{$user->id}}/projects">
{{csrf_field()}}
<div class="form-group">
<textarea name="name"></textarea>
</div>
<div class="form-group">
<textarea name="description" placeholder="Enter project description here"></textarea>
</div>
<div class="form-group">
<button type="submit">Add Project for User</button>
</div>
And in Controller I'm Using this Code:
public function store(User $user)
{
Project::create([
'user_id ' => $user->id,
'name' => request('name'),
'description' => request('description'),
]);
return back();
}
And project table has this Code:
public function up()
{
Schema::create('projects', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->string('name');
$table->string('description');
$table->timestamps();
});
}
Now when I'm adding project for user But I get this error:
"SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into `projects` (`name`, `description`, `updated_at`, `created_at ▶"
How I can fixed this?
If the POST for /User/{{$user->id}}/projects is routing to store(User $user) I think you need to update it to store($user_id).
public function store($user_id)
{
Project::create([
'user_id ' => $user_id,
'name' => request('name'),
'description' => request('description'),
]);
return back();
}
The route is just passing the current user's ID, not the User model.
I'm Editing my view to Offers of friends in stack,such as
<form method="POST" action="/User/{{$user->id}}/projects">
{{csrf_field()}}
<input type="hidden" name="user_id" value="{{$user->id}}">
<div class="form-group">
<textarea name="name" placeholder="Enter project description here"></textarea>
</div>
<!--<div class="form-group">
<textarea name="description" placeholder="Enter project description here"></textarea>
</div>-->
<div class="form-group">
<button type="submit">Add Project for User</button>
</div>
And Edit my controller such as this code
public function store(User $user)
{
Project::create(request(['name', 'user_id']));
return back();
}
That error happened because the $user->id value in the store method is not set. If the user_id is fillable then using hidden input is an option.
<form method="POST" action="/User/{{$user->id}}/projects">
{{csrf_field()}}
<input type="hidden" name="user_id" value="{{$user->id}}">
<div class="form-group">
<textarea name="name"></textarea>
</div>
<div class="form-group">
<textarea name="description" placeholder="Enter project description here"></textarea>
</div>
<div class="form-group">
<button type="submit">Add Project for User</button>
</div>
</form>
and then in the controller :
public function store()
{
Project::create([
'user_id ' => request('user_id'),
'name' => request('name'),
'description' => request('description'),
]);
return back();
}