dimanche 24 juillet 2016

Laravel 5: Insert image urls into column as array

I am building an application using Laravel 5 where I want to insert several image urls within an array separated by commas. This would be placed in a column on the database. The files are successfully uploaded to my AWS S3 bucket, but it's now the input to the database. I tried using Laravel's array_add helper but I get an error stating I am missing argument 2. I am wondering how would I be able to achieve this. My current alternate solution is to put the images with the post id and use the relationship to connect them together.

For reference: The column I intend to place the images is picgallery, and the insert is done using $newproperty['picgallery'] variable.

public function store(Request $request)
{
    //establish random generated string for gallery_id
    $rando = str_random(8);

    //input all data to database 
    $data = $request->all(); 

    $newproperty['title'] = $data['title']; 
    $newproperty['address'] = $data['address']; 
    $newproperty['city'] = $data['city'];
    $newproperty['province'] = $data['province']; 
    $newproperty['contact_person'] = Auth::user()->id; 
    $newproperty['gallery_id'] = $rando; 
    $newproperty['property_description'] = $data['description']; 

    if($request->hasfile('images')) {  
       $files = $request->file('images'); 

       //storage into AWS 
       foreach ($files as $file) { 
           $uploadedFile = $file;
           $upFileName = time() . '.' . $uploadedFile->getClientOriginalName(); 
           $filename = strtolower($upFileName); 

           $s3 = \Storage::disk('s3'); 
           $filePath = 'properties/' . $rando . '/' . $filename;

           $s3->put($filePath, file_get_contents($uploadedFile), 'public');

           $propicurl = 'http://ift.tt/2ajrjBv' . $filePath; 

           $array = array_add(['img'=> '$propicurl']);

           $newproperty['picgallery'] = $array; 

       } 
    } 

    Properties::create($newproperty); 

    return redirect('/properties');
}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2a4rKCX
via IFTTT

Aucun commentaire:

Enregistrer un commentaire