POST request is not tallied with the form in Laravel Blade - php

I am a newbie in Laravel.
My problem is, when I tried to update my form, it kept saying that the tablename not found eventhough I already mentioned it inside my Model. But when I debugged, I found that the request is not the same from what I put inside my form.
But my form actually doesn't have that.
Any idea how this is happening, guys? Pretty sure I missed something but unsure what is it.
Event Model
class Event extends Model
{
//
protected $fillable = ['title', 'objective', 'date', 'venue', 'description', 'slug'];
public function getRouteKeyName()
{
return 'slug';
}
}
Event Controller
class EventController extends Controller
{
public function update(Request $request, Event $event)
{
//
$validated = $request->validate([
'title' => 'required|string|unique:event|min:5|max:100',
'objective' => 'required|string|min:5|max:2000',
'date' => 'required|string|min:5|max:2000',
'venue' => 'required|string|min:5|max:2000',
'description' => 'required|string|min:5|max:2000'
]);
// Create slug from title
$validated['slug'] = Str::slug($validated['title'], '-');
// Update Post with validated data
$event->update($validated);
// Redirect the user to the created post woth an updated notification
return redirect(route('events.edit', [$event->slug]))->with('notification', 'Event updated!');
}
Edit Blade Page
<form method="post" action="{{ route('events.update', [$event->slug]) }}">
#csrf
#method('patch')
#include('partials.errors')
<div class="field">
<label class="label">Title</label>
<div class="control">
<input type="text" name="title" value="{{ $event->title }}" class="input" placeholder="Title" minlength="5" maxlength="100" required />
</div>
</div>
<div class="field">
<label class="label">Objective</label>
<div class="control">
<textarea name="content" class="textarea" placeholder="Content" minlength="5" maxlength="2000" required rows="10">{{ $event->objective }}</textarea>
</div>
</div>
<div class="field">
<label class="label">Date</label>
<div class="control">
<input type="text" name="title" value="{{ $event->date }}" class="input" placeholder="Title" minlength="5" maxlength="100" required />
</div>
</div>
<div class="field">
<label class="label">Venue</label>
<div class="control">
<input type="text" name="title" value="{{ $event->venue }}" class="input" placeholder="Title" minlength="5" maxlength="100" required />
</div>
</div>
<div class="field">
<label class="label">Description</label>
<div class="control">
<textarea name="content" class="textarea" placeholder="Content" minlength="5" maxlength="2000" required rows="10">{{ $event->description }}</textarea>
</div>
</div>
<div class="field">
<div class="control">
<button type="submit" class="button is-link is-outlined">Update</button>
</div>
</div>
</form>
Thank you for your time!

You need to change the name attribute of Date, Venue and Description and Objective.

On your edit blade page, the input names are alternating 'title' and 'content'
can you rename the input names so that they can be unique

Related

data was not stored in laravel and not getting any errors

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

Request contains null although the input has been entered

I'm trying to update my data with Laravel. I'm able to create, read, delete the data but somehow i cannot update my data. I already checked my controller,model,route and view but i don't think there's any typo or anything. It only redirects to it's index page without being updated although i have entered new input. There's no error message at all so i checked where is the problem. So i checked my update function in my controller and tried to show the request by echo "$request->kode_kontak"; and echo $request->kode_kontak; but it shows nothing which i assume that it's null/empty but when i echo "yes" it showed on the screen "yes" i tested this because i want to know if the function itself is working so the problem here is that the request contains null, no wonder i cannot update it. Why is the request isn't passed? why is it like this? and how to fix it?
Route for edit and update
Route::get('contact/{contact}/edit', 'ContactController#edit')->name('contact.edit');
Route::patch('contact/{contact}','ContactController#update')->name('contact.update');
Controller with edit and update function
use Illuminate\Http\Request;
use App\Contact;
use DB;
public function edit($kode_kontak){
$contact = DB::table('contact')->where('kode_kontak',$kode_kontak)->get();
return view('contact.edit',['contact' => $contact]);
}
public function update(Request $request){
DB::table('contact')->where('kode_kontak',$request->kode_kontak)->update([
'email' => $request->email,
'telepon' => $request->telepon,
]);
return redirect('contact');
}
Model
class Contact extends Model
{
public $timestamps = false;
protected $table = 'contact';
protected $fillable = [
'kode_kontak',
'kode_pegawai',
'email',
'telepon'
];
protected $primaryKey = 'kode_kontak';
}
View of edit.blade.php
<div id="contact">
<h2>Edit Contact</h2>
#foreach($contact as $p)
<form action="{{ route('contact.update', ['kode_pegawai' => $p->kode_pegawai]) }}" method="POST">
#csrf
#method('patch')
<div class="form-group">
<label for="kode_contact" class="control-label">Kode Kontak</label>
<input type="text" name="kode_kontak" id="kode_kontak" class="form-control" value="{{ $p->kode_kontak}}" disabled>
</div>
<div class="form-group">
<label for="kode_pegawai" class="control-label">Kode Pegawai</label>
<input type="text" name="kode_pegawai" id="kode_pegawai" class="form-control" value="{{ $p->kode_pegawai}}" disabled>
</div>
<div class="form-group">
<label for="email" class="control-label">Email</label>
<input type="text" name="email" id="email" class="form-control" value="{{ $p->email}}">
</div>
<div class="form-group">
<label for="telepon" class="control-label">Telepon</label>
<input type="text" name="telepon" id="telepon" class="form-control" value="{{ $p->telepon}}">
</div>
<div class="form-group">
<input class="btn btn-primary form-control" type="submit" value="Simpan">
</div>
</form>
#endforeach
</div>
Your issue is that you have disabled those inputs. Disabled inputs will not be submitted.
If you want to display the disabled inputs, but still PATCH the values, you will need to add hidden inputs with those values like:
<div class="form-group">
<label for="kode_contact" class="control-label">Kode Kontak</label>
<input type="text" id="kode_kontak" class="form-control" value="{{ $p->kode_kontak}}" disabled>
<input type="hidden" name="kode_kontak" value="{{ $p->kode_kontak}}">
</div>
<div class="form-group">
<label for="kode_pegawai" class="control-label">Kode Pegawai</label>
<input type="text" id="kode_pegawai" class="form-control" value="{{ $p->kode_pegawai}}" disabled>
<input type="hidden" name="kode_pegawai" value="{{ $p->kode_pegawai}}">
</div>
Hope that helps!
$request->kode_kontak is $contact here, $request->kode_kontak is not available in $request, change $contact instead :
public function update(Request $request, $contact){
DB::table('contact')->where('kode_kontak',$contact)->update([
'email' => $request->email,
'telepon' => $request->telepon,
]);
return redirect('contact');
}

How to fix edit interface getting only first word of a column in table in Laravel 5.7?

My edit interface edit.blade.php only gets the first word of the name from my database, this is what the index.blade.php looks like
and when i click on the edit icon of the 3rd line it leads me to edit.blade.php which gives me this
"Nom d'établissement" textfield only gets the first word from the database
Everything looks fine in the database:
this is my edit.blade.php form:
<form method="post" action="{{ route('etablissements.update', $etablissement->id) }}">
#method('PATCH')
#csrf
<div class="col-5">
<div class="form-group">
<label for="nom">Nom Etablissement :</label>
<input type="text" class="form-control" name="nom" value={{ $etablissement->nom }} />
</div>
<div class="form-group">
<label for="price">E-Mail :</label>
<input type="text" class="form-control" name="email" value={{ $etablissement->email }} />
</div>
<div class="form-group">
<label for="quantity">Telephone:</label>
<input type="text" class="form-control" name="telephone" value={{ $etablissement->telephone }} />
</div>
</div>
<button type="submit" class="btn btn-primary">Confirmer</button>
</form>
this is edit function in the controller:
public function edit($id)
{
$etablissement = Etablissement::find($id);
return view('etablissements.edit', compact('etablissement'));
}
and this is update function in the controller:
public function update(Request $request, $id)
{
$request->validate([
'nom'=>'required',
'email'=> 'required',
'telephone' => 'required|numeric'
]);
$etablissement = Etablissement::find($id);
$etablissement->nom = $request->get('nom');
$etablissement->email = $request->get('email');
$etablissement->telephone = $request->get('telephone');
$etablissement->save();
return redirect('/etablissements')->with('success', 'Utilisateur édité');
}
Quote the value attribute.
<input type="text" class="form-control" name="nom" value="{{ $etablissement->nom }}" />
Without quotes, the second word in$etablissement->nom is interpreted as another attribute rather than part of the value of the value attribute.
The email and telephone values are showing up correctly because there are no spaces, but you should quote those as well just in case.

Use of undefined constant title - assumed 'title'

I am pretty new in Laravel,
actually I am trying to create a crud operation using Laravel 5.6, so I have created create, delete function successfully, but on update function I am getting an error
Please find the attched image for detailed error
Use of undefined constant title - assumed 'title' (this will throw an Error in a future version of PHP)
Controller
public function edit($id){
$blogCategories = BlogCategories::find($id);
if (empty($blogCategories)) {
Flash::error('Category not found');
return redirect(route('categories.index'));
}
return view('cms/BlogCategories/editCategory')->with('blogCategories', $blogCategories);
}
public function update(Request $request, $id){
$blogCategories = BlogCategories::find($id);
$blogCategories->title = $request->get(title);
$blogCategories->slug = $request->get(slug);
$blogCategories->description = $request->get(description);
$blogCategories->featured_image = $request->get(featured_image);
$blogCategories->save();
return redirect()->back();
}
Model
class BlogCategories extends Model{
protected $fillable = ['title', 'slug', 'description', 'featured_image'];
protected $guarded = [];
}
Form
<form action="{{route('categories.update', $blogCategories->id)}}" method="post" class="m-form m-form--fit m-form--label-align-right">
#csrf
#method('put')
<div class="m-portlet__body">
<div class="form-group m-form__group">
<label>Title</label>
<input type="text" class="form-control m-input" name="title" id="title" value="{{$blogCategories->title}}" aria-describedby="emailHelp" placeholder="Muhammad Owais">
</div>
<div class="form-group m-form__group">
<label>slug</label>
<input type="text" class="form-control m-input" name="slug" id="slug" value="{{$blogCategories->slug}}" aria-describedby="emailHelp" placeholder="mail#domain.com">
</div>
<div class="form-group m-form__group">
<label>Description</label>
<textarea class="form-control" name="description" id="description" value="{{$blogCategories->description}}" placeholder="Enter Description"></textarea>
</div>
<div class="form-group m-form__group">
<label>Featured Image</label>
<input type="text" class="form-control m-input" name="featured_image" id="featured_image" value="{{$blogCategories->featured_image}}" aria-describedby="emailHelp" placeholder="Enter Amazon S3 URL">
</div>
</div>
<div class="m-portlet__foot m-portlet__foot--fit">
<div class="m-form__actions">
<button type="submit" class="btn btn-primary">
Submit
</button>
<button type="reset" class="btn btn-secondary">
Cancel
</button>
</div>
</div>
</form>
Use quotes for your all HTTP requests like :
$request->get('title');
$request->get('slug');

I have the following error: "Type error: Too few arguments to function AlbumController::postEdit(), 1 passed and exactly 2 expected"

I have the following problem when trying to edit an "album", hopefully they can help me, I'm a little frustrated haha.
The Form
<form name="editalbum" action="{{ action('AlbumController#postEdit', $album->id) }}" method="POST" enctype="multipart/form-data">
{{ csrf_field() }}
<fieldset>
<h2>Editar <strong>{{$album->name}}</strong></h2>
<br></br>
<div class="form-group">
<label for="name">Nombre del proyecto</label>
<input name="name" type="text" class="form-control" value="{{ $album->name }}" required>
</div>
<div class="form-group">
<label for="description">Descripción del proyecto</label>
<textarea name="description" rows="10" cols="50" type="text" class="form-control" value="{{ $album->description }}" required></textarea>
</div>
<div class="form-group">
<label for="location">Locación:</label>
<input name="location" type="text" class="form-control" value="{{ $album->location }}" required>
</div>
<div class="form-group">
<label for="year">Año:</label>
<input name="year" type="text" class="form-control" value="{{ $album->year }}" required>
</div>
<button type="submit" class="btn btn-primary">Editar</button>
</fieldset>
</form>
So far I think everything is going well because I try to post in the ID of the model.
The function:
public function postEdit(Request $request, $id)
{
$album = Album::find($id);
$album = Album::all();
if(count($album) > 0){
$album->name = Input::get('name');
$album->description = Input::get('description');
$album->year = Input::get('year');
$album->location = Input::get('location');
$album->save();
Alert::success('Successfully Updated', 'Congratulations');
return view('admin.dashboard');
} else {
Alert::error('Facilities not found', 'Error');
return view('galeries');
}
I think you made error in routes.php
It should look like this:
Route::post('albums/update/{id}', ['uses' => 'AlbumController#postEdit']);
One solution will be to remove the DI Request object
public function postEdit($id)
{
//rest of code
}
note: the param has to be passed as a array
action="{{ action('AlbumController#postEdit', ['id' => $album->id]) }}"

Categories