laravel路由资源更新方法

时间:2018-10-22 12:48:27

标签: php laravel laravel-5 laravel-5.6

我正在使用JWt在laravel中创建待办事项应用程序 路由资源中的所有方法(索引,存储,显示..etc等)都可以正常工作,除了更新 结果使其工作正常并获得成功响应,但数据库中没有变化

**对不起,我的英语不好

这是我的短代码

  

api.php

    Route::middleware('jwt.auth')->group(function () {
    Route::resource('/todo', 'API\TodoController');
});
  

BaseController.php

class BaseController extends Controller
{
    public function sendResponse($result,$message)
    {
        $response=[
            'success'=> true,
            'date' => $result,
            'message'=> $message
        ];
         return response()->json($response,200);
    }

    public function sendError($error,$errorMessages=[],$code=404)
    {
        $response=[
            'success'=> false,
            'message'=> $error
        ];

        if (!empty($errorMessages)) {
            $response['date']=$errorMessages;
        }
         return response()->json($response,$code);
    }}
  

TodoController.php

class TodoController extends BaseController
{
    .
    .
    .

public function update(Request $request, Todolist $todolist)
    {

        //
        $input = $request->all();
        $validator = Validator::make($input, [

            'title' => 'required | max:255',
            'content' => 'required | max:255',
            'status' => 'required | max:2',
            'start_date' => 'required  ',
            'end_date' => 'required  ',
        ]);

        if ($validator->fails()) {

            return $this->sendError('error validation', $validator->errors());

        }

        $todolist->title = $request->title;
        $todolist->content = $request->content;
        $todolist->status = $request->status;
        $todolist->start_date = $request->start_date;
        $todolist->end_date = $request->end_date;
        $todolist->save();
        return $this->sendResponse($todolist->toArray(), 'update successfully');

    }}

这是邮递员的结果

https://i.stack.imgur.com/4oHqC.png

2 个答案:

答案 0 :(得分:2)

您应该在上课之前参考模型

use App\Todolist;
class TodoController extends BaseController
{
    .
    .
    .

public function update(Request $request, Todolist $todolist)
    {

        //
        $input = $request->all();
        $validator = Validator::make($input, [

            'title' => 'required | max:255',
            'content' => 'required | max:255',
            'status' => 'required | max:2',
            'start_date' => 'required  ',
            'end_date' => 'required  ',
        ]);

        if ($validator->fails()) {

            return $this->sendError('error validation', $validator->errors());

        }


        $todolist->title = $request->title;
        $todolist->content = $request->content;
        $todolist->status = $request->status;
        $todolist->start_date = $request->start_date;
        $todolist->end_date = $request->end_date;
        $todolist->save();
        return $this->sendResponse($todolist->toArray(), 'update successfully');

    }}

答案 1 :(得分:0)

  

TodoList.php模型

   <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Todolist extends Model
{
    //
    protected $table='todolists';
    protected $fillable =[
        'title',
        'content',
        'status',
        'photo_id',
        'date_id',
        'start_date',
        'end_date',
        'user_id'
    ];
}
  

待办事项表

https://i.stack.imgur.com/GGr5Q.png