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

The image of postman

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

Share:
14,408
Mr Robot
Author by

Mr Robot

Updated on June 04, 2022

Comments

  • Mr Robot
    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
      MounirOnGithub over 7 years
      It 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
      Daan Meijer over 7 years
      You also might want to look into $ulesson->fill($request->all()); :)
    • Mr Robot
      Mr Robot over 7 years
      i'm sry i'm not getting what exactly i should do
    • Manoz Biswas
      Manoz Biswas over 7 years
      It 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
      DevK over 7 years
      Check 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
    Mr Robot over 7 years
    i'm getting output `null``
  • Manoz Biswas
    Manoz Biswas over 7 years
    Yes, 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
    Akkapong Kajornwongwattana over 7 years
    in your postman you add the parameter title body and completed in body form-data ?
  • Manoz Biswas
    Manoz Biswas over 7 years
    If possible can you show me the screenshot of the postman screen or the form you are submitting
  • Manoz Biswas
    Manoz Biswas over 7 years
    you should do dd($request->all())
  • Akkapong Kajornwongwattana
    Akkapong Kajornwongwattana over 7 years
    dd($request) it will show class of request not error
  • Manoz Biswas
    Manoz Biswas over 7 years
    @Mr Robot please check your lessons table to find the id 11 exist or not
  • Mr Robot
    Mr Robot over 7 years
    sir that's a POST request it's working fine the data is inserting to database but how to perform update
  • Akkapong Kajornwongwattana
    Akkapong Kajornwongwattana over 7 years
    Ok you just change method in postman from POST to PUT or PATCH
  • Mr Robot
    Mr Robot over 7 years
    my bad forgot to import db
  • Manoz Biswas
    Manoz Biswas over 7 years
    you have not any Model in App directory named lesson that you have used by using namespace in your controller