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

I got this errror when trying to update data. If I update the image there's no error, but if I'm not it showing Call to a member function move() on null
This is my code:
public function update($id, Request $request)
{
$change = Profile::findorfail($id);
$before = $change->foto_profil;
$profile = [
'fullname' => $request['fullname'],
'phone' => $request['phone'],
'ttl' => $request['ttl'],
'foto_profil' => $before
];
$request->foto_profil->move(public_path().'/image', $before);
$change->update($profile);
return redirect('/profil');
}

You may determine if a file is present on the request using the hasFile() method :
if($request->hasFile('foto_profil')){
$request->foto_profil->move(public_path().'/image', $before);
}
See the documentation here

just add validation if photo exists in request
if($request->foto_profil) {
$request->foto_profil->move(public_path().'/image', $before);
}

Related

Laravel how to pass request from controller to resource collection?

I will pass a parameter in the request. The query won't change. How can I pass the request to SurveyResouce
public function getAllSurveys(\Illuminate\Http\Request $request) {
$surveys = DB::table('surveys')
->select('id', 'survey_name')
->get();
return response()->json([
'error' => false,
'data' => SurveyResource::collection($surveys)
],
}
I want get the request parameters in the resource
public function toArray($request) {
$controller = new SurveyController(new SurveyRepository());
return [
'question' => $request->has('ques') ? $request->input('ques'):'',
];
}
You can try by directly using request() helper function. Like request('parameter');

Route Model binding fails when using Form request

The Route model binding fails when I use the FormRequest to validate the request. To be more specific, when I try to access the parameters in the form request and give the following error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name:\"moniersa\"' in 'where clause'
(SQL: select count(*) as aggregate from `users` where `email` = a.monier2130#gmail.com and
`name:\"moniersa\"` <> {\"id\":5 and `email:\"a`.`monier2130#gmail`.`com\"` =
email_verified_at:null and `admin:1` = verified:0 and `created_at:\"2020-02-11 22:33:33\"` =
updated_at:\"2020-02-11 23:17:40\"})
The code of the update method:
public function update(User $user, UpdateUserRequest $request)
{
$data = $this->extract_data($request);
$user->update($data);
return response()->json(['data' => $user], 200);
}
The code of the rules function in the Update request:
public function rules()
{
return [
'name' => 'required|string|min:3|max:40',
'email' => 'required|email|unique:users,email,' . $this->user,
'password' => 'required|string|min:9|confirmed'
];
}
Whenever, I remove the User hint from the update method or $this->user from the update request file, everything works fine. Any clue about what is the problem is?
Route model binding means you do not need to use $user = User::findOrFail($user); anymore,
public function update(User $user, UpdateUserRequest $request)
{
$user = User::findOrFail($user);// this line is redundant, you can remove it.
$data = $this->extract_data($request);
$user->update($data);
return response()->json(['data' => $user], 200);
}

assertJson and assertSee show call to a null member error

I trying to develop a test for my APIs,
This is my code:
public function testFirstAPI()
{
$user = \User::find(1);
$r = $this
->actingAs($user)
->json('put', route('updateUser'),['lock' => 'true']);
$r->assertResponseStatus(200)->seeJson(['success' => true]);
}
this test will work and when I use $r->dump() I can found the success in the response.
but I don't know why seeJson show this error:
Symfony\Component\Debug\Exception\FatalErrorException]
Call to a member function assertJson() on null
This is because you are chaining assertResponseStatus() first and it does not return a fluent object.
A solution would be to put it as the last assertion in the chain:
public function testFirstAPI()
{
$user = \User::find(1);
$this->actingAs($user)
->json('put', route('updateUser'), ['lock' => 'true'])
->seeJson(['success' => true])
->assertResponseStatus(200)
}

How To Create Conditional for Unique Validation (UPDATE/PATCH) on Form Request

I'm trying to get my controller cleaner by moving 'validation request' into a form request called 'BookRequest'.
The problem is on the update process, I try to create a condition to check, if it PATCH or POST with the following codes
MyRequest.php
public function rules()
{
// Check Create or Update
if ($this->method() == 'PATCH')
{
$a_rules = 'required|string|size:6|unique:books,column2,' .$this->get('id');
$b_rules = 'sometimes|string|size:10|unique:books,column3,' .$this->get('id');
}
else
{
$a_rules = 'required|string|size:6|unique:books,column2';
$b_rules = 'sometimes|string|size:10|unique:books,column3';
}
return [
'column1' => 'required|string|max:100',
'column2' => $a_rules,
'column3' => $b_rules,
'column4' => 'required|date',
'column5' => 'required|in:foo,bar',
'column6' => 'required',
'column7' => 'required',
'column8' => 'required',
];
}
.$this->get('id') it failed, the form still treat the unique on the update.
Controller#update
public function update($id, BookRequest $request)
{
$book = Book::findOrFail($id);
$input = $request->all();
$book->update($request->all());
return view('dashboards.book');
}
Controller#edit
public function edit($id)
{
$book = Book::findOrFail($id);
return view('dashboards.edit', compact('book'));
}
Controller#create
public function create()
{
return view('dashboards.create');
}
Controller#store
public function store(BookRequest $request)
{
$input = $request->all();
$book = Book::create($input);
return redirect('dashboards/book/index');
}
I try the alternative .$book->id, and it throw me an ErrorException Undefined variable: book
Any suggestion? I'm using Laravel 5.2 by the way
You are using book as your route parameter but trying to get with id. try this-
if ($this->method() == 'PATCH')
{
$a_rules = 'required|string|size:6|unique:books,column2,' .$this->route()->parameter('book');
$b_rules = 'sometimes|string|size:10|unique:books,column3,' .$this->route()->parameter('book');
}
Hope it helps :)

How can i edit and delete a page in laravel 5.2?

When i try to edit and delete an employee,an error will shows.
For delete
Missing argument 1 for App\Http\Controllers\Admin\CreateEmployeeController::deleteemployee()
For edit
Missing argument 1 for App\Http\Controllers\Admin\CreateEmployeeController::editemployee()
Methods:
public function editemployee($id)
{
$employee = CreateEmployee::where('id',$id)->get();
return view('app.admin.employee.editemployee',compact('employee'));
}
public function updateemployee(Request $request)
{
CreateEmployee::where('id',$request->id)->update(array('username'=>$request->username,'area'=>$request->area_name));
Session::flash('flash_notification', array('level' => 'success', 'message' => 'channel details updated successfully'));
return Redirect::action('Admin\CreateEmployeeController#addemployee',array('id' => $request->id));
}
public function deleteemployee($id)
{
$employee = CreateEmployee::where('id',$id)->get();
return view('app.admin.employee.deleteemployee',compact('employee'));
}
public function deleteconfirms($id)
{
$employee = CreateEmployee::where('id',$id)->delete();
Session::flash('flash_notification', array('level' => 'success', 'message' => 'employee deleted successfully'));
return Redirect::action('Admin\CreateEmployeeController#addemployee');
}
As I can see that your methods deleteemployee , deleteconfirms and editemployee are expecting the id field.
While in your route you are not using any "Route parameters" (for details see Route Parameters ).
So, change your routes to include Route Parameters as follows. where id represents employee_id
Route::get('edit-employee/{id}','CreateEmployeeController#editemp‌​loyee');
Route::post('update-employee','CreateEmployeeController#upd‌​ateemployee');
Route::get('delete-employee/{id}','CreateEmployeeController#delet‌​eemployee');
Route::post('delete-confirms/{id}','CreateEmployeeController#dele‌​teconfirms');

Resources