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');
}
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
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
I am trying to get hold of Laravel framework and got stuck while retrieving the values from the page in a Database.
These are the snippets of my controller, routes, view part which might help in understanding what I am doing
web.php (for routes):
<?php
use Illuminate\Support\Facades\Route;
Route::resource('posts', 'PostController');
Postcontroller.php (just a snippet)
<?php
public function create()
{
//
return view('posts.create');
}
public function store(Request $request)
{
//
$this->validate($request , array(
'title' => 'required',
'body' => 'required'
));
$post = new Post;
$post->title = $request->title;
$post->body = $request->body;
$post->save();
return redirect()->route('posts.show', $post->id);
}
create.blade.php(for view)
<div class="row">
<div class="col-mid-8 col-md-offset-2" >
<h1>Create New Post</h1>
<hr>
<form route='posts.store' >
#csrf
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1">
</div>
<div class="form-group form-check">
<input type="checkbox" class="form-check-input" id="exampleCheck1">
<label class="form-check-label" for="exampleCheck1">Check me out</label>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
The problem with your code is that you don't have name properties on your input, so nothing gets passed to your backend code.
Add name to your inputs like this (they need to be different, else they would get overwritten):
<input type="text" class="form-control" id="your-unique-input-id" name"your_unique_input_name">
Then, when you submit your form, the all the inputs that have name attribute in them will get passed to your backend method. You can then retrieve their value, by using the name you gave them earlier. So in this example:
$request->your_unique_input_name // Which will return anything that user typed in that field
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');
}
Here is my signup form
#extends('Layouts.master')
#section('title')
welcome
#endsection
#section('content')
#if(count($errors)>1)
<div class="row">
<div class="col-md-4">
<ul>
#foreach($errors->all() as $errors)
<li>{{$errors}}</li>
#endforeach
</ul>
</div>
</div>
#endif
<div class="row" style="background-color:#FFF8F2;">
<div class="col-lg-2">
</div>
<div class="col-lg-8">
<div class="panel panel-danger">
<div class="panel-heading">
<h3>Sign Up As A Company</h3>
</div>
<div class="panel-body">
<form action="{{route('companysignup')}}" method="post" role="form">
<div class="form-group">
<label>Enter Company Name</label>
<input placeholder="Enter Name" name="company_name" class="form-control">
</div>
<div class="form-group">
<label>Enter Owner Name</label>
<input placeholder="Enter Owner Name" name="owner_name" class="form-control">
</div>
<div class="form-group">
<label>Enter Owner Email</label>
<input placeholder="Enter Owner Email" name="email" class="form-control">
</div>
<div class="form-group">
<label>Enter Phone Number</label>
<input placeholder="Enter Phone Number" name="phone_number" class="form-control">
</div>
<div class="form-group">
<label>Company Type</label>
<select name="company_type" class="form-control">
<option>Combined</option>
<option>Individual</option>
<option>None</option>
</select>
</div>
<div class="form-group">
<label>Country</label>
<select name="country" class="form-control">
<option>Pakistan</option>
<option>Sudia Arabia</option>
<option>America</option>
<option>India</option>
</select>
</div>
{{--<div class="form-group">--}}
{{--<label>Attach Registration Extract</label>--}}
{{--<input type="file" name="file">--}}
{{--</div>--}}
<div class="form-group">
<label>Enter short description about your company</label>
<textarea name="description" rows="3" class="form-control"></textarea>
</div>
<div class="form-group">
<label>Enter Password</label>
<input name="password " type="password" placeholder="Enter Password" class="form-control">
</div>
{{csrf_field()}}
<input class="btn btn-outline btn-danger" type="submit" value="Submit">
</form>
</div>
<div class="panel-footer">
</div>
</div>
</div>
<div class="col-lg-2">
</div>
</div>
#endsection
here is my Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
class Company extends Model
{
use Notifiable;
protected $table = 'company';
public function user()
{
return $this->belongsTo('App\User');
}
protected $fillable = [
'user_id',
'company_name',
'owner_name',
'phone_number',
'country',
'company_type',
'description'
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'remember_token'
];
}
Here is my web.php code
Route::group(['middleware'=>['web']],function(){
Route::post('/companysignup',[
'uses' => 'CompanyController#companySignUp',
'as' => 'companysignup'
]);
});
Function is added company signup.
public function companySignUp(Request $request)
{
$this->validate($request,[
'email' => 'required|email|unique:users',
'password' => 'required|min:6',
'company_name' => 'required|max:120',
'owner_name' => 'required|max:120',
'phone_number' => 'required|min:12|max:14',
'company_type' => 'required',
'country' => 'required',
'description' => 'required'
]);
$email = $request['email'];
$password = bcrypt($request['password']);
$user = new User();
$user->email = $email;
$user->password = $password;
$user->role = 1; // Regular = 0, Company - 1
$user->save();
$company_name = $request['company_name'];
$owner_name = $request['owner_name'];
$country = $request['country'];
$phone_number = $request['phone_number'];
$company_type = $request['company_type'];
$description = $request['description'];
$company = new Company();
$company->user_id = $user->id;
$company->company_name = $company_name;
$company->owner_name = $owner_name;
$company->phone_number = $phone_number;
$company->company_type = $company_type;
$company->country = $country;
$company->description = $description;
$company->save();
return view('frontend.user');
}
Now you can check my function in controller Please suggest me solution for this problem .
First your validation is failing for some reasons. One of the reasons is that country is required but you don't send any country. Your country options are all empty. You did
<select name="country" class="form-control">
<option>Pakistan</option>
<option>Sudia Arabia</option>
<option>America</option>
<option>India</option>
</select>
In each of your options, add a value like
<option value="paskistan">Pakistan</option>
You are displaying errors in your view, so somehow it is supposed to display those validation errors to the screen.
Then the way you save your data, it's a little bit too much because your grab your variable and then use them. You could do both in one go
$company = Company::create([
'user_id' => $user->id,
'company_name' => $request->company_name,
'owner_name' => $request->owner_name,
//add all in your input the same way
'description' => $request->description
]);
And this will create a company and return the object to you.