Laravel Api update and delete function
14,408
Solution 1
i just downloaded Insomnia and tested every thing is working fine as expected
i don't know why it's not working in postman though
Solution 2
Please check you postman and set it like this
Solution 3
In update can you dd($request->input('title')) in line 69 I think you don't sent the value of title
and in delete I think you have no value in id field
Author by
Mr Robot
Updated on June 04, 2022Comments
-
Mr Robot about 2 years
I'm building an API, i'm getting the following error while updating and deleting from table i'm using postman to test my api
//update error QueryException in Connection.php line 770: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'title' cannot be null (SQL: update `lessons` set `title` = , `body` = , `completed` = , `updated_at` = 2017-01-03 09:14:10 where `id` = 11) //delete error FatalErrorException in LessonsController.php line 80: Call to a member function delete() on null
My controller LessonsController
<?php namespace App\Http\Controllers; use Response; use App\lesson; use Illuminate\Http\Request; use App\Acme\Transformers\LessonTransformer; use Illuminate\Support\Facades\Input; class LessonsController extends ApiController { protected $lessonTransformer; function __construct(LessonTransformer $lessonTransformer) { $this->lessonTransformer = $lessonTransformer; } //fetch all and pass a metadata 'data' public function index() { $lessons = Lesson::all(); return $this->respond([ 'data' => $this->lessonTransformer->transformCollection($lessons->all()) ]); } //fetch by id public function show($id) { $lesson = Lesson::find($id); if(! $lesson) { return $this->respondNotFound(); } return $this->respond([ 'data' => $this->lessonTransformer->transform($lesson) ]); } public function store() { if (! input::get('title') or ! input::get('body')) { return $this->respondBadRequest(); } Lesson::create(input::all()); return $this->respondCreated(); } public function update(Request $request, $id) { $ulesson = Lesson::find($id); $ulesson->title = $request->input('title'); $ulesson->body = $request->input('body'); $ulesson->completed = $request->input('completed'); $ulesson->save(); return "Sucess updating user #" . $ulesson->id; } public function destroy(Request $request) { $dlesson = Lesson::find($request->input('id')); $dlesson->delete(); return "Employee record successfully deleted #" . $request->input('id'); } }
my model Lesson
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Lesson extends Model { protected $fillable = ['title', 'body', 'completed',]; //protected $hidden =['title']; }
the store and other functions are working fine
Thank You
-
MounirOnGithub over 7 yearsIt looks like your
$ulesson->title
is not set in your update function and all those values might be required to execute your update request -
Daan Meijer over 7 yearsYou also might want to look into
$ulesson->fill($request->all());
:) -
Mr Robot over 7 yearsi'm sry i'm not getting what exactly i should do
-
Manoz Biswas over 7 yearsIt seems that within the destroy function $dlesson = Lesson::find($request->input('id')); does not find anything to delete. You can check if($dlesson) then $dlesson->delete();
-
DevK over 7 yearsCheck out Laravel form request validation. I find this to be one of the cleanest solutions for validaiton. You could have Request validation check that all the required fields are set and this way the controller will never get executed if validation is not passed.
-
-
Mr Robot over 7 yearsi'm getting output `null``
-
Manoz Biswas over 7 yearsYes, for this null value it does not find anything to delete, please check which input value you are passing from the form input
-
Akkapong Kajornwongwattana over 7 yearsin your postman you add the parameter title body and completed in body form-data ?
-
Manoz Biswas over 7 yearsIf possible can you show me the screenshot of the postman screen or the form you are submitting
-
Manoz Biswas over 7 yearsyou should do dd($request->all())
-
Akkapong Kajornwongwattana over 7 yearsdd($request) it will show class of request not error
-
Manoz Biswas over 7 years@Mr Robot please check your lessons table to find the id 11 exist or not
-
Mr Robot over 7 yearssir that's a POST request it's working fine the data is inserting to database but how to perform update
-
Akkapong Kajornwongwattana over 7 yearsOk you just change method in postman from POST to PUT or PATCH
-
Mr Robot over 7 yearsmy bad forgot to import db
-
Manoz Biswas over 7 yearsyou have not any Model in App directory named lesson that you have used by using namespace in your controller