检查集合是否包含与用户ID

时间:2017-06-28 16:44:35

标签: laravel collections eloquent

我需要检查用户提交的数据集合是否包含该数据的ID,其中发布该数据的用户是经过身份验证并执行请求的用户。 如何使用eloquent中提供的收集方法?

示例:

 public function index(){

        $user_id = Auth::id();

        if(Request::ajax()){

            $suggested_data_id = Input::get('suggested_data_id');

            $LatestSuggestions = Suggested_data::with('user')->latest()->orderBy('id', 'desc')->take(10)->get();

            if($LatestSuggestions->contains('id', $suggested_data_id)->where( // the user that posted that data that is found in the collection is the authenticated user )){

                return response()->json(array('latest_suggestions' => $LatestSuggestions));

            }else{

                $userLatestAddition = Suggested_data::with('user')->where('id', '=', $suggested_data_id)->get();

                return response()->json(array('latest_suggestions' => $LatestSuggestions, 'last_user_addition' => $userLatestAddition));

            }


        }

    }

0 个答案:

没有答案