getting an Undefined index: id in submitting a form - php

I got this error yesterday and I thought I fixed it. I am submitting an update form.
#extends('layouts.master')
#section('content')
<form action="{{url('/student/update')}}" method="POST" role="form">
{{ csrf_field() }}
{{method_field('PUT')}}
<legend>Create a Student</legend>
<input type="hidden" name="id" class="form-control" value="{{$student->id}}">
<div class="form-group">
<label for="">Name</label>
<input type="text" class="form-control" name="name" value="{{$student->name }}"required="required">
</div>
<div class="form-group">
<label for="">Address</label>
<input type="text" class="form-control" name="address" value="{{$student->address }}" required="required">
</div>
<div class="form-group">
<label for="">Phone</label>
<input type="text" class="form-control" name="phone" value="{{$student->phone }}" required="required">
</div>
<div class="form-group">
<label for="">Career</label>
<select name="career" class="form-control" required="required">
<option>Select a Career</option>
<option value="math"{{$student->career == 'math' ? 'selected' : ''}}>Math</option>
<option value="physics"{{$student->career == 'physics' ? 'selected' : ''}}>Physics</option>
<option value="engineering"{{$student->career == '' ? 'engineering' : ''}}>Engineering</option>
</select>
</div>
<button type="submit" class="btn btn-primary">Update Student</button>
</form>
#endsection
The error says that it relates to my ClientController on line 82.
protected function updateOneStudent($parameters)
{
$studentId = $parameters['id'];
return $this-
>performPutRequest("https://lumenapi.juandmegon.com/students/{$studentId}",
$parameters);
}
It was the same function that was giving me the problem yesterday. The problem was that I was not calling a function. The performPutRequest function is like this.
protected function performPutRequest($url, $parameters = [])
{
$contents = $this->performAuthorizeRequest('PUT', $url, $parameters);
$decodedContents = json_decode($contents);
return $decodedContents->data;
}
Any help would be appreciated.

Thanks beginner for point me in the right direction. I had the code below.
protected function updateOneStudent($parameters)
{
$studentId = $parameters['id';
return $this->performPutRequest("https://lumenapi.juandmegon.com/students/{$studentId}", $parameters);
}
I missed a bracket from the id. So it should look like this
protected function updateOneStudent($parameters)
{
$studentId = $parameters['id'];
return $this->performPutRequest("https://lumenapi.juandmegon.com/students/{$studentId}", $parameters);
}

Related

blade file : $meeting is undefined

Whenever I visit the add-meeting URL, I receive an error message stating that $meeting is undefined. However, when I attempt to edit a meeting, everything works as expected. I am using the same blade file for both creating and updating meetings. Can you explain why this error is occurring?
Here is MeetingController:
public function add_meeting()
{
$customers = Customer::all();
$projects = Project::all();
return view('admin.meeting.add-meeting', get_defined_vars());
}
public function store(Request $request)
{
$this->validate($request, [
'meeting_scedule' => 'required',
'meeting_user_id' => 'required',
'project_id' => 'required',
'agenda' => 'required',
]);
if ($request->id) {
$input['meeting_scedule'] = $request->meeting_scedule;
$input['meeting_user_id'] = $request->meeting_user_id;
$input['project_id'] = $request->project_id;
$input['agenda'] = $request->agenda;
$meeting = Meeting::where('id', $request->id)->update($input);
return back()->with('success', 'Updated Successfully!');
} else {
$new['meeting_scedule'] = $request->meeting_scedule;
$new['meeting_user_id'] = $request->meeting_user_id;
$new['project_id'] = $request->project_id;
$new['agenda'] = $request->agenda;
$meeting = new Meeting();
$meeting->persist($new);
return back()->with('success', 'Meeting Created Successfully!');
}
}
public function edit_meeting($id)
{
$customers = Customer::all();
$projects = Project::all();
$meeting = Meeting::find($id);
return view('admin.meeting.add-meeting', get_defined_vars());
}
Here is add-meeting.blade.php file:
<form action="{{url('admin/update-meeting')}}" method="POST" id="add-rel-form"
enctype="multipart/form-data">
#csrf
#if(isset($meeting))
<input class="hidden" type="hidden" name="id" value="{{$meeting->id ?? ''}}">
#endif
<div class="form-row">
<div class="form-group col-md-6 mb-0">
<label> Meeting Schedule</label>
<div class="form-group">
<input type="datetime-local" value="{{$meeting->meeting_scedule ?? ''}}" required
class="form-control" name="meeting_scedule" id="meeting_scedule">
</div>
</div>
<div class="form-group col-md-6 mb-0">
<label> User</label>
<select id="customer-select" class='form-control' required name="meeting_user_id">
<option value="" id="">--Select User--</option>
#foreach($customers as $customer)
<option value="{{$customer->id}}"{{ $customer->id == $meeting->meeting_user_id ? 'selected' : '' }}>{{$customer->first_name." ".$customer->last_name}}</option>
#endforeach
</select>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6 mb-0">
<label> Project</label>
<select id="project-select" class='form-control' required name="project_id" disabled>
<option value="" id="">--Select Project--</option>
#foreach($projects as $project)
<option value="{{$project->id}}"{{$project->id == $meeting->project_id ? 'selected' : ''}}>{{$project->project_type}}</option>
#endforeach
</select>
</div>
<div class="form-group col-md-6 mb-0">
<label> Agenda</label>
<div class="form-group">
<textarea type="text" value="" required
class="form-control" name="agenda" id="agenda">{{$meeting->agenda ?? ''}}</textarea>
</div>
</div>
</div>
Since there is no $meeting variable when creating a new meeting, the error occurs.
You can use null coalescing operator (??) or ternary operator (?:) to check if $meeting is defined before attempting to access its properties :
#if(isset($meeting))
<input class="hidden" type="hidden" name="id" value="{{$meeting->id}}">
#endif
...
<select id="customer-select" class='form-control' required name="meeting_user_id">
<option value="" id="">--Select User--</option>
#foreach($customers as $customer)
<option value="{{$customer->id}}" {{ isset($meeting) && $customer->id == $meeting->meeting_user_id ? 'selected' : '' }}>{{$customer->first_name." ".$customer->last_name}}</option>
#endforeach
</select>
...
<select id="project-select" class='form-control' required name="project_id" disabled>
<option value="" id="">--Select Project--</option>
#foreach($projects as $project)
<option value="{{$project->id}}"{{ isset($meeting) && $project->id == $meeting->project_id ? 'selected' : ''}}>{{$project->project_type}}</option>
#endforeach
</select>
...
<textarea type="text" value="" required class="form-control" name="agenda" id="agenda">{{ isset($meeting) ? $meeting->agenda : '' }}</textarea>

Missing required parameter for [Route: admin.request.update] [URI: admin/request/{request}] [Missing parameter: request]

I got an error when I tried to access detail.blade.php that said "Missing required parameter for [Route: admin.request.update] [URI: admin/request/{request}] [Missing parameter: request].". I don't know where did I do wrong, because I copied the steps and codes exactly like my other project that did the same thing (editing data).
Here is my detail.blade.php :
<form action="{{ route('admin.request.update', $requestStock) }}" method="POST">
#csrf
{{ method_field('PUT') }}
<div class="form-group row">
<label for="name" class="col-sm-2 col-form-label">Name</label>
<div class="col-sm-10">
<textarea class="form-control" id="name" name="name" rows="3" readonly value="{{ $requestStock->name }}"></textarea>
</div>
</div>
<div class="form-group row">
<label for="status" class="col-sm-2 col-form-label">Complete</label>
<div class="col-sm-10">
<select class=" form-control" name="status" id="status">
<option name="status" value="1" {{ $requestStock->status == '1' ? 'selected' : ''}}> Not Completed </option>
<option name="status" value="0" {{ $requestStock->status == '0' ? 'selected' : ''}}> Complete </option>
</select>
</div>
</div>
<button type="submit" class="btn btn-secondary" style="margin-top: 20px; width: 100%">Update</button>
</form>
And this is my RequestPageController :
public function edit(RequestStock $requestStock)
{
return view('admin.request.detail')->with([
'requestStock' => $requestStock,
]);
}
public function update(Request $request, RequestStock $requestStock)
{
$requestStock->status = $request->status;
$requestStock->save();
return redirect()->route('admin.request.index');
}
Route :
Route::namespace("App\Http\Controllers\Admin")->prefix("admin")->name("admin.")->middleware('can:adminpage')->group(function () {
Route::resource("/request", RequestPageController::class);
});
Thank you.
Try the following changes:
Changed Route:
Route::namespace("App\Http\Controllers\Admin")->prefix("admin")->name("admin.")->middleware('can:adminpage')->group(function () {
Route::resource("/request-stock", RequestPageController::class);
});
Changed RequestPageController:
public function edit(RequestStock $requestStock)
{
return view('admin.request-stock.detail')->with([
'requestStock' => $requestStock,
]);
}
public function update(Request $request, RequestStock $requestStock)
{
$requestStock->status = $request->status;
$requestStock->save();
return redirect()->route('admin.request-stock.index');
}

ErrorException Trying to get property of non-object laravel

umm hello.
Im new in laravel and i want to create a program for list of workers.
I'm trying to access this route:
http://127.0.0.1:8000/posts/create
and there's error message.
ErrorException
Trying to get property '{"role":"pegawai","name":"asdasdasdasd1","email":"1asdad#ifocaproject.com","updated_at":"2020-05-11T18:26:31.000000Z","created_at":"2020-05-11T18:26:31.000000Z","id":7}' of non-object
this is my controller.
public function create(Request $request)
{
$user = new \App\User;
$user->role = 'pegawai';
$user->name = $request['nama_pegawai'];
$user->email = $request['email'];
$user->password = bcrypt('rahasia');
$user->remember_token = Str::random(60);
$user->save();
$request ->request->add(['user_id'-> $user->id]);
$pegawai = \App\Pegawai::create($request->all());
return redirect('/pegawai')->with('sukses','Data Berhasil Di-input');
}
and this is my blade.
<div class="modal-body">
<form action="/pegawai/create" method="POST">
{{ csrf_field() }}
<div class="form-group">
<label for="exampleFormControlInput1">Nama Pegawai</label>
<input name="nama_pegawai" type="text" class="form-control"
id="exampleFormControlInput1" placeholder="Joni">
</div>
<div class="form-group">
<label for="exampleFormControlInput1">Email</label>
<input name="email" type="text" class="form-control"
id="exampleFormControlInput1" placeholder="eve#ifocaprojec.com">
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">Jenis Kelamin</label>
<select name="jenis_kelamin" class="form-control" id="exampleFormControlSelect1">
<option value="Laki-Laki">Laki-laki</option>
<option value="Perempuan">Perempuan</option>
<option value="-none-">-none-</option>
</select>
</div>
<div class="form-group">
<label for="exampleFormControlInput1">Umur</label>
<input name="umur" type="text" class="form-control" placeholder="Cth:21">
</div>
<div class="form-group">
<label for="exampleFormControlInput1">Agama</label>
<input name="agama" type="text" class="form-control" placeholder="Cth:Islam">
</div>
<div class="form-group">
<label for="exampleFormControlTextarea1">Alamat</label>
<textarea name="alamat" class="form-control" rows="3"></textarea>
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">Divisi</label>
<select name="divisi" class="form-control">
<option value="Inbound">Inbound</option>
<option value="Outbound">Outbound</option>
</select>
</div>
</div>
table user:
table pegawai:
What Am I missing? Any help would be greatly appreciated, Thanks.
And I'm sorry, I'm not very good at English.
You may have typo in code.
Here it must be => instead of ->:
$request->request->add(['user_id' => $user->id]);
I believe you need to reload data from DB after ->save():
.......
$user->save();
$request ->request->add(['user_id'-> $user->fresh()->id]);

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]) }}"

(1/1) ErrorException Undefined index: id in ClientController.php (line 81)

I am not sure why I am getting this error. Here is the method in the ClientController.
protected function updateOneStudent($parameters)
{
$studentId = $parameters['id'];
return $this- >performPutRequest("https://lumenapi.juandmegon.com/students/{$studentId}", $parameters);
}
Basically I am trying to update a selected student. Below is the update form.
#extends('layouts.master')
#section('content')
<form action="{{url('/student/update')}}" method="POST" role="form">
{{ csrf_field() }}
{{method_field('PUT')}}
<legend>Create a Student</legend>
<div class="form-group">
<label for="">Name</label>
<input type="text" class="form-control" name="name" value="{{$student->name }}"required="required">
</div>
<div class="form-group">
<label for="">Address</label>
<input type="text" class="form-control" name="address" value="{{$student->address }}" required="required">
</div>
<div class="form-group">
<label for="">Phone</label>
<input type="text" class="form-control" name="phone" value="{{$student->phone }}" required="required">
</div>
<div class="form-group">
<label for="">Career</label>
<select name="career" class="form-control" required="required">
<option>Select a Career</option>
<option value="math"{{$student->career == 'math' ? 'selected' : ''}}>Math</option>
<option value="physics"{{$student->career == 'physics' ? 'selected' : ''}}>Physics</option>
<option value="engineering"{{$student->career == '' ? 'engineering' : ''}}>Engineering</option>
</select>
</div>
<button type="submit" class="btn btn-primary">Update Student</button>
</form>
#endsection
The request I was sending was wrong. The error was in the StudentController.
I had
public function getUpdateStudent()
{
$students = $this->obtainAllStudents;
return view('students.select-student', ['students'=> $students]);
}
It it should have been
public function getUpdateStudent()
{
$students = $this->obtainAllStudents();
return view('students.select-student', ['students'=> $students]);
}
I missed the brackets to call the getUpdateStudent. Sorry guys I did not show this code earlier.

Categories