コード例 #1
0
 /**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function store(Request $request, RentalUnit $property)
 {
     // Validation //
     $validation = Validator::make($request->all(), ['caption' => 'max:50|regex:/^[A-Za-z ]+$/', 'photo' => 'required|image|mimes:jpeg,png|min:1|max:2048']);
     // Check if it fails //
     if ($validation->fails()) {
         return Response::json($validation->errors()->getMessages(), 400);
     }
     /**
      * Upload the image
      */
     $image = new PropertyPicture();
     $file = $request->file('photo');
     $destination_path = 'propertyImages/' . $property->id . '/';
     $filename = time() . '_' . $file->getClientOriginalName();
     $filename_thumb = time() . '_thumb_' . $file->getClientOriginalName();
     /**
      * Check whether the path exists
      */
     if (!File::exists($destination_path)) {
         File::makeDirectory($destination_path);
     }
     /**
      * Save the image to the specified path under the specified name
      */
     Image::make($file->getRealPath())->resize(null, 300, function ($constraint) {
         $constraint->aspectRatio();
     })->save($destination_path . $filename_thumb);
     $file->move($destination_path, $filename);
     /**
      * Save the image into the database
      */
     $image->location = $destination_path . $filename;
     $image->thumb_location = $destination_path . $filename_thumb;
     $image->caption = $request->input('caption');
     $image->mime_type = $file->getClientMimeType();
     $property->propertyPicture()->save($image);
     return Response::json(RentalUnit::with('User')->with('PropertyPicture')->where('id', $property->id)->first());
 }
コード例 #2
0
 /**
  * Display the specified resource.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function show(RentalUnit $rentalUnit)
 {
     return Response::json(RentalUnit::with('User')->with('PropertyPicture')->where('id', $rentalUnit->id)->first());
 }