Hi my laravel buddies, i have implemented the e-shop, i have an product categories and products, the products contain it's product-images, now i have implemented to upload images and update their details about name , alt, and title and all as one by one, but i need to update their details by whole here i have attached my Full MVC code for your reference, kindly please let me know how it's possible for Laravel,
And i am using relationship for product and it's product images, so let me know if i need to change any relationship to update alll the product images,
Controller code For Product Images:
public function updateImageInfo($product_id, $image_id) {
$image = ProductImage::findOrFail($image_id);
// $ftproot = '/alluploads/alterlondon/';
$ftproot = '~/alterlondon/';
if (Input::get('delete') == 1) {
File::delete(public_path() . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'products' . DIRECTORY_SEPARATOR . 'large' . DIRECTORY_SEPARATOR . $product_id . DIRECTORY_SEPARATOR . $image->image_path);
File::delete(public_path() . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'products' . DIRECTORY_SEPARATOR . 'medium' . DIRECTORY_SEPARATOR . $product_id . DIRECTORY_SEPARATOR . $image->image_path);
File::delete(public_path() . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'products' . DIRECTORY_SEPARATOR . 'small' . DIRECTORY_SEPARATOR . $product_id . DIRECTORY_SEPARATOR . $image->image_path);
File::delete(public_path() . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'products' . DIRECTORY_SEPARATOR . 'original' . DIRECTORY_SEPARATOR . $product_id . DIRECTORY_SEPARATOR . $image->image_path);
$image->delete();
/* Delete ftp imagees start */
$ftpimg_original = $ftproot . "uploads/products/original/$product_id/" . $image->image_path;
$ftpimg_small = $ftproot . "uploads/products/small/$product_id/" . $image->image_path;
$ftpimg_medium = $ftproot . "uploads/products/medium/$product_id/" . $image->image_path;
$ftpimg_large = $ftproot . "uploads/products/large/$product_id/" . $image->image_path;
Ftp::connection('connection1')->delete($ftpimg_original);
Ftp::connection('connection1')->delete($ftpimg_small);
Ftp::connection('connection1')->delete($ftpimg_medium);
Ftp::connection('connection1')->delete($ftpimg_large);
/* Delete ftp imagees ends */
return Redirect::back()->with('tab', 'tab_images')->with('success', 'Awesome! you have changed the image title/alt.');
} else {
$validator = Validator::make($data = Input::all(), ProductImage::$rules);
if ($validator->fails()) {
return Redirect::back()->with('tab', 'tab_images')->withErrors($validator)->withInput();
}
$image->title = Input::get('title');
$image->alt = Input::get('alt');
if ($image->save()) {
return Redirect::back()->with('tab', 'tab_images')->with('success', 'Awesome! you have changed the image title/alt.');
}
return Redirect::back()->with('tab', 'tab_images')->with('error', 'Sad! Some error occured');
}
}
View Code for Product-Images:
<h3 class="box-title">Images</h3>
{{ Form::model($product, ['route' => ['backend.products.images', $product->id],
'method' => 'post', 'enctype' =>'multipart/form-data', 'class' => 'dropzone', 'id'=>'images']) }}
{{ Form::close() }}
<div>
@foreach ($product->product_images as $image)
<div style="float: left;margin-left: 10px;width:250px;padding:5px;border:1px solid #ccc;border-radius:5px;">
<img src="{{URL::to('/')}}/uploads/products/large/{{$product->id}}/{{$image->image_path}}" width="240" />
@if(isset($product->id))
{{ Form::model($product, ['route' => ['backend.products.images',$product->id,$image->id], 'method' => 'patch']) }}
@else
{{ Form::open(['route' => 'backend.products.create']) }}
@endif
<br />
<div class="box-body">
<div class="form-group">
{{ Form::label('title', 'Title Tag') }}
{{ Form::text('title',$image->title, array('class' => 'form-control','placeholder' => 'Enter Title Tag')) }}
</div>
<div class="form-group">
{{ Form::label('alt', 'Alt Tag') }}
{{ Form::text('alt',$image->alt, array('class' => 'form-control','placeholder' => 'Enter Alt Tag')) }}
</div>
<div class="box-footer">
{{ Form::button('Update',array('class'=>'btn btn-success','type' => 'submit')) }}
{{ Form::button('Delete',array('class'=>'btn btn-danger','name'=>'delete','value'=>'1','type' => 'submit')) }}
</div>
</div>
{{ Form::close() }}
</div>
@endforeach
<div class="clearfix"></div>
</div>
My product Model with it's relationship:
public function product_images() { return $this->hasMany('ProductImage')->orderBy('position_id'); }
My Product Image Model Code:
<?php
class ProductImage extends \Eloquent {
public static $rules = [
// 'id' => 'required',
// 'position_id' => 'required'
];
protected $fillable = ['id','product_id', 'image_path', 'title', 'alt','position_id','image_position'];
public function product() {
return $this->belongsTo('Product');
}
public function wishlist() {
return $this->belongsToMany('Wishlist');
}
}
My Routes Code:
Route::patch('backend/products/images/{product_id}/{image_id}',array('as'=>'backend.products.images','uses'=>'Backend\ProductsController@updateImageInfo'));
Route::get('backend/products/edit/{id}',array('as'=>'backend.products.edit','uses'=>'Backend\ProductsController@edit'));
So kindly please let me know how it's possible to update all the product images same time.
My Product DB Schema:
Create Table
CREATE TABLE `products` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`parent_category_id` int(255) DEFAULT '0',
`category_id` int(11) DEFAULT '0',
`child_category_id` int(255) DEFAULT '0',
`on_sale` tinyint(1) NOT NULL DEFAULT '1',
`quantity` int(11) NOT NULL DEFAULT '0',
`price` float(8,2) DEFAULT NULL,
`price_wholesale` float(8,2) DEFAULT '0.00',
`show_price` tinyint(1) NOT NULL DEFAULT '0',
`manufacturer_id` int(11) DEFAULT NULL,
`product_id` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`reference` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`out_of_stock` tinyint(1) NOT NULL DEFAULT '0',
`enabled` tinyint(1) NOT NULL DEFAULT '1',
`condition` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'new',
`type` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'standard',
`short_description` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`description` text COLLATE utf8_unicode_ci,
`slug` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`meta_title` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`meta_description` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`meta_keyword` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`created_at` timestamp NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`view_count` int(5) NOT NULL DEFAULT '0',
`installation_charges` tinyint(1) DEFAULT '1',
`product_position` int(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=844 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='utf8_unicode_ci'
My Product Image DB schema:
Create Table
CREATE TABLE `product_images` (
`position_id` int(11) NOT NULL,
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`product_id` int(11) NOT NULL,
`image_path` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`title` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`alt` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1048 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
I have same column call product id in products and Product images.so please check this and let me know.
I would use Laravel's storage class, so in the future if you decide to move to the cloud you can do so .easily.
instead of sending the file on the form post data, upload images with ajax and insert a hidden input with the image id. <input type="hidden" name="image_ids[]" value="1">
Create a new model (image or something) then save each image on the db. so you can easily save images and product relationships in just one line.
class Product{
public function images()
{
return $this->belongsToMany(Image::class);
}
}
class Image{
public function product()
{
return $this->belongsToMany(Product::class);
}
public function getMediumUrlAttribute()
{
// return path to medium size file
}
}
$product->images()->sync($request::get('image_ids'));
echo $image->medium_url;
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community