Laravel Update Function Don't Save New Data - php

So I have trouble updating data from edit form. I tried to use 'dd' and it's collect all the data it needs. No error, but the data on database not change.
public function update(Request $request, Stationery $stationery)
{
$validated = $request->validate([
'category_id' => 'required',
'nama' => 'required',
'satuan' => 'nullable',
'harga' => 'required',
'keterangan' => 'nullable'
]);
// dd($validated);
Stationery::where('id', $stationery->id)
->update($validated);
return redirect('/barang/pakaihabis')->with('success', 'Data Berhasil Diubah!!');
}
The success message pop out but the data still same.
The only protected in the model Stationery
protected $guarded = ['id'];

You can use:
$stationery->update($validated);
There is no need for where because you use route model binding ;)

Related

How to save data from result where eloquent Laravel

I want to save data returned from 'where' eloquent laravel.
My code:
public function duplicate_save(Request $request){
$this->validate($request, [
'bulan_from' => 'required',
'tahun_from' => 'required',
'bulan_to' => 'required',
'tahun_to' => 'required',
]);
$realisasi_keuangan = RealisasiKeuangan::where('bulan', $request->bulan_from)->where('tahun', $request->tahun_from)->get();
RealisasiKeuangan::create($realisasi_keuangan);
return redirect()->route('apps.realisasi-keuangan.index');
}
But the code return error
Illuminate\Database\Eloquent\Builder::create(): Argument #1 ($attributes) must be of type array, App\Models\RealisasiKeuangan given

Laravel saving data to two locations

I'm working on a larvel project where the user can create appointments. In addition I've created another model called clients so when a user creates an appointment the users "client" data is saved.
In my appointments controller I have the following: -
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required',
]);
//create appointment
$apt = new Appointment;
$apt->name = $request->input('name');
$apt->user_id = auth()->user()->id;
$apt->save();
//create client
$client = new Client;
$client->first_name = $request->input('name');
$client->user_id = auth()->user()->id;
$client->save();
return redirect('/appointments')->with('success', 'Appointment created');
}
When saving the data it works and stores the data in the clients table however I know this isn't the cleanest way of saving the data, but what is the "laravel" way of doing this?
There's nothing wrong with your code. It's totally fine keeping it that way.
I prefer to say Model::create() to create models in one statement.
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required',
]);
Appointment::create([
'name' => request('name'),
'user_id' => auth()->id()
]);
Client::create([
'first_name' => request('name'),
'user_id' => auth()->id,
]);
return redirect('/appointments')->with('success', 'Appointment created');
}
You can also use tap() function:
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required',
]);
tap(Appointment::create(['name' => request('name'), 'user_id' => auth()->id()]), function ($newAppoinment) {
Client::create([
'first_name' => $newAppoinment->name,
'user_id' => auth()->id,
]);
});
return redirect('/appointments')->with('success', 'Appointment created');
}
Or the best approach could be using model events:
class Appointment extends Model
{
public static function boot()
{
parent::boot();
static::created(function ($appointment) {
Client::create([
'user_id' => $appoinment->user_id,
'first_name' => $appoinment->name,
])
});
}
}

Laravel Put/Patch Request

Im trying to do a Put/Patch Request, I am using Postman, this is my current Code:
class CustomerController extends Controller
{
public function getAllCustomer()
{
return Customer::get();
}
public function addNewCustomer(Request $request)
{
$validatedData = $request->validate([
'Title' => 'required',
'Name' => 'required|max:255',
'Surname' => 'required|max:255',
'Email' => 'required',
'Phone' => 'required',
'Password' => 'required',
'dateofBirth' => 'required'
]);
return \app\model\Customer::create($request->all());
}
public function update (Request $request , Customer $id)
{
$id->update($request->all());
}
And this my route:
Route::put('Customer/{id}' , 'CustomerController#update');
Im trying to insert some Parameters into Postman, but I think the way I do it is not correct, right now I do it like this:
Im not getting any Errors, but nothing is happening, maybe somebody knows a solution.
I want to Change the Name of the customer.
Thanks!
Try to set x-www-form-urlencoded for body in postman.

How to get id When Using Implicit Route Model Binding, i need it in unique validation

// in the validation section "alias" field should be unique so i need this NursingHome object id(primary key) to force validation to not to check for this id.
I have checked it with $nursinghome->getKey() method but no success.
public function update(Request $request, NursingHome $nursinghome)
{
$request->validate([
'name' => 'required|string|max:255',
'address' => 'nullable|string',
'alias' => 'required|string|unique:nursing_home,'.$nursinghome->id,
]);
$data = $request->all();
$data['updated_by'] = Auth::guard('api')->id();
$nursinghome->update($data);
return response()->json($nursinghome, 200);
}
There is a know issue disscussed in laravel github, that if your model has two words like NursingHome the it is not injected in controller:
public function update(Request $request, $id){
$nursinghome = NursingHome::find($id); //now you will get $nursinghome->id
$request->validate([
'name' => 'required|string|max:255',
'address' => 'nullable|string',
'alias' => 'required|string|unique:nursing_home,'.$nursinghome->id,
]);
$data = $request->all();
$data['updated_by'] = Auth::guard('api')->id();
$nursinghome->update($data);
return response()->json($nursinghome, 200);
}
If your model having two or more words, you have to use only small letters.

Laravel 5.5 duplicate insert to database on submit

can someone help me...my laravel code insert twice to database when I click submit, it store double data to database,
here is my code :
controller
public function store(Request $request)
{
$this ->validate($request,[
'nim' => 'required|max:8',
'nama' => 'required|max:30',
'alamat' => 'required|max:100',
'jenis_kelamin' => 'required|max:9',
'no_tlp' => 'required|regex:/[0-9]{12}/',
'tempat' => 'required',
'tanggal' => 'required',
'id_jurusan' => 'required'
]);
$simpan = new Mahasiswa([
'nim' => $request->get('nim'),
'nama' => $request->get('nama'),
'alamat' => $request->get('alamat'),
'jenis_kelamin' => $request->get('jenis_kelamin'),
'no_tlp' => $request->get('no_tlp'),
'tempat' => $request->get('tempat'),
'tanggal' => $request->get('tanggal'),
'id_jurusan' => $request->get('id_jurusan')
]);
$simpan->save();
if (Mahasiswa::create($request->all())) {
$request->session()->flash('status', 'success');
$request->session()->flash('pesan', 'Data Berhasil Disimpan');
}else{
$request->session()->flash('status', 'danger');
$request->session()->flash('pesan', 'Data gagal Disimpan!!');
}
return redirect('/Mahasiswa/create');
}
and this my model code :
class Mahasiswa extends Model
{
protected $fillable = ['nim','nama','alamat','jenis_kelamin','no_tlp','tempat','tanggal','id_jurusan'];
protected $table = 'mahasiswa';
}
Thanks in advance
You are saving your data twice:
$simpan->save();
and then again:
Mahasiswa::create($request->all())
Just remove the $simpan->save(); line.

Categories