Call to a member function update() on null in laravel - laravel

So i want to make admin can change any user's password. but i got this error
Call to a member function update() on null
I have no idea what to do
this is my controller
public function resetPassword(Request $req, $id)
{
$req->validate([
'new_password' => ['required'],
'new_confirm_password' => ['same:new_password'],
],
[
'new_password.required' => 'Password Baru Harus Diisi !',
'new_confirm_password.same' => 'Password Baru Harus Sama Dengan Confirm Password !',
]);
User::find($id)->update(['password'=> Hash::make($req->new_password)]);
return redirect('/admin/list_user')->with('status', 'Password Berhasil di Ubah !');
}
this my route
Route::put('/admin/{id}/reset/password', 'PageController#resetPassword')->name('resetPassword');
this is my view modal
<div class="modal fade" id="resetModal" tabindex="-1" role="dialog" aria-labelledby="resetModal" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Reset Password</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form action="/admin/{id}/reset/password" method="POST">
{{csrf_field()}}
{{ method_field('PUT') }}
<div class="form-group">
<label for="password">Password Baru</label>
<input name="new_password" type="password" class="form-control" id="new_password" required>
</div>
<div class="form-group">
<label for="password">Confirm Password</label>
<input name="new_confirm_password" type="password" class="form-control" id="new_confirm_password" required>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Tutup</button>
<button type="submit" class="btn btn-primary">Buat</button>
</form>
</div>
</div>
</div>
</div>
</div>

<form action="/admin/{id}/reset/password" method="POST">
You're not passing any id. The route picks up '{id}' as the id, then tries to find an User with the id '{id}' and finds none. (->first() returns null).
Just change that opening form tag's action attribute:
<form action="{{ route('resetPassword', ['id' => Auth::id()]) }}" method="POST"> (or instead of Auth::id() the user_id you're trying to update.
You could also use findOrFail($id) instead of find($id) so you'll get a clearer error message if an User is not found.

Related

Laravel store doesn't save anything

I am making an application in Laravel. I've created a form to save a model (Repair). But when I save the form, you see it redirecting, but nothing saves. No error or success message is shown?
The store function in the RepairController.php:
public function store(Request $request)
{
$this->validate($request, [
'customer_id' => 'required',
'defect' => 'required',
'stats_id' => 'required'
]);
Repair::create([
'customer_id' => $request->customer_id,
'serial_number' => $request->serial_number,
'model' => $request->model,
'defect' => $request->defect,
'diagnostics' => $request->diagnostics,
'comments' => $request->comments,
'status_id' => $request->status_id
]);
return redirect()->route('repair.index')->with('success',$msg);
}
Repair.php has a fillable:
protected $fillable = ['customer_id', 'serial_number', 'model', 'defect', 'diagnostics', 'comments', 'status_id'];
The create modal:
<div id="newModal" class="modal fade" id="modal-default" tabindex="-1" role="dialog" aria-labelledby="modal-default" aria-hidden="true">
<div class="modal-dialog modal- modal-dialog-centered modal-" role="document">
<div class="modal-content">
<div class="modal-header">
<h6 class="modal-title" id="modal-title-default">Type your modal title</h6>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form action="{{ route('repair.store') }}" method="POST" id="editForm">
#csrf
<input type="text" name="customer_id" class="form-control" placeholder="Customer ID"><br>
<input type="text" name="serial_number" class="form-control" placeholder="Serial number"><br>
<input type="text" name="model" class="form-control" placeholder="Model"><br>
<input type="text" name="defect" class="form-control" placeholder="Defect"><br>
<div class="form-group">
<label for="Diagnostics">Diagnostics</label>
<textarea name="diagnostics" class="form-control" id="Diagnostics" rows="3"></textarea>
</div>
<div class="form-group">
<label for="Comments">Comments</label>
<textarea name="comments" class="form-control" id="Comments" rows="3"></textarea>
</div>
<input type="text" name="status_id" class="form-control" placeholder="Status ID"><br>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Create repair</button>
<button type="button" class="btn btn-link ml-auto" data-dismiss="modal">Close</button>
</div>
</form>
</div>
</div>
</div>
The route in web.php:
Route::resource('repair', 'RepairController');
There might be some validation fails in your data. Can you add the below code above your form and see what it prints.
#if($errors->any())
<ul>
#foreach($errors->all() as $error)
<li>{{$error}}</li>
#endforeach
</ul>
#endif

How do I solve the modal update issues in Laravel?

I have a modal in which I want to update my Question but the modal keeps updating the same question so can someone help me because my modal keeps taking the same $id even if I click on different entries.
This is my QuestionController
public function update($questionId)
{
$this->validate(request(),[
'label' => 'required',
]);
$data = request()->all();
$question = Question::find($questionId);
$question->label = $data['label'];
$question->save();
return redirect('/question');
}
this is my Modal in the blade.php file
<!-- Modal Edit -->
<div class="modal" id="editMcqModal" tabindex="-1" role="dialog"
aria-labelledby="editMcqModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalCenteredLabel">Edit Question</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form class="" action="/question/{{$question->id}}" method="post">
#csrf
#method('PATCH')
<div class="form-group">
<textarea name="label" class="form-control" placeholder="Please Enter Question" cols="5"
row="15">{{$question->label}}</textarea>
</div>
<div class="form-group">
<button type="submit" name="add" class="btn btn-success float-right">Update
Question</button>
</div>
</form>
</div>
</div>
</div>

Error: 405 - Method not allowed while trying to update in Laravel

Current, I am working on a project using Laravel-5.8. I have a code to update data:
Controller
public function store_external_respondent(StoreAppraisalRespondentExternalRequest $request)
{
$userCompany = Auth::user()->company_id;
$userEmployee = Auth::user()->employee_id;
DB::beginTransaction();
try {
$respondent = AppraisalRespondent::create([
'fullname' => $request->fullname,
'respondent_email' => $request->respondent_email,
'company_name' => $request->company_name,
'is_internal' => 1,
]);
DB::commit();
Session::flash('success', 'Appraisal Respondent created successfully');
return redirect()->route('appraisal.appraisal_respondents.index');
} catch (Exception $ex) {
DB::rollback();
Session::flash('error', 'Action failed! Please try again');
return back();
}
}
route/web.php:
Route::group(['prefix' => 'appraisal', 'as' => 'appraisal.', 'namespace' => 'Appraisal', 'middleware' => ['auth']], function () {
Route::post('appraisal/appraisal_respondents/update_external_respondent/{id?}', 'AppraisalRespondentsController#update_external_respondent')->name('appraisal_respondents.update_external_respondent');
});
When I did php artisan route:list, I got:
| | POST | appraisal/appraisal/appraisal_respondents/update_external_respondent/{id?} | appraisal.appraisal_respondents.update_external_respondent | App\Http\Controllers\Appraisal\AppraisalRespondentsController#update_external_respondent | web,auth
update view blade
<button type="button" class="btn btn-sm btn-info mr-1 edit-respondent-respondent" data-toggle="modal" data-target="#edit_respondent_external{{ $respondentexternal->id }}">Edit</button>
<div class="modal fade" id="edit_respondent_external{{ $respondentexternal->id }}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form action="{{route('appraisal.appraisal_respondents.update_external_respondent',['id'=>$respondentexternal->id])}}" method="post" id="edit_respondent-external-form">
{{ csrf_field() }}
<input type="hidden" name="appraisal_identity_id" value="{{$identities->id}}">
<input type="hidden" name="employee_id" value="{{$employees->id}}">
<input name="_method" type="hidden" value="PUT">
<div class="modal-header">
Update Respondent (External)
</div>
<div class="col-md-12">
<div class="form-group">
<label class="control-label"> FullName:<span style="color:red;">*</span></label>
<input type="text" name="fullname" placeholder="Enter external respondent fullname here" class="form-control" value="{{old('fullname',$respondentexternal->fullname)}}">
#error('fullname')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label class="control-label"> Company:<span style="color:red;">*</span></label>
<input type="text" name="company_name" placeholder="Enter external respondent company here" class="form-control" value="{{old('company_name',$respondentexternal->company_name)}}">
#error('company_name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label class="control-label"> Respondent Email:<span style="color:red;">*</span></label>
<input type="text" name="respondent_email" placeholder="Enter external respondent email here" class="form-control" value="{{old('respondent_email',$respondentexternal->respondent_email)}}">
#error('respondent_email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" id="edit_respondent_external_btn-submit" class="btn btn-success btn-ok">Save</button>
</div>
</form>
</div>
</div>
</div>
When I submitted, I got this error:
405
Method Not Allowed.
Sorry, the page you are looking for could not be found. Go Back
In the console I have:
POST http://localhost:8888/myapp/appraisal/appraisal/appraisal_respondents/update_external_respondent/2 405 (Method Not Allowed)
How do I resolve this?
Also Why is appraisal repeated twice in
POST http://localhost:8888/myapp/appraisal/appraisal/appraisal_respondents/update_external_respondent/2 405 (Method Not Allowed)
Thanks
// STEP 1
Route::post('appra....
// STEP 2
<input name="_method" type="hidden" value="PUT">
So what's wrong?
please use {{method_field('check_your_route_type')}} below {{ csrf_field() }}
e.g : Route::put('url' ... should use {{method_field('PUT')}}

Laravel - How to Pass Parameter to return redirect

In my Laravel-5.8, I have:
public function manager_employee_goal_list($id)
{
$goal = AppraisalGoal::findOrFail($id);
$goaldetails = AppraisalGoalDetail::where('appraisal_goal_id', $id)->get();
return view('appraisal.appraisal_goals.manager_employee_goal_list')
->with('goal', $goal)
->with('goaldetails', $goaldetails);
}
public function manager_employee_goal_approve(Request $request, $id)
{
$goal = AppraisalGoal::find($id);
$goal->is_approved = 3;
$goal->line_manager_comment = $request->line_manager_comment;
$goal->save();
Session::flash('success', 'Goal is approved');
return redirect()->route('appraisal.appraisal_goals.manager_employee_goal_list');
}
view : manager_employee_goal_list
<a class="btn btn-xs btn-info" data-toggle="modal" data-target="#approve{{ $goal->id }}" data-original-title="Approve">
Approve
</a>
<div class="modal fade" id="approve{{ $goal->id }}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form action="{route('appraisal.appraisal_goals.manager_employee_goal_approve',['id'=>$goal->id])}}" method="post">
{{ csrf_field() }}
<div class="modal-header">
Approve Goal
</div>
<div class="col-md-12">
<div class="form-group">
<label class="control-label">Comment</label>
<textarea rows="2" name="line_manager_comment" class="form-control" placeholder="Enter Comment here" value="{{old('line_manager_comment')}}" required data-validation-required-message="This field is required"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-success btn-ok">Approve Goal</button>
</div>
</form>
</div>
</div>
</div>
route/web.php
Route::get('appraisal_goals/manager_employee_goal_list/{id?}', 'Appraisal\AppraisalGoalsController#manager_employee_goal_list')->name('appraisal.appraisal_goals.manager_employee_goal_list');
Route::post('appraisal_goals/manager_employee_goal_approve/{id?}', 'Appraisal\AppraisalGoalsController#manager_employee_goal_approve')->name('appraisal.appraisal_goals.manager_employee_goal_approve');
When I submit on the modal form, it saves but couldn't redirect to:
return redirect()->route('appraisal.appraisal_goals.manager_employee_goal_list');
It displays this error
Too few arguments to function App\Http\Controllers\Appraisal\AppraisalGoalsController::manager_employee_goal_list(), 0 passed and exactly 1 expected
How do I resolve it?
Thanks
manager_employee_goal_list method expects an ID, all you have to do is to pass that ID, like so:
return redirect()->route(
'appraisal.appraisal_goals.manager_employee_goal_list',
['id' => $id]
);
Also I suggest you to either remove question mark after id in your defined route, or update arguments from your method by making them nullable
public function manager_employee_goal_list($id = null)
and of course you have to adjust code too

Cannot retrieve posted data from vue.js ajax call

I'm giving a try to Vue.js, It's much simpler than Angular and then I stuck on this problem. I'm using CodeIgniter for the backend process. Basically I just want to send data using ajax call via Vue.js then I want to retrieve the data in mmy controller. But I can't retrive the data using $this->input->post().
Here is my view. I'm using form on a modal.
<div class="modal fade modal-primary" id="modalObat" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Tambah Obat</h4>
</div>
<form #submit.prevent="onSubmit" class="form-horizontal">
<div class="modal-body">
<div class="form-group">
<label for="nama-obat" class="col-sm-2 control-label">Nama Obat</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="namaObat" placeholder="nama obat" v-model="newObat.nama">
</div>
</div>
<div class="form-group">
<label for="harga" class="col-sm-2 control-label">Harga</label>
<div class="col-sm-10">
<input type="number" class="form-control" id="harga" placeholder="harga" v-model="newObat.harga">
</div>
</div>
<div class="form-group">
<label for="Stok" class="col-sm-2 control-label">Stok</label>
<div class="col-sm-10">
<input type="number" class="form-control" id="stok" placeholder="jumlah" v-model="newObat.stok">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-outline pull-left" data-dismiss="modal">Close</button>
<button type="submit" id="simpen" class="btn btn-outline">Save Changes</button>
</div>
</form>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
This is how I send the data on vue
Then here is my controller
public function tambahObat()
{
$data = array(
'nama' => $this->input->post('nama'),
'harga' => $this->input->post('harga'),
'stok' => $this->input->post('stok')
);
$data1 = $this->input->post('newObat');
$query = $this->obat_m->save($data, null);
if( $query == true )
{
$message = $this->session->set_flashdata('message', 'berhasil menyimpan');
}
else
{
$message = $this->session->set_flashdata('message', 'berhasil mengupdate');
}
redirect('obat');
}
I succeed to send the data as I check on the console
I think there is a problem with the controller but I dont know why. Could you help me, please??
You are trying to access this from inside the post callback, where this refers to something other than your vue instance. Try this format:
this.$http.post('url/to/post',{},function(){
//because of "bind", 'this' is vue
}.bind(this));

Resources