i'm not able to store my image data in db. i'm trying to store my image in data base but my image code is not executing.
here is my controller code and image form
public function addimages(Request $request,$id=null)
{
$productdetails= Product::with('attributes')->where(['id'=>$id])->first();
if($request->isMethod('post'))
{
$data=$request->all();
// echo "<pre>";print_r($data);die;
if($request->hasFile('image')) {
// echo "maaz";die;
$files=$request->file('image');
// echo "<pre>";print_r($files);die;
foreach($files as $file)
{
$image=new products_image;
$extension=$file->getClientOrignalExtension();
$filename=rand(111,9999).'.'.$extension;
$large_image_path='images/backend_images/products/large'.$filename;
$medium_image_path='images/backend_images/products/medium'.$filename;
$small_image_path='images/backend_images/products/small'.$filename;
// echo "<pre>";print_r($small_image_path);die;
Image::make($file)->save($large_image_path);
Image::make($file)->resize(600,600)->save($medium_image_path);
Image::make($file)->resize(300,300)->save($small_image_path);
$image->image=$filename;
//print_r($filename);die;
$image->product_id=$data['product_id'];
$image->save();
}
}
return redirect('admin/add-images/'.$id)->with('flash_message_success','proudct image has been added successfully');
}
// print_r($productdetails);die;
return view('admin.products.add_images')->with(compact('productdetails'));
}
Alternate Image(s)
<div class="control-group">
<label class="control-label">Alternate Image(s)</label>
<div class="controls">
<input type="file" name="image" id="image" multiple="multiple" />
</div>
@muhammadmaaz Add name="image[]" to your input like
<input type="file" name="image[]" id="image" multiple="multiple">
actually i did it,and also i printed the image which i wanna store by printing $data,i's coming up,but just after that line,my image store code is not working,please guide about it.
here is the result of printing $data
Array
(
[_token] => SBahrbR8NEu3RUhcHZdxLx5nuxiwHTwfKMw84sw1
[product_id] => 2
[image] => Array
(
[0] => sideviewgreen_grande.webp
)
)
@michaloravec
actually my image code is not working,if i try to print below line,it stills redirects and says image has been added successfully.which shows image code is not working,btw i used a jpg image this time.
$files=$request->file('image');
echo "<pre>";print_r($files);die;
@muhammadmaaz Change your code to something like this
if ($request->hasFile('image')) {
foreach($request->file('image') as $image) {
$filename = Str::random(14).'.'.$image->getClientOriginalExtension();
$largeImage = Image::make($image)->encode();
Storage::disk('public')->put("images/backend_images/products/large/{$filename}", (string) $largeImage);
$mediumImage = Image::make($image)->resize(600, 600)->encode();
Storage::disk('public')->put("images/backend_images/products/medium/{$filename}", (string) $mediumImage);
$smallImage = Image::make($image)->resize(300, 300)->encode();
Storage::disk('public')->put("images/backend_images/products/small/{$filename}", (string) $smallImage);
$productdetails->images()->create([
'image' => $filename
]);
}
}
$productdetails->images() just chnge to your relationship name.
@michaloravec
sorry i didn't understand this line of code and editor is showing error too,please guide me about specific relationship name you mentioned.
$productdetails->images()->create([
'image' => $filename}
@muhammadmaaz Do you use relationships?
https://laravel.com/docs/7.x/eloquent-relationships
Because your products_image model has foreing key product_id, actually I don't know why you use name for it instead of ProductImage.
So your Product model should have this relationship
public function images()
{
return $this->hasMany(App\ProductImage::class);
}
Please set your relationships and use Laravel naming convention for models and you avoid to more problems in the future.
@muhammadmaaz Please read that part of documentation what I posted before.
This is same as before
products_image::create([
'image' => $filename,
'product_id' => $data['product_id']
]);
@michaloravec
okay here is my product model in which i added images function like this,
public function images()
{
return $this->hasMany(App\ProductImage::class);
}
then i added relationship like that,
$productdetails= Product::with('images')->where(['id'=>$id])->first();
but it still didn't work and showed an error on this line
$productdetails->images()->create(['image' => $filename}
You don't understand at all...
You don't have to do this
$productdetails= Product::with('images')->where(['id'=>$id])->first();
To create use this code
$productdetails->images()->create([
'image' => $filename
]);
@michaloravec
okay then why editor is showing red line under this line of code
$productdetails->images()->create(['image' => $filename}
i'm really sorry about that,as i'm not a master and things are really messed up.
@muhammadmaaz What do you thing this is same?
$productdetails->images()->create([
'image' => $filename
]);
as
$productdetails->images()->create([
'image' => $filename
}
No, so use this
$productdetails->images()->create([
'image' => $filename
]);
actually i did it and it works,sorry for my fault.
i ran the code and it showed this error.
Class 'App\App\ProductImage' not found
Your model App\ProductImage exists as app/ProductImage.php?
Then your relationship should be
use App\ProductImage;
public function images()
{
return $this->hasMany(ProductImage::class);
}
@michaloravec
actually i did it but error still persists.here is my product model.
<?php
namespace App;
use App\ProductImage;
use Illuminate\Database\Eloquent\Model;
class product extends Model
{
public function attributes(){
return $this->hasMany('App\products_attribute','product_id');
}
public function images()
{
return $this->hasMany('App\ProductImage','product_id');
}
}
@michaloravec
actually my model is Products_image,not ProductImage,so i changed my header and also my model,it worked,when i tried to add image it again redirected by showing message image added successfully while it wasn't in database.my database name is Products_images.
@michaloravec
here is my products_image file.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class products_image extends Model
{
//
}
@muhammadmaaz
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class products_image extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['product_id', 'image'];
}
@michaloravec
it's my controller.
if($request->isMethod('post'))
{
$data=$request->all();
// echo "<pre>";print_r($data);die;
if ($request->hasFile('image'))
{
foreach($request->file('image') as $image) {
$filename = Str::random(14).'.'.$image->getClientOriginalExtension();
echo "<pre>";print_r($filename);die;
$largeImage = Image::make($image)->encode();
Storage::disk('public')->put("images/backend_images/products/large/{$filename}", (string) $largeImage);
$mediumImage = Image::make($image)->resize(600, 600)->encode();
Storage::disk('public')->put("images/backend_images/products/medium/{$filename}", (string) $mediumImage);
$smallImage = Image::make($image)->resize(300, 300)->encode();
Storage::disk('public')->put("images/backend_images/products/small/{$filename}", (string) $smallImage);
$productdetails->images()->create([
'image' => $filename
]);
}
}
return redirect('admin/add-images/'.$id)->with('flash_message_success','proudct image has been added successfully');
}
and i tried to print below line and it didn't work.
$filename = Str::random(14).'.'.$image->getClientOriginalExtension();
echo "<pre>";print_r($filename);die;
why debugger is ignoring this part of code?
@michaloravec
it didn't work.this part of code is not being execuated.
dd($filename);
Please sign in or create an account to participate in this conversation.