blade file : $meeting is undefined - php

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>

Related

How to import excel file into two tables with input tags at the same time?

I have two table one is users and the next one is students, the students table store the data of users based on their role. and the relation between user and student is user_id in students table.
And the form includes three inputs for text and one for excel file to import. I want to import the users by file and their data by inputs.
my form is like this:
<form method='post' action="{{ route('importExcel') }}" enctype="multipart/form-data">
#csrf
<div class="box-body row">
<div class="col-md-12">
<label for="faculty" class="col-md-12">فاکولته</label>
<div class="form-group">
<select name='faculty' class="form-control" id="faculty">
<option value=""> انتخاب فاکولته </option>
#foreach($faculties as $faculty)
<option value="{{ $faculty->id }}" >{{ $faculty->name }}</option>
#endforeach
</select>
</div>
</div>
<div class="col-md-12">
<label for="department" class="col-md-12">دیپارتمنت</label>
<div class="form-group">
<select class="form-control" name="department" id="department">
<option value="">انتخاب دیپارتمنت</option>
#foreach($departments as $department)
<option value="{{ $department->id }}">{{ $department->name }}</option>
#endforeach
</select>
</div>
</div>
<div class="col-md-12">
<label for="sesson" class="col-md-12">فصل</label>
<div class="form-group">
<select name="sesson" id="sesson" class="form-control">
<option value="">انتخاب فصل</option>
<option value="spring">بهار</option>
<option value="fall">خزان</option>
</select>
</div>
</div>
<div class="col-md-12">
<label for="file" class="col-md-12">فایل اکسل</label>
<div class="form-group">
<input type="file" name="file" class="form-control" id="file">
</div>
</div>
<input type="hidden" name="role" value="user">
<input type="hidden" name="admin" value="admin">
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" class="btn btn-info pull-left submit">تایید</button>
</div>
<!-- /.box-footer -->
</form>
and my import class is like this:
class UsersImport implements ToCollection
{
public function collection(Collection $rows)
{
foreach ($rows as $row)
{
User::create([
'name' => str_replace(' ', '_', $row['name']),
'email' => $row['email'],
'password' => bcrypt(str_replace(' ', '_', $row['name'])),
'role' => 'user',
]);
Student::create([
'user_id' => DB::table('users')->getPdo()->lastInsertId(),
'department_id' => $row['department'],
'faculty_id' => $row['faculty'],
'sesson' => $row['sesson']
]);
}
}
}
and my controller is like this:
try{
$this->validate($request,[
'faculty' => 'required',
'department'=> 'required',
'sesson'=> 'required',
'file'=> 'required',
]);
$file = $request->file('file');
$faculty = $request-> faculty;
$department = $request -> department;
$sesson = $request -> sesson;
Excel::import(new UsersImport,[$file, $faculty, $department, $sesson]);
return redirect()->route('admin.students')->with('success','عملیات موفقانه انجام شد.');
}
catch(Exception $e){
return redirect()->back()->with('error','عملیات انجام نشد!');
}
but its not working, the error is like this: pathinfo() expects parameter 1 to be string, array given

How can I fetch record drom database in edit view In Laravel?

In my edit view I have code like this
<div class="form-group">
<label class="col-md-12">Last Name</label>
<div class="col-md-12">
<input type="text" placeholder="Enter Last Name" name="lastName" class="form-control form-control-line" value="{{$profile->personal_detail['last_name']}}" required>
</div>
</div>
<div class="form-group">
<label class="col-md-12">Department</label>
<div class="col-md-12">
<select class="custom-select form-control col-md-11" id="department" name="department">{{ $profile->personal_profile['department'] }}
#foreach($listDepartment as $departmentList){
<option value='{{$departmentList->nameOfDepartment}}'>{{$departmentList->nameOfDepartment}}</option>
}
#endforeach
</select>
</div>
</div>
In edit view my last name field it gives me last name from database, in department it display me drop down of department but I want that inserted name of department in that field.
how can i get it??
I have other drop down like this
<div class="row">
<label class="col-md-6"><b> Mode </b></label>
<div class="col-md-6">
<select class="custom-select form-control col-md-12" name="mode" id="mode" required>
<option value=""> --- Select Interciew Mode --- </option>
<option value="telephonic">Telephonic</option>
<option value="facetoface">Face 2 face</option>
<option value="skype">Skype</option>
</select>
</div>
</div><hr>
This is my controller
public function candidateDetail($id)
{
$empDetails = User::all();
$candidateDetail = EmployeeHire::find($id);
$interview = [
'' => '--- Select Interciew Mode ---',
'telephonic' => 'Telephonic',
'facetoface' => 'Face 2 face',
'skype' => 'Skype'
];
return view('pages.candidatedetails', compact('id', 'candidateDetail', 'empDetails', 'interview'));
}
You can check in your foreach if the value of nameOfDepartment match the one from your user.
<div class="form-group">
<label class="col-md-12">Department</label>
<div class="col-md-12">
<select class="custom-select form-control col-md-11" id="department" name="department">
#foreach($listDepartment as $departmentList)
#if ($profile->personal_profile['department'] == $departmentList->nameOfDepartment)
<option value="{{$departmentList->nameOfDepartment}}" selected="selected">{{$departmentList->nameOfDepartment}}</option>
#else
<option value="{{$departmentList->nameOfDepartment}}">{{$departmentList->nameOfDepartment}}</option>
#endif
#endforeach
</select>
</div>
</div>
For your second select field, create an array with all possible values in your controller.
$interview = [
'' => '--- Select Interciew Mode ---',
'telephonic' => 'Telephonic',
'facetoface' => 'Face 2 face',
'skype' => 'Skype'
];
Then you can do the same as your previous select :
<select class="custom-select form-control col-md-12" name="mode" id="mode" required>
#foreach($interview as $key => $name)
#if ($profile->personal_profile['interview'] == $key)
<option value="{{ $key }}" selected="selected">{{ $name }}</option>
#else
<option value="{{ $key }}">{{ $name }}</option>
#endif
#endforeach
</select>

getting an Undefined index: id in submitting a form

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);
}

(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.

Highlight multiple select options after validations fails - Laravel 5.2

I'm learning Laravel 5.2 for first time, and I need to repopulate a form when the validation fails.
I was able to do so with single inputs as 'name' or 'description', but I couln't find out how to do it with the 'category' which is a multiple select.
View:
<form action="{{ url('addproduct') }}" method="POST">
{!! csrf_field() !!}
<div class="form-group">
<label for="#productName">Name:</label>
<input id="productName" class="form-control" name="name" type="text" placeholder="Product name" value="{{ old('name') }}">
</div>
<div class="form-group">
<label for="#productDescription">Description:</label>
<textarea id="productDescription" class="form-control" name="description" rows="3" placeholder="Product description" value="{{ old('description') }}"></textarea>
</div>
#if ($categories)
<div class="form-group">
<label for="#productCategory">Category:</label>
<select id="productCategory" class="form-control" name="category[]" multiple>
#foreach ($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
</select>
</div>
#endif
<div class="form-group">
<label for="#productQuantity">Quantity:</label>
<input id="productQuantity" class="form-control" name="quantity" type="number" min="0" value="0">
</div>
<button class="form-control btn btn-success" type="submit">Add Product</button>
</form>
Controller:
public function add(Request $request) {
$data = array();
$data['msgAdded'] = false;
$data['categories'] = Category::select('name', 'id')->get();
if ($request->isMethod('post')) {
$this->validate($request, [
'name' => 'required|max:50',
'description' => 'required|max:200',
'category' => 'required',
'quantity' => 'required|min:0',
]);
$product = new Product;
$product->name = $request->name;
$product->description = $request->description;
$product->quantity = $request->quantity;
$product->save();
foreach ($request->category as $cat) {
$category = Category::find($cat);
$category->products()->attach($product->id);
}
$data['msgAdded'] = true;
}
$data['view'] = 'addproduct';
return view('products/add', $data);
}
I would like to know if it can be achieve with blade, or the best way to achieve that.
Thank you!
Option 1
You would have to write the select part of your form like this:
<div class="form-group">
<label for="#productCategory">Category:</label>
<select id="productCategory" class="form-control" name="category[]" multiple>
#foreach ($categories as $category)
<option value="{{ $category->id }}"{{ !empty(old('category')) && in_array($category->id, old('category')) ? ' selected="selected"' : '' }}>{{ $category->name }}</option>
#endforeach
</select>
</div>
Option 2
And arguably the more elegant solution is to install the html package from the laravel collective
composer require laravelcollective/html
follow the installation instructions on the laravel collective Forms & HTML documentation.
then just enter this where your select would be:
{!! Form::select('category[]', $categories, old('category'),['class' => 'form-control', 'multiple' => 'multiple'] ) !!}

Categories