@muhammadmaaz To your form add enctype="multipart/form-data"
<form ction="/your-url" method="POST" enctype="multipart/form-data">
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
i am trying to save an image in data base through form using post method.but when i print the recieving data on action page,i don't get image filed,so please guide me about that issue?
@muhammadmaaz To your form add enctype="multipart/form-data"
<form ction="/your-url" method="POST" enctype="multipart/form-data">
have done all necessary chores,my whole data is passing except image,just cz it's input type is file
Can you show us your code so we can see what you are doing?
@muhammadmaaz Also check
Documentation https://laravel.com/docs/7.x/requests#files
Because retrieving uploaded files is different then normal data.
it's my image filed.
Imagehere is my form action etc,
{{csrf_field()}}]and here im printing my form data,
if($request->isMethod('POST')){
$data=$request->all();
tell me if i need to show more code,
well im not retrieving it,im actually uploading it on database
You need to put back ticks ``` around your code. And ideally the blade file where your form is and the controller please
''' if($request->isMethod('POST')){
$data=$request->all();
echo "<pre>";print_r($data);die;''''
here is my controller code where im printing my data sent through form
@muhammadmaaz Could you post whole your view where is <form> and your controller?
But please to format it well.
please guide me how can i post here my code in proper format
``` is not same as '''
@section('content')
<div id="content">
<div id="content-header">
<div id="breadcrumb"> <a href="index.html" title="Go to Home" class="tip-bottom"><i class="icon-home"></i> Home</a> <a href="#">products</a> <a href="#" class="current">Add products</a> </div>
<h1>products</h1>
@if(Session::has('flash_message_error'))
<div class="alert alert-error alert-block">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>{!! Session('flash_message_error')!!}</strong>
</div>
@endif
@if(Session::has('flash_message_success'))
<div class="alert alert-error alert-block">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>{!! Session('flash_message_success')!!}</strong>
</div>
@endif
</div>
<div class="container-fluid"><hr>
<div class="row-fluid">
<div class="span12">
<div class="widget-box">
<div class="widget-title"> <span class="icon"> <i class="icon-info-sign"></i> </span>
<h5>Add product</h5>
</div>
<div class="widget-content nopadding">
<form enctype="multipart/form-data" class="form-horizontal" method="POST" action="{{url('/admin/add-product')}}" name="add_product" id="add_product" novalidate="novalidate">{{csrf_field()}}
<div class="control-group">
<label class="control-label">Under Categroy</label>
<div class="controls">
<select name="category_id" id="category_id" style="width:220px;">
<?php echo $categories_dropdown; ?>
</select>
</div>
</div>
</div>
<div class="control-group">
<label class="control-label">Product Name</label>
<div class="controls">
<input type="text" name="product_name" id="product_name">
</div>
</div>
<div class="control-group">
<label class="control-label">Product Code</label>
<div class="controls">
<input type="text" name="product_code" id="product_code">
</div>
</div>
<div class="control-group">
<label class="control-label">Product Color</label>
<div class="controls">
<input type="text" name="product_color" id="product_color">
</div>
</div>
<div class="control-group">
<label class="control-label">Description</label>
<div class="controls">
<textarea name="description" id="description"></textarea>
</div>
</div>
<div class="control-group">
<label class="control-label">Price</label>
<div class="controls">
<input type="text" name="price" id="price">
</div>
</div>
<div class="control-group">
<label class="control-label">Image</label>
<div class="controls">
<input type="file" id="image" name="image">
</div>
</div>
<div class="form-actions">
<input type="submit" value="Add product" class="btn btn-success">
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
@endsection```
namespace App\Http\Controllers;
use Auth;
use Session;
use App\Category;
use App\Product;
use Illuminate\Http\Request;
class productcontroller extends Controller
{
public function addproduct(Request $request)
{
if($request->isMethod('POST')){
$data=$request->all();
echo "<pre>";print_r($data);die;
// print_r($data);die;
if(empty($data['category_id'])){
return redirect()->back()->with('flash_message_error','under category is missing');
}
$product=new product;
$product->category_id=$data['category_id'];
$product->product_name=$data['product_name'];
$product->product_code=$data['product_code'];
$product->product_color=$data['product_color'];
if(!empty($data['description'])){
$product->description=$data['description'];
}else{
$product->description='';
}
$product->price=$data['price'];
$product->image=$data['image'];
$product->save();
return redirect()->back()->with('flash_message_success','product has been added successfully');
}
$categories= Category::where(['parent_id'=>0])->get();
//print_r($categories);die;
$categories_dropdown= "<option value='' selected disabled>select</option>";
// print_r($categories_dropdown);die;
foreach($categories as $cat) {
$categories_dropdown .="<option value='".$cat->id."'>".$cat->name."</option>";
$sub_categories= Category::where(['parent_id'=>$cat->id])->get();
//print_r($sub_categories);die;
foreach($sub_categories as $sub_cat){
$categories_dropdown.= "<option value='".$sub_cat->id."'> -- ".$sub_cat->name." </option>";
}
// print_r($categories_dropdown);die;
}
return view('admin.products.add-product')->with(compact('categories_dropdown'));
}
}```
please check my code
you can't save the image into database by simply assigning it like other value data, file type input is an object not a string or integer...
check this out... https://stackoverflow.com/questions/16382672/how-insert-and-retrieve-images-to-and-from-database-using-php
@muhammadmaaz Why do you use same method for get and post request?
Change this echo "<pre>";print_r($data);die; to dd($data);
And show us result.
"_token" => "jla2fXLwpRuXRujQB86MFbYN6W6OqPEb64Ol12gi"
"category_id" => "1"
"product_name" => "dsfsd"
"product_code" => "xewwe"
"product_color" => "sdvd"
"description" => "sdfsd"
"price" => "332"
]
im not saving it yet,im simply checking whether image filed is being posted or not?and sadly it's not
You cant assign image to variable and send it with mail. You need to do several steps
Check whether image is in valid format (jpg, png...) Then upload it to sever then send it with attach and in you should use enctype="multipart/form-data"
@muhammadmaaz And what do you get int this?
$request->file('image');
should i try this?
i used it like that and i get empty page as an output,
$cat= $request->file('image');
print_r($cat);die;
```
im working with laravel 5.6
I see that you don't have validation, do you really upload any image?
well i printed both suggestions and each time it truned out to be an empty page
yes i selected an image in my form,
then i need to send my image from form trough $_Files instead of $_post,am i correct,sorry i am yet a student,
that's all what it returned,
^ []
Please or to participate in this conversation.