Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Api update and delete function

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

like image 828
Mr Robot Avatar asked Jul 20 '26 20:07

Mr Robot


1 Answers

The image of postman

Please check you postman and set it like this

like image 100
Akkapong Kajornwongwattana Avatar answered Jul 22 '26 10:07

Akkapong Kajornwongwattana



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!