ErrorException Trying to get property of non-object laravel - php

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

Related

Data is not displayed in view Laravel

I am trying to display data from database to detail.blade.php, but no data is displayed in the view.
Here is the detail.blade.php view:
<h6 class="card-title">Order No. {{ $order->order_number }}</h6>
<form>
<div class="form-group">
<label for="date">Date</label>
<input type="text" class="form-control" name="order_date" value="{{ $order->order_date}}" readonly>
</div>
<div class="form-group">
<label for="handle">Handle by</label>
<input type="text" class="form-control" value="." readonly>
</div>
<div class="form-group">
<label for="status">Status</label>
<input type="text" class="form-control" name="status" value="{{ $order->status}}" readonly>
</div>
<div class="form-group">
<label for="subtotal">Subtotal</label>
<input type="text" class="form-control" name="billing_subtotal" value="{{ $order->billing_subtotal}}" readonly>
</div>
</form>
OrderMenuController:
public function show(Order $order)
{
$data = DB::table('order_menu')
->join('menus', 'menus.id', '=', 'order_menu.menu_id')
->join('orders', 'orders.id', '=', 'order_menu.order_id')
->select('orders.*', 'menus.name', 'order_menu.quantity')
->where('orders.id', $order->id)
->get();
return view('admin.order.detail')->with([
'order' => $order,
'data' => $data,
]);
}
And for the route:
Route::namespace("App\Http\Controllers\Admin")->prefix("admin")->name("admin.")->middleware('can:adminpage')->group(function () {
Route::resource("/ordermenu", OrderMenuController::class);
});
Tried dd($order) in the controller, and this is what comes up:
How to solve this? thank you
your dd() shows that you don't have any attributes for the order instance, so it makes sense that you got any result back
Try To change the resource route into orders it might work for you

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

Laravel 5.2 Method Not Allowed Exception on Production Server

I've build a simple from in laravel which takes some data and stores it in the DataBase.
Problem occurred when i uploaded my code on production server.
In development and local it's working perfectly, but when i try to submit the same form on production server it throws following exception:
MethodNotAllowedHttpException in RouteCollection.php line 218:
I've already checked my form and route methods both are post. I'm lost in it.
routes Code
Route::get('/', function () {
return view('welcome');
});
Route::group(['middleware' => ['web']], function () {
Route::get('auth/register', 'Auth\AuthController#register');
Route::post('/ajax-registration','RegistrationController#registration');
Route::post("/signup", "Api\ApplicantRegistrationController#registration");
Route::auth();
Route::get('/home', 'HomeController#index');
Route::get('/roles/{id}',"HomeController#roles");
Route::post('/complete-profile', 'HomeController#completeProfile');
});
Controller Code (the function which i am calling from route)
public function completeProfile(Request $request){
If(Input::hasFile('file')){
$file = Input::file('file');
$cDate = date("d-m-Y_H:i:s");
$destinationPath = public_path(). '/uploads/';
$filename = $cDate."_".$file->getClientOriginalName();
$file->move($destinationPath, $filename);
}
$insert = DB::table('Applicant')->insert(
[
'name' => Auth::user()->name,
'email' => Auth::user()->email,
'total_experience' => $request->input('int_exp'),
'functional_area' => $request->input('txtFunctionalArea'),
'current_role' => $request->input('txtRole'),
'current_company' => $request->input('company'),
'desired_role' => $request->input('txtRole2'),
'file' => $filename
]);
\DB::table('users')->where('email', Auth::user()->email)->update(['allInformationReceived' => 1]);
if($insert){
return redirect('/home');
}
else{
return 'Some Error';
}
}
form
<form id="signupform" method="POST" action="complete-profile" class="mob-pad0" enctype="multipart/form-data" style="padding: 20px 250px;">
<div class="col-md-6 col-xs-12 mob-pad0 padd-right60">
<div class="form-horizontal">
<div class="form-group">
<label for="int_exp">Total Experience</label>
<select class="form-control border-radius0" name="int_exp" id="int_exp" tabindex="2" required="required">
<option value="">Select</option>
Dropdown Using Ajax
<option value="15+">15+</option>
</select>
</div>
<div class="form-group">
<label for="txtFunctionalArea">Functional Area</label>
<select class="form-control border-radius0" name="txtFunctionalArea" id="txtFunctionalArea" tabindex="3" required="required">
<option value="">Select</option>
<option value="32">IT Software- Application Programming / Maintenance </option>
<option value="37">IT Software- Network Administration / Security </option>
</select>
</div>
<div class="form-group">
<label for="txtRole2">Desired Role</label>
<select class="form-control border-radius0" name="txtRole2" id="txtRole2" tabindex="6" required="required">
<option value="">Select</option>
Dropdown Using Ajax
</select>
</div>
</div>
</div>
<div class="col-md-6 col-xs-12 mob-pad0 padd-right60">
<div class="form-horizontal">
<div class="form-group">
<label for="company">Current Company</label>
<input name="company" id="company" type="text" placeholder="Current Company" class="form-control border-radius0" required="required" tabindex="5"/>
</div>
<div class="form-group">
<label for="txtRole">Current Role</label>
<select class="form-control border-radius0" name="txtRole" id="txtRole" tabindex="4" required="required">
<option value="">Select</option>
Dropdown Using Ajax
</select>
</div>
<div class="form-group">
<label for="resume">Resume (PDF/DOC)</label>
<input type="file" name="file" id="resume" class="form-control border-radius0" required="required" accept=".pdf,.doc, .docx" onchange="validate_fileupload(this);">
</div>
</div>
</div>
<input type="hidden" id="csrfToken" name="_token" value="{{ csrf_token() }}">
<div class="col-sm-12 col-xs-12 mob-pad0 text-center padd30">
<input type="submit" id="submitbtn_2" value="Get Started" class="inputButton btn btn-success"/>
</div>
</form>
Any help will be appreciated, thanks.
Thanks Guys for your inputs, i got the solution on the following URL:
MethodNotAllowedException
And made two changes in my code:
one in route, changed method to any
and one in form, changed post method to PUT.

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.

Categories