please provide me the solution
getting error on multiple images
I am not able to store multiple images in database,when i upload two images ,only latest one i stored in database but on image folder both images are stored how can i solve this
begin to share some code of your methods (form...and controller or route)... how can people know what you did already and try to solve your issue ? did you use vue, ajax, html and/or php ?
please provide me the solution
You did not even show what you tried. That is not a proper way to post a question.
A helpful link: https://laracasts.com/discuss/channels/general-discussion/guidelines-for-posting-on-laracastscom
There could be a million reasons for this, and we're not going to just guess. Show your code so we can see where it's going wrong. We don't have crystal balls.
What the yek, not laravel but similar: It's Nova framework
public function add() {
if (isset($_POST['submit'])) {
$lid = DB::table('recents')->count();
$k = -1;
if (empty($lid) || strlen($lid) == 0 || is_null($lid)) {
$lid = 1;
}
$newname = '';
$destinationPath = ROOTDIR . 'upload/imgrecent/'; /////ADJUST FOR YOUR LARAVEL/////////
$files = Input::file('ufile');
//$names = [];
$arrname = [];
foreach ($files as $file) {
$k = $k + 1;
if (empty($file)) {
$arrname[$k] = "";
} else {
$file_name = $file->getClientOriginalName();
$file_ext = $file->getClientOriginalExtension();
$lid = $lid + $k + 1;
$fileInfo = pathinfo($file_name);
$filename = $fileInfo['filename'];
print_r($filename); /////TAKE OUT WAS FOR TESTING////////////
$arrname[$k] = $filename . $lid . "." . $file_ext;
$file->move($destinationPath, (string) $arrname[$k]);
}
}
$pic1 = Cln::fixValue((string) $arrname[0]);
$pic2 = Cln::fixValue((string) $arrname[1]);
$pic3 = Cln::fixValue((string) $arrname[2]);
$pic4 = Cln::fixValue((string) $arrname[3]);
$comments = Cln::fixValue($_POST['comments']);
if (!isset($error)) {
$postdata = array(
'pic1' => $pic1,
'pic2' => $pic2,
'pic3' => $pic3,
'pic4' => $pic4,
'comments' => $comments
);
DB::table('recents')->insert($postdata);
}
}//end add
$this->layout = 'addtpl';
return View::make('Recents/Add')->shares('title', 'Recent Add');
}
VIEW
<div style="margin-right:auto;margin-left:0px; width:900px;">
<form action='' method='post' enctype="multipart/form-data">
<?php
$num = 0;
$num_uploads = 4;
while ($num < $num_uploads) {
echo '<div><input name="ufile[]" type="file" id="ufile[]" size="50" /></div>';
$num++;
}
?>
<!--<p>Comments</p><br>
<textarea name='comments' rows='10' cols="70"></textarea>-->
<p>Comments</p><br>
<textarea class="rightdiv" name='comments'></textarea>
<p><input type='submit' name='submit' value='submit'></p>
</form>
</div>
Works 100%, zero problems. Older code was testing, need to add your csrf token, etc
TAH DAH
this is my controller public function postAddItem(Request $request) {
if(isset($request->parent_item) && $request->parent_item != 0)
{
$parent_item = $request->parent_item;
}
else
{
$parent_item = 0;
}
$distributor_only = $request->distributor_only;
$file_path = '';
$categories = ItemCategory::pluck('slug','id')->all();
$item_attribute_names = ItemAttributeName::pluck('id', 'name')->all();
$item_types = ItemType::pluck('id', 'name')->all();
if(isset($request->launch_date) && $request->launch_date != "")
{
$launchdate = $request->launch_date;
$launchdate = explode("/", $launchdate);
$launch_date = $launchdate[2] . "-" . $launchdate[0] . "-" . $launchdate[1];
$launch_date = date("Y-m-d", strtotime($launch_date));
}
else
{
$launch_date = "0000-00-00";
}
if(isset($request->disable_date) && $request->disable_date != "")
{
$disabledate = $request->disable_date;
$disabledate = explode("/", $disabledate);
$disable_date = $disabledate[2] . "-" . $disabledate[0] . "-" . $disabledate[1];
$disable_date = date("Y-m-d", strtotime($disable_date));
}
else
{
$disable_date = "0000-00-00";
}
$invalid_countries = "";
for($i = 0; $i < count($request->countries); $i++)
{
//must force it to be a string...
(string)$invalid_countries .= (string)$request->countries[$i];
if($i != count($request->countries) - 1)
{
$invalid_countries .= ",";
}
}
$item_status_id = $request->item_status;
$slug = $this->prepareSlug($request->item_name);
if($request->hasfile('product_image') !='')
{
$files = $request->file('product_image');
for($i=0; $i<count($files); $i++){
$destinationPath = base_path() . '/public/assets/custom/images/products/'.$categories[$request->item_category].'/';
$extension = $files[$i]->getClientOriginalExtension(); // getting image extension
$fileName = $slug.time().'.'.$extension; // renameing image
$i = 0;
if(!file_exists(base_path() . '/public/assets/custom/images/products/' . $categories[$request->item_category] . '/'))
mkdir( base_path() . '/public/assets/custom/images/products/' . $categories[$request->item_category] . '/' );
//if(!file_exists(base_path() . '/public/assets/custom/images/products-mobile/' . $categories[$request->item_category] . '/'))
//mkdir( base_path() . '/public/assets/custom/images/products-mobile/' . $categories[$request->item_category] . '/' );
while(file_exists(base_path() . '/public/assets/custom/images/products/' . $categories[$request->item_category] . '/' . $fileName))
{
//instead of tossing the file like we do now, we'll just add a numeric to the end.
$i++;
$fileName = $slug .time(). '_' . $i . '.' . $extension;
}
$file_path = 'products/'.$categories[$request->item_category].'/'.$fileName;
$img = Image::make($files[$i])->save( base_path() . '/public/assets/custom/images/products/'.$categories[$request->item_category].'/'.$fileName );
//$img = Image::make($request->file('product_image'))->save( base_path() . '/public/assets/custom/images/products-mobile/'.$categories[$request->item_category].'/'.$fileName );
$img->resize(160, 130);
//$img->save(base_path() . '/public/assets/custom/images/products-mobile/'.$categories[$request->item_category].'/'.$fileName);
}
//exit;
}
$enable_type = $request->enable_type;
$size=$request->size;
//you've gotta be kidding me...
if($request->mojiez_enable)
$mojiez_enable = 1;
else
$mojiez_enable = 0;
$request->description = str_replace("<br>", "", $request->description);
$request->description = str_replace("<br/>", "", $request->description);
$params = [
'name' => $request->item_name,
'description' => "",
'sku' => $request->sku,
'item_type_id' => $item_types['retail'],
'item_category' => $request->item_category,
'order_limit' => $request->order_limit,
'item_price' => $request->item_price,
'enable_type' => $enable_type,
'item_pv'=> $request->item_pv,
'long_description_attribute_id' => $item_attribute_names['long_description'],
'long_description_attribute_value' => $request->description,
'main_img_path_attribute_id' => $item_attribute_names['main_img_path'],
'main_img_path_attribute_value' => $file_path,
'in_stock' => $request->in_stock,
'initial_stock' => $request->initial_stock,
'sale_onoff' => $request->sale_onoff,
'sale_value' => $request->sale_value,
'mojiez_enable' => $mojiez_enable,
'sale_item_pv' => $request->sale_item_pv,
'new' => $request->new_banner,
'trending' => $request->trending,
'is_bundle' => $request->is_bundle
];
if($params['sale_onoff'] == 1){
if($params['sale_value']=='0.00'){
$params['sale_value']= $params['item_price'];
$params['sale_item_pv']= $params['item_pv'];
}
}else{
$params['sale_onoff'] = 0;
}
if(!isset($request->halfprice_enable))
$halfprice_enable = 0;
else
$halfprice_enable = 1;
if(isset($request->item_id) && $request->item_id!='')
{
$item_id = $request->item_id;
// update
DB::table('items')
->where("id",$request->item_id)
->update(
[
'name' => $params['name'],
'sku' => $params['sku'],
'item_status_id' => $item_status_id,
'enable_type' => $enable_type,
'item_category_id' => $params['item_category'],
'mojiez_enable' => $params['mojiez_enable'],
'order_limit' => $params['order_limit'],
'mojiez_enable' => $params['mojiez_enable'],
'launch_date' => DB::raw( "\"".$launch_date."\"" ),
'parent_item' => $parent_item,
'distributor_only' => $distributor_only,
'saleon' => $params['sale_onoff'],
'new' => $request->new_banner,
'trending' => $request->trending,
'blacklist_markets' => $invalid_countries,
'disable_date' => DB::raw( "\"".$disable_date."\"" ),
'is_bundle' => $request->is_bundle,
'halfprice_enable' => $halfprice_enable
]
);
$item_id = $request->item_id;
DB::table('item_category_items')->where('item_id',$item_id)->delete();
if($params['item_category'] !='')
{
$check_exists = itemCategoryItem::where('item_id',$item_id)->where('item_category_id',$params['item_category'])->get();
if(count($check_exists) == 0)
{
DB::table('item_category_items')->insert(
[
'item_id' => $item_id,
'item_category_id' => $params['item_category'],
]
);
}
}
if($request->item_category2 !='')
{
$check_exists = itemCategoryItem::where('item_id',$item_id)->where('item_category_id',$request->item_category2 )->get();
if(count($check_exists) == 0)
{
DB::table('item_category_items')->insert(
[
'item_id' => $item_id,
'item_category_id' => $request->item_category2,
]
);
}
}
if($request->item_category3 !='')
{
$check_exists = itemCategoryItem::where('item_id',$item_id)->where('item_category_id',$request->item_category3 )->get();
if(count($check_exists) == 0)
{
DB::table('item_category_items')->insert(
[
'item_id' => $item_id,
'item_category_id' => $request->item_category3,
]
);
}
}
DB::table('item_prices')->where("item_id",$request->item_id)
->where("end",NULL)
->update(
[
'value' => $params['item_price'],
'item_pv' => $params['item_pv'],
'sale_value' => $params['sale_value'],
'sale_item_pv' => $params['sale_item_pv'],
]
);
DB::table('item_attributes')
->where("item_id",$item_id)
->where("item_attribute_name_id",$params['long_description_attribute_id'])
->update(
[
'attribute_value' => $params['long_description_attribute_value']
]
);
if($files !='')
{
DB::table('item_attributes')
->where("item_id",$request->item_id)
->where("item_attribute_name_id",$params['main_img_path_attribute_id'])
->update(
[
'attribute_value' => $params['main_img_path_attribute_value']
]
);
}
$inventory = ItemInventory::where("item_id",$request->item_id)->first();
if($inventory){
DB::table('item_inventories')->where("item_id",$request->item_id)
->update(
[
'in_stock' => $params['in_stock'],
'initial_stock' => $params['initial_stock'],
]
);
}else{
DB::table('item_inventories')->insert(
[
'item_id' => $request->item_id,
'in_stock' => $params['in_stock'],
'initial_stock' => $params['initial_stock'],
'created_at' => DB::raw('now()')
]
);
}
$item_id = $request->item_id;
ItemSize::where("item_id",$item_id)->delete();
ItemColor::where("item_id",$item_id)->delete();
}
else{
if($categories[$params['item_category']] == 'mojieasy_items'){
$enable_type = 4;
}else{
$enable_type = 1;
}
$vendor_id = auth()->guard("admin")->user()->id;
$data = [
'name' => $params['name'],
'description' => $params['description'],
'sku' => $params['sku'],
//'size' => $params['size'],
'slug' => $slug,
'item_type_id' => $params['item_type_id'],
'item_status_id' => $item_status_id,
'enable_type' => $enable_type,
'mojiez_enable' => $params['mojiez_enable'],
'order_limit' => $params['order_limit'],
'item_category_id' => $params['item_category'],
'parent_item' => $parent_item,
'distributor_only' => $distributor_only,
'new' => $request->new_banner,
'created_at' => DB::raw("now()"),
'launch_date' => DB::raw( "\"".$launch_date."\"" ),
'blacklist_markets' => $invalid_countries,
'disable_date' => DB::raw( "\"".$disable_date."\"" ),
'vendor_id' =>$vendor_id,
'halfprice_enable' => $halfprice_enable
];
if(isset($params["sale_onoff"]))
{
$data['saleon'] = $params['sale_onoff'];
}
// insert into items
DB::table('items')->insert(
$data
);
$item_id = DB::getPdo()->lastInsertId();
if($params['item_category'] !='')
{
$check_exists = itemCategoryItem::where('item_id',$item_id)->where('item_category_id',$params['item_category'])->get();
if(count($check_exists) == 0)
{
DB::table('item_category_items')->insert(
[
'item_id' => $item_id,
'item_category_id' => $params['item_category'],
]
);
}
}
if($request->item_category2 !='')
{
$check_exists = itemCategoryItem::where('item_id',$item_id)->where('item_category_id',$request->item_category2 )->get();
if(count($check_exists) == 0)
{
DB::table('item_category_items')->insert(
[
'item_id' => $item_id,
'item_category_id' => $request->item_category2,
]
);
}
}
if($request->item_category3 !='')
{
$check_exists = itemCategoryItem::where('item_id',$item_id)->where('item_category_id',$request->item_category3 )->get();
if(count($check_exists) == 0)
{
DB::table('item_category_items')->insert(
[
'item_id' => $item_id,
'item_category_id' => $request->item_category3,
]
);
}
}
$prices = [
'item_id' => $item_id,
'item_price_type_id' => 1,
'value' => $params['item_price'],
'item_pv' => $params['item_pv'],
'start' => DB::raw('now()')
];
if(isset($params["sale_value"]))
{
$prices["sale_value"] = $params["sale_value"];
}
if(isset($params["sale_item_pv"]))
{
$prices["sale_item_pv"] = $params["sale_item_pv"];
}
DB::table('item_prices')->insert(
$prices
);
DB::table('item_attributes')->insert(
[
[
'item_id' => $item_id,
'item_attribute_name_id' => $params['long_description_attribute_id'],
'attribute_value' => $params['long_description_attribute_value'],
'created_at' => DB::raw("now()")
],
[
'item_id' => $item_id,
'item_attribute_name_id' => $params['main_img_path_attribute_id'],
'attribute_value' => $params['main_img_path_attribute_value'],
'created_at' => DB::raw("now()")
]
]
);
DB::table('item_inventories')->insert(
[
'item_id' => $item_id,
'in_stock' => $params['in_stock'],
'created_at' => DB::raw('now()')
]
);
}
$sizes = $request->size;
for($i = 0; $i < count($sizes); $i++)
{
$size = $sizes[$i];
ItemSize::insert([
'item_id'=>$item_id,
'size_id'=>$size,
]);
}
$colors = $request->colors;
for($i = 0; $i < count($colors); $i++)
{
$colors = $colors[$i];
ItemColor::insert([
'item_id'=>$item_id,
'color_id'=>$size,
]);
}
if(isset($request->item_id))
{
\Session::flash('message', 'Item Details Updated Successfully');
\Session::flash('alert-class', 'alert-success');
}
else
{
\Session::flash('message', 'Item added Successfully');
\Session::flash('alert-class', 'alert-success');
}
return redirect("admin/add-item/" . $item_id );
}
view blade
Image: @if(@$attributes['main_img_path']->attribute_value!='') <img width="50" height="50" src = "<?= IMG_PATH; ?>/{{@$attributes['main_img_path']->attribute_value}}">
</div>
@endif
<input type="file" id="product_image" name="product_image[]" multiple>
</div>
</div>
on that code currently i am able to show only one image but i need to display two or three or number of images
The code I showed works for me. Nova and Laravel, can be adjusted as needed.
Break up that controller code some for better code management. Have your add method separate. Or just add images alone till you work out bugs, don't go so fast.
@surendramannam1 That's one heck of a controller you got there lol.
From what I saw, you are cycling through the images and saving them to the filesystem, but when you save the actual record in the db you only save one image path, which would be the last one that you processed in the loop where saving images.
I think the problem is you are wanting a one to many relationship with images, where each "item" can have many images. You'd need an additional database table to hold those image paths with a foreign key pointing to your items table (I think that's the one, theres so much code it's hard to quickly see what's going on).
Then you'd have a hasMany relationship to retrieve the images associated with that "item".
Looks like you can stand to slow down a little and study and learn some relations and view some of Jeffrey's videos. Laravel does have somewhat of a learning curve take a deep breath and learn some first.
And that is one huge controller.
Holy crap. I have whole applications that have less lines than that one method !
lol :)
@Cronix i have a problem with images showing in blader.php i stored my images through code in image/products/vendorid(id gets dynamically)/category
how can i show my images in front end
what are you doing man? https://d.pr/free/i/X2AfYT
my code file I pasted here but it not coming here
use formatting. Put ``` on a line before and after your code
hi @ snapey
how can i get my user_id dynamically in blade.php files
{{auth()->user()->id}}
Or just the shortcut {{ auth()->id() }}
Please or to participate in this conversation.