2016-05-16 6 views
0

Gute Nacht!Wie man multiple Fotos überprüft, wenn null ist

Ich erstelle eine Validierung für mehrere Dateien für Laravel5. Ich habe diesen Code in PHP, die es funktioniert, wenn ich ein Bild senden

public function uploadFotos(UploadFotosRequest $request){ 
     $image = $request->file('photo'); 

     $identificador = \Request::input('id'); 
     $pi=PI::find($identificador); 
     foreach($image as $file){ 
       $fotosPI= new PhotosPI(); 
       $fotosPI->ruta = 'images/punto_interes/'.$pi->id.'/'.$file->getClientOriginalName(); 
       $file->move('images/pi/'.$pi->id.'/', $file->getClientOriginalName()); 
       $fotosPI->creador_id=Auth::user()->id; 
       $fotosPI->punto_interes_id=$puntoInteres->id; 
       $fotosPI->save(); 
     } 

    } 

Der Antrag Validierung

<?php 

namespace App\Http\Requests; 

use App\Http\Requests\Request; 

class UploadFotosRequest extends Request 
{ 
    /** 
    * Determine if the user is authorized to make this request. 
    * 
    * @return bool 
    */ 
    public function authorize() 
    { 
     return true; 
    } 

    /** 
    * Get the validation rules that apply to the request. 
    * 
    * @return array 
    */ 
    public function rules() 
    { 
     return [ 
      'photo' => 'required' 
     ]; 
    } 
} 

Wenn ich senden keine Bilder ich folgende Fehlermeldung empfangen, und ich brauche zu kontrollieren, wenn Foto erhält ein Bild oder nicht.

Call to a member function getClientOriginalName() on a non-object 
+0

Was ist drin $ image? Können Sie es mit XDebug oder print_r/var_dump debuggen? – Willian

+0

Array (1) {[0] => NULL} Ich erhalte ein Array, ich muss dies überprüfen @Willian – jc1992

Antwort

1

Versuchen falsche Werte zu entfernen (nach dem Gießen) mit array_filter:

$image = array_filter($request->file('photo')); 
+0

Ja, es funktioniert;) – jc1992

Verwandte Themen