i want to edit data, and the id is primary key.
but when i submit, the database does not change.
here the code
controller :
public function edit($id)
{
$karyawan = Karyawan :: find ($id);
$jabatan = Jabatan::all();
return view ('/karyawan.editData', compact(('karyawan'),('jabatan')));
}
public function update(Request $request, $id)
{
$request->validate([
'id' => 'required',
'nama'=> 'required',
'umur'=>'required',
'alamat'=> 'required',
'nomor'=> 'required'
]);
$karyawan =Karyawan::find($id);
$karyawan->jabatan_id = $request->jabatan;
$karyawan->nama = $request->nama;
// $karyawan->id = $request->id;
$karyawan->umur = $request->umur;
$karyawan->alamat = $request->alamat;
$karyawan->nomor = $request->nomor;
$karyawan->save();
return redirect ('/karyawan');
}
and the editData :
#extends ('layout')
#section ('content')
<h1>Edit Data Karyawan </h1>
<form action="/karyawan/{{ $karyawan->Id }}" method="post">
{{-- <form action = "/karyawan" method="post"> --}}
#csrf
<div class="form-group">
<label for = "title">ID Jabatan</label>
<select name="jabatan" class="form-control">
#foreach ($jabatan as $baris)
<option value = "{{ $baris->id}}">{{ $baris->nm_jabatan }}</option>
#endforeach
</select>
<div class = "form-group">
<label for="title"> nama</label>
<input type="text" class="form-control" name="nama">
</div>
<div class = "a">
<label for="title"> ID </label>
<input type="number" class="form-control" name="id" value = "{{ old('id') ? old('id'): $karyawan->id }}">
</div>
<div class = "form-group">
<label for="title"> umur</label>
<input type="text" class="form-control" name="umur">
</div>
<div class = "form-group">
<label for="title"> alamat</label>
<input type="text" class="form-control" name="alamat">
</div>
<div class = "form-group">
<label for="title"> nomor</label>
<input type="text" class="form-control" name="nomor">
</div>
<a href="/karyawan">
<button type = "button" class="btn btn-warning">kembali</button>
</a>
<button type ="submit" class = "btn btn-primary">Simpan</button>
</form>
#endsection
how i can update the data? from these code
i expect to get some explanation, and show me how to fix my code. so i can submit the update data on my database.
Try this in your controller
public function update(Request $request, $id)
{
$validated_data = $request->validate([
'name' => 'required|max:255', (add all the form here)
]);
YourModel::where('id', $id)->update($validated_data);
return redirect('admin/problem')->with('success', 'Data berhasil diubah');
}
Tips* dump all the request with dd($request->all) so you can see all of your input form data
Related
I m using laravel 8 controller for saving text input type using "Store function".My input for 'name' are not stored in database when i submited it.
Here is my blade
<form class="text-center p-4" action="{{ route('store') }}" method = "POST">
#csrf
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">names</label>
<input type="text" class="form-control" id="name" placeholder="name">
</div>
<button class="btn btn-primary" type="submit">Button</button>
</form>
Here is my ProductController.php
public function create()
{
return view('products.create');
}
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'detail' => 'required',
]);
Product::create($request->all());
$products -> save();
return redirect('/saving-list') ->with('success','Umrah record has been updated');
}
your parameter is not complete, 'detail' => 'required' but it's not found
you need new value input <input type="text" class="form-control" id="detail" placeholder="detail" name="detail">
and you have to add attribute name in every input
for complete code like this bellow
<form class="text-center p-4" action="{{ route('store') }}" method = "POST">
#csrf
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">names</label>
<input type="text" class="form-control" id="name" name="name" placeholder="name">
<input type="text" class="form-control" id="detail" name="detail" placeholder="detail">
</div>
<button class="btn btn-primary" type="submit">Button</button>
</form>
and for save or store method https://laravel.com/docs/8.x/eloquent#inserts
$product= new Product;
$product->name = $request->name;
$product->detail= $request->detail;
$product->save();
or like this
$product= Product::create([
'name' => $request->name,
'detail' => $request->detail,
]);
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');
}
I have a table in my database with contact details. On my form, I have a Dropdown which displays the names of people, and which, once I click on the name, fills in the fields with the data (ajax).
The problem happens when I want to perform operations on the form data.
For example, I click on "M Bram Manu" (id = 1) in the Dropdown. My Ajax will fill out my form with the details of this contact.
But when I want to update, delete or add a new contact, it doesn't work properly.
So, if I click on "delete", it not delete the contact I choose but the last contact of the dropdown list. Why???
So I think my link retrieves the last ID from my dropdown, so I decided to put an hidden Input with the value of the contact ID that appears in the form. But I don't know how to retrieve this value to define it as the ID to send to the controller.
Dropdown bar and link button :
<form class="col-md-9">
<label class="col-md-2">Rechercher : </label>
<select class="form-control select2 col-md-7" id="selInscrit" name="selInscrit" onchange="selectID()">
<option value="0" selected="selected"></option>
#foreach($inscrit as $inscrits)
<option value="{{$inscrits->INS_ID}}">{{$inscrits->INS_CIVILITE}} {{$inscrits->INS_NOM}} {{$inscrits->INS_PREN}} {{$inscrits->INS_NUM_RUE}} {{$inscrits->INS_Rue}} </option>
#endforeach
</select>
</form>
<div class="btn-group btn-group-sm col-md-3" role="group">
<form action="" method="GET">
<button type="submit" class="btn btn-secondary btn-sm">Edit</button>
</form>
<button type="submit" form="registerForm" formmethod="POST" formaction="{{ route('inscrits.store') }}" class="btn btn-secondary btn-sm">Save</button>
<button type="submit" form="registerForm" formmethod="POST" formaction="{{ route('inscrits.update') }}" class="btn btn-secondary btn-sm">Update</button>
<form action="{{ route('inscrits.destroy') }}" method="POST">
<input id="INS_ID" name="INS_ID" value="" type="hidden">
#method('DELETE')
#csrf
<button type="submit" class="btn btn-secondary btn-sm">Delete </button>
</form>
</div>
Ajax for dropdown :
<script>
$('#selInscrit').change(function() {
var id = $(this).val();
var url = '{{ route("inscrits.show", ":id") }}';
url = url.replace(':id', id);
$.ajax({
url: url,
type: 'get',
dataType: 'json',
success: function(response) {
if (response != null) {
$('#INS_ID').val(response.INS_ID);
$('#INS_CIVILITE').val(response.INS_CIVILITE);
$('#INS_NOM').val(response.INS_NOM);
$('#INS_PREN').val(response.INS_PREN);
$('#INS_NAISS').val(response.INS_NAISS);
$('#INS_AGE').val(response.INS_AGE);
$('#INS_NUM_RUE').val(response.INS_NUM_RUE);
$('#INS_Rue').val(response.INS_Rue);
$('#INS_TEL1').val(response.INS_TEL1);
$('#INS_OBS').val(response.INS_OBS);
$('#INS_DATE').val(response.INS_DATE);
$('#INS_TEL2').val(response.INS_TEL2);
}
}
});
});
Route :
// INSCRITS
/ route for index
Route::get('/inscrits', 'InscritController#index')->name('inscrits.index');
// route for dropdown bar
Route::get('/inscrits/show/{id}', 'InscritController#show')->name('inscrits.show');
// route for update
// Route::match(['put', 'patch'], '/inscrits/update','InscritController#update')->name('inscrits.update');
Route::post('/inscrits/update','InscritController#update')->name('inscrits.update');
// route for store
Route::post('/inscrits/store', 'InscritController#store')->name('inscrits.store');
//route for delete
Route::delete('/inscrits/destroy', 'InscritController#destroy')->name('inscrits.destroy');
Form :
<form id="registerForm">
#csrf
<div class="form-group row">
<div class="col-lg-1">
<label for="INS_CIVILITE">Civilité</label>
<select class="form-control form-control-sm" id="INS_CIVILITE" name="INS_CIVILITE">
<option value="" selected="selected"></option>
<option value="Mme">Mme</option>
<option value="Mlle">Mlle</option>
<option value="M.">M.</option>
</select>
</div>
<div class="col-lg-4">
<label for="INS_NOM">Nom</label>
<input class="form-control form-control-sm" id="INS_NOM" name="INS_NOM" value="" type="text">
</div>
<div class="col-lg-3">
<label for="INS_PREN">Prénom</label>
<input class="form-control form-control-sm" id="INS_PREN" name="INS_PREN" value="" type="text">
</div>
<div class="col-lg-2">
<label for="INS_NAISS">Année Naiss</label>
<input class="form-control form-control-sm" id="INS_NAISS" name="INS_NAISS" value="" type="text">
</div>
<div class="col-lg-2">
<label for="INS_AGE">Age</label>
<input class="form-control form-control-sm" id="INS_AGE" name="INS_AGE" value="" type="text">
</div>
</div>
<div class="form-group row">
<div class="col-lg-1">
<label for="INS_NUM_RUE"># Rue</label>
<input class="form-control form-control-sm" id="INS_NUM_RUE" name="INS_NUM_RUE" value="">
</div>
<div class="col-lg-9">
<label for="INS_Rue">Libellé voie</label>
<select class="form-control form-control-sm" id="INS_Rue" name="INS_Rue">
#foreach($rue as $rues)
<option value="{{$rues->RUE_NUM}}">{{$rues->RUE_NOM}} ({{$rues->RUE_Type}})</option>
#endforeach
</select>
</div>
<div class="col-lg-2">
<label for="INS_TEL1">Téléphone 1</label>
<input class="form-control form-control-sm" id="INS_TEL1" name="INS_TEL1" value="" type="text">
</div>
</div>
<div class="form-group row">
<div class="col-lg-8">
<label for="INS_OBS">Observation</label>
<input class="form-control form-control-sm" id="INS_OBS" name="INS_OBS" value="" type="text">
</div>
<div class="col-lg-2">
<label for="INS_DATE">Date d'inscription</label>
<input class="form-control form-control-sm" id="INS_DATE" name="INS_DATE" value="" type="text">
</div>
<div class="col-lg-2">
<label for="INS_TEL2">Téléphone 2</label>
<input class="form-control form-control-sm" id="INS_TEL2" name="INS_TEL2" value="" type="text">
</div>
</div>
<input id="INS_LIEU" name="INS_LIEU" value="WEB" type="hidden">
<input id="INS_EID" name="INS_EID" value="" type="hidden">
</form>
The controller :
public function index()
{
$inscrit = Inscrit::all();
return view('index', compact('inscrit'));
}
public function store(Request $request)
{
$storeData = $request->validate([
'INS_CIVILITE' => 'max:15',
'INS_NOM' => 'max:50',
'INS_PREN' => 'max:50',
'INS_NUM_RUE' => 'max:8',
'INS_TEL1' => 'max:10',
'INS_TEL2' => 'max:10',
'INS_AGE' => 'numeric',
'INS_OBS' => 'max:255',
'INS_Rue' => 'max:255',
'INS_DATE' => 'max:255',
'INS_NAISS' => 'max:255',
]);
$inscrit = Inscrit::create($storeData);
return redirect('/inscrits')->with('completed', 'Nouvel inscrit !');
}
public function show($id = 0)
{
$data = Inscrit::where('INS_ID', $id)->first();
return response()->json($data);
}
public function edit($id)
{
$inscrit = Inscrit::findOrFail($id);
return view('index', compact('inscrit'));
}
public function update(Request $request, $id)
{
$updateData = $request->validate([
'INS_CIVILITE' => 'max:15',
'INS_NOM' => 'max:50',
'INS_PREN' => 'max:50',
'INS_NUM_RUE' => 'max:8',
'INS_TEL1' => 'max:10',
'INS_TEL2' => 'max:10',
'INS_AGE' => 'numeric',
'INS_OBS' => 'max:255',
'INS_Rue' => 'max:255',
'INS_DATE' => 'max:255',
'INS_NAISS' => 'max:255',
]);
$id = request()->input('INS_EID');
Inscrit::where('INS_ID', $id)->update($updateData);
return redirect('/inscrits')->with('completed', 'inscrit mis à jour');
}
public function destroy($id)
{
$id = request()->input('INS_ID');
$inscrit = Inscrit::where('INS_ID', $id)->delete();
return redirect('/inscrits')->with('completed', 'inscrit supprimé');
}
The form
Database
Something like :
public function edit ($id) {
$inscritContent = Inscrit::where('id', $id)->firstOrFail();
return view('admin.inscrit.edit.form', [
'content' => $iscritContent
]);
}
public function update($id, Request $req) {
$inscrit = Inscrit::findOrFail($id);
$inscrit->update($req->all());
return redirect(route('admin.inscrit.index'));
}
admin.inscrit.edit.form and admin.inscrit.index are blade template
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.
I have a user and profile table and user_id is the foreign key in the profile table and oneto one relationship. Now I am trying to show the data of user and profile in text field at the profile blade. Also, I want to save edit data in using same text field by clicking save change button. I can showing the user table data using value="{{Auth::user()->mobile}}" but confused to show profile data and how to edit user and profile data. How can I edit the all data?
Profile Blade
<div class="tab-pane active" id="tab_1_1">
<form role="form" method="POST">
{{ csrf_field() }}
<div class="form-group">
<label class="control-label">Full Name</label>
<input type="text" value="{{Auth::user()->name}}" name="name" class="form-control">
</div>
<div class="form-group">
<label class="control-label">Email</label>
<input type="text" value="{{Auth::user()->email}}" name="email" class="form-control"> </div>
<div class="form-group">
<label class="control-label">Mobile Number</label>
<input type="text" value="{{Auth::user()->mobile}}" name="mobile" class="form-control">
</div>
<div class="form-group">
<label class="control-label">Organization</label>
<input type="text" value="" name="organization" class="form-control">
</div>
<div class="form-group">
<label class="control-label">Department</label>
<input type="text" value="" name="department" class="form-control">
</div>
<div class="form-group">
<label class="control-label">Designation</label>
<input type="text" value="" name="designation" class="form-control">
</div>
<div class="form-group">
<label class="control-label">Address</label>
<textarea class="form-control" rows="3" name="address" value=""></textarea>
</div>
<div class="margiv-top-10">
#if (Auth::user()->id)
<!-- <button type="submit" class="btn btn-primary">Edit</button> -->
Save
#endif
Cancel
</div>
</form>
</div>
Controller
public function profileDataUpdate(Request $request)
{
$this->validate($request,[
'user_id'=> '',
'organization' => '',
'department' => '',
'designation' => '',
'address' => '',
]);
$users = new User();
$users->name = $request->name;
$users->email = $request->email;
$users->mobile = $request->mobile;
$users->save();
$profiles = new Profile();
dd($profiles->user_id = $request->user_id);
$profiles->organization = $request->organization;
$profiles->department = $request->department;
$profiles->designation = $request->designation;
$profiles->address = $request->address;
$profiles->save();
return redirect(route('profile'))->with('successMsg','profile Successfully Updated');
}
First, you need to fetch user information in controller, with that way you show related information easly
in your user model
user model
public function profile(){
return $this->hasOne(Profile::class,"user_id","id");
}
in your controller (we gonna fetch user information)
public function showProfile(){
$id = Auth::id();
$user = User::with("profile")->find($id);
return view("profile",compact("user"));
}
view (so in your view, we're able to get profile information with $user variable)
name: {{$user->name}}
organization: {{$user->profile->organization}}
And updating.. You're not updating in your controller, you're trying to create new user and profile.
I assume that profile informations doesn't exist at the first, so we need to use updateOrCreate method
public function profileDataUpdate(Request $request)
{
// validation stuff here
User::where("id", $request->user_id)
->update([
"name" => $request->name,
"email" => $request->email,
"mobile" => $request->mobile
]);
Profile::updateOrCreate(["user_id" => $request->user_id],[
"organization" => $request->organization,
"department" => $request->department,
"designation" => $request->designation,
"address" => $request->address,
]);
return redirect(route('profile'))->with('successMsg','profile Successfully Updated');
}