2017-01-03 55 views
2

我建立了一個API,我發現了以下錯誤而更新,並從表中刪除我使用的郵遞員來測試我的APILaravel阿比更新和刪除功能

//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 

我控制器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'); 
    } 

} 

我的模型課

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 

class Lesson extends Model 
{ 
    protected $fillable = ['title', 'body', 'completed',]; 

    //protected $hidden =['title']; 
} 

商店等功能工作正常

謝謝

+0

它看起來像你的'$ ulesson-> title'不設置你的更新功能和所有這些值可能需要執行您的更新請求 – MounirOnGithub

+0

你也可能想看看'$ ulesson->填充($ request-> all());':) –

+0

我是sry我不是得到我應該做的事 –

回答

1

在更新 可以爲您的DD線69 ($請求 - >輸入(「標題」))我覺得你不發送標題

和刪除的價值 我認爲你必須在id字段沒有值

+0

我得到輸出'null'' –

+0

是的,對於這個空值,它沒有找到任何要刪除的東西,請檢查你的郵遞員中輸入的 –

+0

是哪個輸入值,你添加參數標題正文並完成身體形式 - 數據? –

1

The image of postman

請檢查你的郵遞員,並設置它像這樣

+0

先生這是一個POST請求它的工作正常數據插入到數據庫,但如何執行更新 –

+0

好吧,你只需要改變postman中的方法從POST到PUT或PATCH –

1

我剛剛下載失眠和測試預期

每一件事工作正常,我不知道爲什麼它不是在郵遞員的工作,雖然