如何在json响应中添加isFavourite-使用Laravel

时间:2019-05-21 07:59:01

标签: php json laravel

我收到针对docId的文件夹响应

我的控制器代码:

               public function getFolders(Request $request)
{
    $doctorId = $request->input('doctor_id');

    $folder =  DB::table('folder_details')
                ->join('doctor_favourite_folder','folder_details.folder_id','=','doctor_favourite_folder.id')
                ->select('doctor_favourite_folder.id','doctor_favourite_folder.folder_name')
                ->where('folder_details.doctor_id',$doctorId)->get();

    $resultArray = ['status' => true, 'message' => 'Folder detail by doctorId!', 'data' => $folder];

    return Response::json($resultArray, 200);
}

我的回应:

             {
"status": true,
"message": "Folder detail by doctorId!",
"data": [
    {
        "id": 1,
        "folder_name": "Accu"
    },
    {
        "id": 2,
        "folder_name": "PT"
    },
    {
        "id": 3,
        "folder_name": "Chiro"
    }
]
        }

现在我想显示这些文件夹是喜欢的还是不喜欢下面的

                {
        "id": 1,
        "folder_name": "Accu",
         "isFavourite": true or false
    },

我如何在每个文件夹中显示这种回复,我们将不胜感激您的帮助?

2 个答案:

答案 0 :(得分:0)

我假设isFavourite数据不在数据库中,但是它是通过某种逻辑计算的。

然后在创建$resultArray之前,您可以执行以下操作

$folder = $folder->map(function(&$f) {
    $f->isFavourite = ... // here you put your logic to compute that info.
});

答案 1 :(得分:0)

您可以尝试使用@Leonardo Rossi和 您应该在select中声明 DispatchQueue.main.async { # mainly in this thread getting error check it once let ud = UserDefaults.standard ud.set(true, forKey: "isCheckedIn") ud.set( 1, forKey:"type") ud.set( self.project.ID!, forKey: "projectId" ) ud.set( self.todoItem.ID! , forKey: "todoId" ) // print(" todo data is : \(self.todoItem.ID!)") ud.set( self.des, forKey: "des") ud.set( startTime,forKey: "startTime") ud.set( startTime, forKey: "checkInTime" ) ud.synchronize() self.hideHUD() self.performSegue(withIdentifier: "goStartTodoFromSelect", sender: nil ) }

const obj = {
    a:false,
    b:{
        c:{
            e:{
                f:false,
                g:true,
            },
            h:{
                i:false,
                j:true,
            }
        }
    },
    k:{
        l:false,
        m:false
    },
    n:true
};

function checkAll(obj,state = true) {
  const regex = state ? /true/gi : /false/gi;
  const changeTo = state ? 'false' : 'true';
  // the magic, using string replace to set all at once
  return JSON.parse(JSON.stringify(obj).replace(regex, changeTo));
}

function uncheckAll(obj) {
  return checkAll(obj,false);
}

console.log(checkAll(obj));
console.log(uncheckAll(obj));

对不起,我无法评论他的帖子