Reputation: 897
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
Upvotes: 3
Views: 19920
Reputation: 11
Laravel APi Update Function...
According to this query no need to define keys
`
public function update(Request $request){
$reqdata = $request->all();
$reqdata['date_created'] = date('Y-m-d');
$lesson= Lesson::where('id',$request->id)->update($reqdata);
if ($lesson) {
return 'true';
}else{
return 'false';
}
} `
Your data type as attached image
Upvotes: -1
Reputation: 897
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
Upvotes: 1
Reputation: 513
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
Upvotes: 1