0

i have been able to follow this answer and i can actually create multiple image size.

My question is, how can i save each path to a database.

public function store(Request $request)
{
    $input= $request->all();

    $file = $input['image'];

    $destinationPath='images/products';
    $destinationPathlarge='images/products/large';

    $extension = $file->getClientOriginalExtension();

    $fileName = rand(111,999).'.'.$extension;
    $image = $destinationPath . '/' .$fileName;

   $upload_success=  $file-> move($destinationPath,$fileName);
    $doc = new Products();
    $doc->name = $input['name'];
    $doc->price = $input['price'];
    $doc->description = $input['description'];
    $doc->s_description = $input['s_description'];
    $doc->brands_id = $input['brands_id'];
    $doc->categories_id = $input['categories_id'];
   $upload = Image::make($image)->resize(190,185)->save($destinationPath. '/' .$fileName)
        ->resize(100,100)->save($destinationPathlarge. '/'.$fileName);
    $doc->save();
Community
  • 1
  • 1
Adedapo Ajuwon
  • 323
  • 2
  • 6

1 Answers1

1

You should create an appropriate Eloquent model.

First, run an artisan command in your project's folder.

php artisan make:model MyImage

This will create the 'MyImage' Eloquent Model and it's database migration.

Edit the newly created migration file by adding new path fields to the up() function like this:

Schema::create('my_images', function(Blueprint $table)
{
    $table->increments('id');

    $table->string('path_190');
    $table->string('path_100');

    $table->timestamps();
});

Run the new migration to make the changes to your database.

Then, in the App\MyImage model class, add the fillable property to enable filling of the path fields:

class Image extends Model {

protected $fillable = [
    'path_100',
    'path_190',
];

}

Now add to your Controller's store action:

App\MyImage::create([
    'path_100' => asset($destinationPathlarge. '/'.$fileName100),
    'path_190' => asset($destinationPathlarge. '/'.$fileName190),
])->save();

I hope it helps :)

Michael Chekin
  • 568
  • 4
  • 11
  • I don't think creating a table for different sizes of an image is a good way. A proper naming convention is much better. – Hkan Oct 24 '15 at 18:09
  • You are mistaken. I've created just one table 'my_images' with two path fields. – Michael Chekin Oct 24 '15 at 18:11
  • No, I get it. I just chose bad words. Using database for this particular thing is unnecessary IMO. I'm posting an answer in a few minutes. – Hkan Oct 24 '15 at 18:58
  • The question was: **how can i save each path to a database**. And it **DOES** make sense to store the paths to the uploaded and re-sized images in order to retrieve them on demand. It **DOES NOT** make sense cropping the image every time you want to display it. – Michael Chekin Oct 24 '15 at 19:49
  • 1
    Oh you're right, I didn't notice that he explicitly wanted database. I apologize for that. BTW, the codes in my answer doesn't crop on every request. It is fairly efficient than using database but it's only problem is lack of flexibility. Anyway, since it is not what OP requested, I'm deleting my answer and voting you up. – Hkan Oct 24 '15 at 20:56
  • hi @MishaChekin, thanks for the answer, it really helped. i would have love to upvote and also mark it as answer, but SO hasn't given me the permission to do so. – Adedapo Ajuwon Oct 25 '15 at 22:45
  • @user3185218 That is very strange. I'm pretty sure you should be able to accept an Answer to your own Question. Maybe you are mistaking it with Voting Up. – Michael Chekin Oct 29 '15 at 15:04