i'm totally new to laravel. i'm trying to POST an input from another table but in same database using Helpers. I try to get name by email using Helpers. But i got this error:
Trying to get property 'name' of non-object (View: C:\xampp\htdocs\ims\resources\views\leave\add.blade.php)
Below is my Helpers:
public static function getnamebyemail($id)
{
$userdata = DB::table('hr_intern_table')->where('id', '=', $id)->first();
if(!empty($userdata))
{
return $userdata->name;
}
return '';
}
Below is the form that i try to get and post:
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<div class="form-group form-float">
<label for="name" class="form-label">Trainee Name</label>
<div class="form-line">
<input type="text" class="form-control" name="name" disabled value="{{Helpers::getnamebyemail($data->name)}}"/>
</div>
</div>
</div>
Below is my controller:
public function add($id)
{
$data = DB::table('leave_req_tb')->where('id', '=', $id)->first();
return view('leave.add', compact('data', 'id'));
}
public function store_request(Request $request, $id)
{
$data = $this->validate($request, [
'leave_applied'=>'required',
'start_leave'=>'required',
'end_leave'=>'required',
'justification'=>'required',
'sv_approval'=>'required',
'hod_approval'=>'required',
'hr_approval'=>'required',
]);
DB::table('leave_req_tb')->where('id', '=', $id)->insert([
'intern_id'=>$intern_id,
'leave_applied'=>$request->leave_applied,
'start_leave'=>$request->start_leave,
'end_leave'=>$request->end_leave,
'justification'=>$request->justification,
'sv_approval'=>$request->sv_approval,
'hod_approval'=>$request->hod_approval,
'hr_approval'=>$request->hr_approval,
'created_at' => now(),
]);
return redirect('/leave/view')->with('success', 'The profile has been updated successfully.');
}
I appreciate if someone can help me and explain why i got this error because i don't think there is a problem with my code.
Helper functions are global and don't have a class
So in your Helpers.php
Just place the
function getnamebyemail($id)
{
$userdata = DB::table('hr_intern_table')->where('id', '=', $id)->first();
if(!empty($userdata))
{
return $userdata->name;
}
return '';
}
along with the required imports
In your blade just use
{{getnamebyemail($data->name)}}
Related
So, in my project, a user needs to register first before logging-in. I am trying to display their first, middle and last name. But, I am having a problem since multiple data are displayed on my input field. What is the correct query? Here is my controller code
public function get_first_name()
{
$first_names = UserModel::all();
$input="<input></input>";
foreach($first_names as $first_name)
{
$input.="<input value={$first_name->seq_id}>{$first_name->first_name}</input>";
}
return $input;
}
public function step1()
{
$users = UserModel::all();
$data['optStatus']=$this->get_civil_status();
$data['displayFirstName']=$this->get_first_name();
return view ("enrollment-steps.step1", $data);
}
Here is the blade file
<div class="col-sm-12 col-lg-4">
<div class="form-group row">
<label for="firstName" class="col-sm-3 text-right control-label col-form-label">First
Name</label>
<div class="col-sm-9">
<input class="form-control" type="text" id='firstName' readonly value="{!! $displayFirstName !!}" >
</div>
</div>
</div>
this is the data that it displays
There is some confusing stuff going on there, but you probably want
<input class="form-control" type="text" id='firstName' readonly value="{{ $data['displayFirstName'] }}" >
which will display the value with the key 'displayFirstName' in the $data array.
dd is your friend, use this in your blade file to see what you have in your $data variable.
{{dd($data)}}
In you controller bind the data's in a single variable, then show it in the blade
public function get_first_name()
{
$first_names = UserModel::all();
$input="<input></input>";
foreach($first_names as $first_name)
{
$input.="<input value={$first_name->seq_id}>{$first_name->first_name}</input>";
}
return $input;
}
Instead of this, do something like this:
public function get_first_name()
{
$first_names = UserModel::all();
$input="<input></input>";
foreach($first_names as $first_name)
{
$displayname = $first_name->seq_id . $first_name->first_name;
$input.="<input value="{$displayname}"</input>";
}
return $input;
}
I have a project in Laravel-5.8.
'actionURL' => route('appraisal.appraisal_goals.goal_adjustment_self_review', ['id'=>$employeeId]),
From the Notification actionUrl, I passed ['id'=>$employeeId] into:
public function goal_adjustment_self_review($id)
{
$goals = AppraisalGoal::where('employee_id', $id)->whereNull('deleted_at')->get();
return view('appraisal.appraisal_goals.goal_adjustment_self_review')->with(['goals' => $goals]);
}
Then I have this edit controller functions generated from goal_adjustment_self_review($id)
public function goal_adjustment_edit($id)
{
$userCompany = Auth::user()->company_id;
$userId = Auth::user()->employee_id;
$goal = AppraisalGoal::findOrFail($id);
return view('appraisal.appraisal_goals.goal_adjustment_edit')
->with('goal', $goal);
}
public function goal_adjustment_update(UpdateAppraisalGoalAdjustmentRequest $request, $id)
{
DB::beginTransaction();
try {
$goal = AppraisalGoal::findOrFail($id);
$goal->goal_type_id = $request->goal_type_id;
$goal->weighted_score = $request->weighted_score;
$goal->save();
DB::commit();
Session::flash('success', 'Goal Setting Weight is updated successfully');
return redirect()->route('appraisal.appraisal_goals.goal_adjustment_self_review');
} catch (Exception $exception) {
Log::error($exception);
DB::rollback();
Session::flash('error', 'Action failed! Please try again');
return redirect()->route('appraisal.appraisal_goals.goal_adjustment_self_review');
}
}
view: goal_adjustment_edit
<form action="{{route('appraisal.appraisal_goals.goal_adjustment_update', ['id'=>$goal->id])}}" method="post" class="form-horizontal" enctype="multipart/form-data">
{{ csrf_field() }}
<input name="_method" type="hidden" value="PUT">
<div class="card-body">
<div class="form-body">
<div class="row">
<div class="col-12 col-sm-6">
<div class="form-group">
<label class="control-label"> Weight(%):<span style="color:red;">*</span></label> <input type="hidden" id="goal_weight_balance" value="0" disabled>
<input id="total_weighted_score" type="text" name="weighted_score" value="{{old('weighted_score',$goal->weighted_score)}}" placeholder="Enter weighted score here" class="form-control" max="120" onkeyup="checkScore(this.value)">
</div>
</div>
</div>
</div>
</div>
<!-- /.card-body -->
<div class="card-footer">
<button type="submit" class="btn btn-primary">Update</button>
</div>
</form>
route:
Route::group(['prefix' => 'appraisal', 'as' => 'appraisal.', 'namespace' => 'Appraisal', 'middleware' => ['auth']], function () {
Route::get('appraisal_goals/goal_adjustment_self_review/{id?}', 'AppraisalGoalsController#goal_adjustment_self_review')->name('appraisal_goals.goal_adjustment_self_review');
Route::get('appraisal_goals/goal_adjustment_edit/{id?}', 'AppraisalGoalsController#goal_adjustment_edit')->name('appraisal_goals.goal_adjustment_edit');
Route::put('appraisal_goals/goal_adjustment_update/{id?}', 'AppraisalGoalsController#goal_adjustment_update')->name('appraisal_goals.goal_adjustment_update');
});
When I submitted the update form above, I got this error:
[2020-12-18 20:10:46] production.ERROR: Too few arguments to function App\Http\Controllers\Appraisal\AppraisalGoalsController::goal_adjustment_self_review(),
0 passed and exactly 1 expected
For this line:
public function goal_adjustment_self_review($id)
How do I get this resolved?
The error is very clear, you are not passing a parameter to the method. The method is called by the routing engine, which means you are not passing a route parameter that you should.
Your controller method goal_appraisal_update() includes this line (twice for some reason):
return redirect()->route('appraisal.appraisal_goals.goal_adjustment_self_review');
Where is the parameter?
What you should be doing is using route model binding so your controller method looks like this:
public function goal_adjustment_self_review($employee)
{
$goals = $employee->appraisal_goals;
return view('appraisal.appraisal_goals.goal_adjustment_self_review')
->with('goals', $goals);
}
public function goal_adjustment_edit(AppraisalGoal $goal)
{
return view('appraisal.appraisal_goals.goal_adjustment_edit')
->with('goal', $goal);
}
public function goal_adjustment_update(UpdateAppraisalGoalAdjustmentRequest $request, AppraisalGoal $goal)
{
try {
$goal->update($request->only('goal_type_id', 'weighted_score'));
Session::flash('success', 'Goal Setting Weight is updated successfully');
} catch (\Exception $exception) {
Log::error($exception);
Session::flash('error', 'Action failed! Please try again');
}
return redirect()
->route('appraisal.appraisal_goals.goal_adjustment_self_review', $goal);
}
And then routes can be defined like so:
Route::group(['prefix' => 'appraisal', 'as' => 'appraisal.', 'namespace' => 'Appraisal', 'middleware' => ['auth']], function () {
Route::get('appraisal_goals/goal_adjustment_self_review/{employee}', 'AppraisalGoalsController#goal_adjustment_self_review')
->name('appraisal_goals.goal_adjustment_self_review');
Route::get('appraisal_goals/goal_adjustment_edit/{goal}', 'AppraisalGoalsController#goal_adjustment_edit')
->name('appraisal_goals.goal_adjustment_edit');
Route::put('appraisal_goals/goal_adjustment_update/{goal}', 'AppraisalGoalsController#goal_adjustment_update')
->name('appraisal_goals.goal_adjustment_update');
});
Note the route parameters match the name of the method parameters. When type is declared in the method signature, magic happens.
//here is my controller
public function edituser(Request $request,$id){
$user=User::find($id);
$user->name=$request->name;
$user->email=$request->email;
$user->role_id=$request->role_id;
$users=User::all();
return view('edit',compact('users'));
}
//here is blade
<form action="{{route('edituser',['id'=>$user->id])}}" method="post">
#csrf
<div class="form-row align-items-center">
<div class="col-auto">
<label class="sr-only" for="inlineFormInput">Adı</label>
<input type="text" value="" class="form-control mb-2" id="inlineFormInput" placeholder="Adı">
</div>
<div class="col-auto">
<label class="sr-only" for="inlineFormInputGroup">Emaili</label>
<div class="input-group mb-2">
<input type="text" class="form-control" id="inlineFormInputGroup" placeholder="Emaili">
</div>
</div>
<select class="mdb-select md-form">
#foreach ($users as $user)
<option>--Səlahiyyət seç---</option>
<option value="1">{{$user->name}}</option>
#endforeach
</select>
//here is route
Route::get('edit/{id}','AdminController#edit')->name('edit');
Route::post('edituser/{id}','AdminController#edituser')->name('edituser');
Update: here is my all controller
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
class AdminController extends Controller
{
public function delete($id){
$delete=User::where('id',$id)->delete();
if($delete){
return redirect()-> back();
}
}
public function edit(){
$users=User::all();
return view('edit',compact('users'));
}
public function edituser(Request $request,$id){
$user=User::find($id);
$user->name=$request->name;
$user->email=$request->email;
$user->role_id=$request->role_id;
}
It seems like you are using the wrong method on the controller.
The edituser method is used by the POST route, so you should have users/compact code on the edit method instead, as that is being used by the GET route.
Move the following code from edituser and put it into edit
$users = User::all();
return view('edit',compact('users'));
Your edituser method should probably look like:
public function edituser(Request $request,$id){
$user = User::find($id);
$user->name=$request->name;
$user->email=$request->email;
$user->role_id=$request->role_id;
return view('edit',compact('user'));
}
Please update your original answer to show both methods.
public function edit($id) {
$user = User::find($id);
$users = User::all();
return view('edit')->with(compact('user', 'users'));
}
public function edituser(Request $request, $id) {
$user = User::find($id);
$user->name = $request->name;
$user->email = $request->email;
$user->role_id = $request->role_id;
$users = User::all();
return view('edit')->with(compact('user', 'users'));
}
Hope fully it will help to you easily.
I'm new to Laravel and would I'm confused on how to properly store a data from a form with a polymorphic relation. I have three models.
Salary Grade Model
public function salaryallowance() {
return $this->morphOne('App\SalaryAllowanceModel', 'salary');
}
Salary Matrix Model
public function salaryallowance() {
return $this->morphOne('App\SalaryAllowanceModel', 'salary');
}
Salary Allowance Model
public function salary() {
return $this->morphTo();
}
Salary Allowance Controller
public function create()
{
$getSalaryAllowanceWithSalary = SalaryAllowanceModel::all();
$getPosition = PositionModel::pluck('position_name', 'id');
$getSalaryGrade = SalaryGradeModel::pluck('salary_grade', 'id');
return view('SalaryAllowanceView.add', compact('getSalaryAllowanceWithSalary', 'getPosition', 'getSalaryGrade'));
}
public function store(Request $request) {
SalaryAllowanceModel::create ([
'salary_id' => $request->position,
'salary_type' => get_class($request),
'salary_allowance' => $request->allowance,
'salary_transportation' => $request->transportation,
'salary_medical' => $request->medical,
'salary_education' => $request->education,
'salary_housing' => $request->housing,
'salary_clothing' => $request->clothing
]);
return back();
}
Blade.php
<div class="form-group col-md-12 row">
<label class="col-md-4 col-form-label text-md-right">{{ __('Position') }}</label>
<div class="col-md-6">
<select name="position" class="form-control">
<option value="0">Please Select Position</option>
#foreach ($getPosition as $id => $position)
<option value="{{$id}}">{{ $position }}</option>
#endforeach
</select>
</div>
</div>
<div class="form-group col-md-12 row">
<label class="col-md-4 col-form-label text-md-right">{{ __('Allowance') }}</label>
<div class="col-md-6">
<input type="text" class="form-control" name="allowance">
</div>
</div>
I'm having a hard time to do a syntax for storing the data. I want the ID to be based on the selected drop-down.
EDIT:
I have tried using this approach but I'm getting the wrong ID to be saved in the column salary_id of Salary Allowance table. The model is properly saved in the salary_type.
public function store(Request $request)
{
$post = SectionModel::find(2);
$comment = new SalaryAllowanceModel;
$comment->salary_allowance = $request->allowance;
$comment->salary_id = $request->position;
$post->salaryallowance()->save($comment);
return back();
}
EDIT 2: SOLVED Finally I have found the solution. Posting answer for people who have the same problem
public function store(Request $request)
{
$post = SalaryMatrixModel::find($request->position);
$comment = new SalaryAllowanceModel;
$comment->salary_allowance = $request->allowance;
$comment->salary_id = $request->position;
$post->salaryallowance()->save($comment);
}
i want to save data in to table with these code
create.blade.php
<div class="row">
<div class="col-md-4"></div>
<div class="form-group col-md-4">
<label for="Name"> City Name:</label>
<input type="text" class="form-control" name="city_name">
</div>
</div>
CityController.php
public function store(Request $request)
{
$options = array();
$options->city_name=$request->get('city_name');
$attributes =array('city_name');
$table = 'cities';
(new City())->store($table,$attributes,$options);
return redirect('cities')->with('success','Information has been inserted');
}
my model is
City.php
public function store($table,$attributes,$options)
{
(new Root())->store($table,$attributes,$options);
}
with root model Root.php
public function store($table ,$attributes, $options) {
$value = array();
foreach($attributes as $key=>$data)
{
$value[$data] = $options[$key];
}
DB::table($table)->insert($value);
}
but when i push the submit button in create view give me the
ErrorException (E_WARNING)
Attempt to assign property 'city_name' of non-object
how can i solve this?
You can't use an array with the object accessor. In your controller it should be:
$options = array();
$options['city_name'] = $request->get('city_name');
$attributes = array('city_name');