Snapey's avatar

Why does your form not contain proper tags?

Where is the submit button?

Snapey's avatar

It doesn't help. Just paste the actual code and put ``` on their own lines before and after. HTML is displayed fine here:

            <div class="content">
                <div class="title m-b-md">
                    Template Mailer
                </div>
1 like
kendrick's avatar

The problem is the if condition around the foreach loop. Without it, the files will be listed. @Snapey

Now that I create a product with:

foreach ($request->images as $image) {
                $image = $request->file('images');
                $filename = time() . '.' . $image->getClientOriginalExtension();
                $location = public_path('uploads/business/products/'. $filename );          
                Image::make($image)->resize(812, null, function ($constraint){$constraint->aspectRatio();})->save($location);
            }

            $product->pictures()->save($picture);

it results in Call to a member function getClientOriginalExtension() on null

Snapey's avatar

it results in Call to a member function getClientOriginalExtension() on null

Comment out this line

                $image = $request->file('images');

and sometime do that dd() so you can see what you are working with

1 like
kendrick's avatar

@Snapey

Call to a member function getClientOriginalExtension() on string

It's crazy

Snapey's avatar

because you don't have uploaded files, just file names..

Still looking for that dd()

1 like
kendrick's avatar
foreach ($request->images as $image) {
     dd($image) 
}

only gets the first selected image. @Snapey

Snapey's avatar

Is that what I suggested?

dd is DIE and dump. so on the first time around the loop it DIES

also, still looking for that form code...

1 like
kendrick's avatar
foreach ($request->images as $image) {
    dump($image);
}
die;

Shows the list of the files, but what's the difference. When i put dd($image); outside the brackets it'll show the last file name of the loop. @snapey

Which form code?

Snapey's avatar

Do you remember this

dd($request->images); and post the result

All we are looking to establish is if you are getting instance of UploadedFile object

and I suggested you post the form as it is and not with you changing all the Brackets

1 like
kendrick's avatar

Sorry, I forgot @Snapey

array:3 [▼
  0 => "blueboxtest.jpg"
  1 => "wfp.jpg"
  2 => "mmm.jpg"
]
<form enctype="multipart/form-data" action="/success" method="POST">

        <h1 class="qsetp">Image</h1>
                                
            <input type="file" name="images[]" multiple="true" />
        <hr>

        <h3 class="qsetp">Title</h3>
        <div class="container" id="editedit11">
                                                
        <div class="form-group{{ $errors->has('title') ? ' has-error' : ''}}"">
                                                            
        <input type="text" name="title" class="form-control" id="editfields22" placeholder="Title">
            @if ($errors->has('title'))
                    <span class="help-block">{{ $errors->first('title') }}</span>
            @endif          
            </div>  
        </div>

                                
                            
        <div class="form-group">
               <button type="submit" class="btn btn-default" style="margin-top: 20px; color: white;" id="button-profile">Create Product</button>
        </div>
        <input type="hidden" name="_token" value="{{ Session::token() }}">
</form>

Snapey's avatar

I replicated your code, pointed it at a controller and put dd($request->images); at the top

This is what I see.

Puzzled why you only see filenames. What is going on in the rest of the controller?

array:3 [▼
  0 => UploadedFile {#198 ▶}
  1 => UploadedFile {#204 ▶}
  2 => UploadedFile {#205 ▶}
]
1 like
kendrick's avatar

This is the whole controller @Snapey :

public function postProduct(Request $request){
        
        $this->validate($request, [

            'title'=> 'max:140',
            'brand'=> 'max:140',
            'sku'=> 'max:140',
            'description'=> 'max:140',
            'price'=> 'max:140',

        ]);

        
        $product = Auth::user()->products()->create([
            'business_id' => auth()->user()->id,
            'title' => $request->title,
            'brand' => $request->brand,
            'sku' => $request->sku,
            'description' => $request->description,
            'price' => $request->price,
        ]);

            foreach ($request->images as $image) {
            
                dd($request->images);

            }

            $product->pictures()->save($picture);
        
          return redirect()
            ->route('products.create')
            ->with('info', 'Your product has been created.');
    }
}
Snapey's avatar

Can't see anything wrong there.

Try with your dd at the top of the request.

How come your form posts to '/success' but the method is postProduct ?

Do you submit the form with traditional post or are you submitting with ajax

1 like
kendrick's avatar

Solution:

Controller:

foreach ($request->images as $image) {
                        
                        $business = Auth::user();
                        $filename = time() . '.' . $image->getClientOriginalExtension();
                        $location = public_path('uploads/'. $filename );          
                        Image::make($image)->resize(812, null, function ($constraint){$constraint->aspectRatio();})->save($location);
                
                    $pictures[] = new Picture(['product_image' => $filename]);

                    }

                    $product->pictures()->saveMany($pictures);

Picture.php

public function product(){
        return $this->belongsTo(Product::class);
}

Product.php

public function pictures(){
          return $this->hasMany(Picture::class);
}

Pictures Table:

 Schema :: create ('pictures', function(Blueprint $table){
            $table->increments('id');
            $table->unsignedInteger('product_id'); // 
            $table->string('product_image')->default('productdefault.jpg');
            $table->timestamps();
 });
Snapey's avatar

Great. How did you manage to get the images? I can't see anything here that would resolve the problem?

1 like
Previous

Please or to participate in this conversation.