示例#1
0
 /**
  * Store a newly created resource in storage.
  *
  * @return Response
  */
 public function store()
 {
     $response = new stdClass();
     $statusCode = 201;
     $in = Input::only('name', 'lat', 'lng', 'cid');
     $rules = array('name' => 'required | alpha_spaces | unique:churches', 'lat' => 'numeric', 'lng' => 'numeric', 'cid' => 'integer');
     $vd = Validator::make($in, $rules);
     if ($vd->fails()) {
         $errs = $vd->messages();
         $statusCode = 400;
         $response = $errs->all();
         if ($errs->has('name')) {
             // 教會已有建檔,嘗試建立教會與使用者之間的關係
             $cid = Church::where('name', $in['name'])->pluck('id');
             $statusCode = 201;
             // 先檢查是否已經加入教會
             $uc = UserChurch::where('uid', Auth::user()->id)->first();
             if ($uc && $uc->uid === Auth::user()->id) {
                 $uc->cid = $cid;
                 $uc->save();
             } else {
                 $relation = array('cid' => $cid, 'uid' => Auth::user()->id);
                 UserChurch::firstOrCreate($relation);
             }
             $response = new stdClass();
             return Response::json($response, $statusCode);
         }
     } else {
         $in['qlink'] = XUtil::makeQlink($in['name']);
         $church = Church::create($in);
         // 先檢查是否已經加入教會
         $uc = UserChurch::where('uid', Auth::user()->id)->first();
         if ($uc && $uc->uid === Auth::user()->id) {
             $uc->cid = $church->id;
             $uc->save();
         } else {
             // 建立教會與使用者之間的關係
             $relation = array('cid' => $church->id, 'uid' => Auth::user()->id);
             UserChurch::firstOrCreate($relation);
         }
         $response = new stdClass();
     }
     return Response::json($response, $statusCode);
 }