Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

VenkatAdapa's avatar

Form data not saving and no errors

public function postProperty(Request $request){ $this->validate($request, [ 'subcategoryid' => 'required', 'productname' => 'required', 'yearofpurchase' => 'required', 'expsellprice' => 'required', 'name' => 'required', 'mobile' => 'required', 'emailid' => 'required', 'location' => 'required', 'city' => 'required', 'photos' => 'required', 'photos.*' => 'image|mimes:jpg,png,jpeg,gif,svg|max:2048'

        ]);
        $ads = new Advertisement;
        $images = $request->file('photos');
        $count = 0;
        if($request->file('photos')){
            foreach($images as $item){
                if($count < 4){
                    $var = date_create();
                    $date = date_format($var, 'Ymd');
                    echo $imageName = $date.'_'.$item->getClientOriginalName();
                    $item->move(public_path().'/uploads/',$imageName);
                    $url = URL::to("/").'/uploads/'.$imageName;
                    $arr[] = $url;
                    $count++;

                }
                
            }
            $image = implode(",", $arr);
            $ads->maincategoryid = $request->input('maincategoryid');
            $ads->subcategoryid = $request->input('subcategoryid');
            $ads->productname = $request->input('productname');
            $ads->yearofpurchase = $request->input('yearofpurchase');
            $ads->expsellprice = $request->input('expsellprice');
            $ads->name = $request->input('name');
            $ads->mobile = $request->input('mobile');
            $ads->emailid = $request->input('emailid');

            $ads->location = $request->input('location');
            $ads->city = $request->input('city');
            $ads->photos = $image;
            $ads->save();
            return redirect('/')->with('info', 'Advertisement published successfully.');
0 likes
11 replies
VenkatAdapa's avatar

propertyads.blade.php

@extends('layouts.app')

@section('content') Categories

    @if(isset($categories)) @if(count($categories)>0) @foreach($categories as $category)
  • <a href='{{url('/post-classified-ads/'.preg_replace('/\s+/','',$category->maincategory).'/'.$category->id)}}'>{!!html_entity_decode($category->icons)!!}{{$category->maincategory}}
  • @endforeach @else

                            @endif
    		   			@endif
    		   			
    		   		</ul>
    		   	</div>
    		   </div>
    		</div>
    		<div class="col-md-9">
    			<div class="card">
    		   	<div class="card-header">
    		   		<strong>Advertisements</strong>
    		   	</div>
    		   	<div class="card-body">
    		   		<ul class="nav nav-tabs">
                            <li class="nav-item">
                                <a class="nav-link" data-togle="tab" href="#home">Property</a>
                            </li>
                        </ul>
                        <div id="myTabContent" class="tab-content">
                            <div id="home">
    
                            </div>
                        </div>
                    <div id="myTabContent" class="tab-content">
                            <div class="" id="home">
                            <h1 style="padding: 10px 10px;" id="selcatmsg"></h1>
                            <form class="form-horizontal" enctype="multipart/form-data" method="post" action="{{ url('/postProperty') }}" style="padding-left: 20px;">
                                {{csrf_field()}}
    
                                    <div class="=row">
                                        <div class="col-lg-6">
                                            @if(count($errors)>0)
                                                @foreach($errors->all() as $error)
                                                @endforeach
                                                @endif
                                            </div>
                                        </div>
    
                                <div class="row">
                                    <div class="col-lg-6">
                                        <div class="col-lg-12">
                                            <div class="form-group">
                                            <input type="hidden" name="maincategoryid" value={{ Request::segment(3) }}>
                                                <label><strong>Select Subcategory</strong></label>
                                                <select class="form-control" name="subcategoryid">
                                                    <option value="">Select</option>
                                                    @if(count($subcategories)>0)
                                                        @foreach($subcategories as $subcategory)
                                                            <option value={{$subcategory->id}}>{{$subcategory->subcategory}}</option>
                                                        @endforeach
                                                    @else
    
                                                    @endif
    
                                                </select>
                                            </div>
                                        </div>
    
                                        <label></label>
                                        @if($errors->has('subcategoryid'))
                                        <span class="alert alert-danger" style="margin-left: 13px; padding: 5px;">{{$errors->first('subcategoryid')}}</span>
                                        @endif
                                    </div>
                                    <div class="col-lg-6">
                                        <div class="col-lg-12">
                                            <div class="form-group">
                                                <label><strong>Property Type (WholeUnit/Room/Landed)</strong></label>
                                                <input type="text" class="form-control" name="productname" placeholder="Enter Your Property">
                                            </div>
                                        </div>
    
                                        <label></label>
                                        @if($errors->has('productname'))
                                        <span class="alert alert-danger" style="margin-left: 13px; padding: 5px;">{{$errors->first('productname')}}</span>
                                        @endif
    
                                    </div>
                                </div>
    
                                <div class="row">
                                    <div class="col-lg-6">
                                        <div class="col-lg-12">
                                            <div class="form-group">
                                                <label><strong>Year of Purchase</strong></label>
                                                <input type="text" class="form-control" name="yearofpurchase" placeholder="Year of Purchase">
                                            </div>
                                        </div>
    
                                        <label></label>
                                        @if($errors->has('yearofpurchase'))
                                        <span class="alert alert-danger" style="margin-left: 13px; padding: 5px;">{{$errors->first('yearofpurchase')}}</span>
                                        @endif
    
                                    </div>
                                    <div class="col-lg-6">
                                        <div class="col-lg-12">
                                            <div class="form-group">
                                                <label><strong>Price</strong></label>
                                                <input type="text" class="form-control" name="expsellprice" placeholder="Expected Selling Price">
                                            </div>
                                        </div>
    
                                        <label></label>
                                        @if($errors->has('expsellprice'))
                                        <span class="alert alert-danger" style="margin-left: 13px; padding: 5px;">{{$errors->first('expsellprice')}}</span>
                                        @endif
    
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-lg-6">
                                        <div class="col-lg-12">
                                             <div class="form-group">
                                                <label><strong>Name</strong></label>
                                                <input type="text" class="form-control" name="name" placeholder="Your Name">
                                                </div>
                                            </div>
    
                                        <label></label>
                                        @if($errors->has('name'))
                                        <span class="alert alert-danger" style="margin-left: 13px; padding: 5px;">{{$errors->first('name')}}</span>
                                        @endif
    
                                    </div>
                                    <div class="col-lg-6">
                                        <div class="col-lg-12">
                                            <div class="form-group">
                                                <label><strong>Mobile Number</strong></label>
                                                <input type="text" class="form-control" name="mobile" placeholder="Your Mobile">
                                            </div>
                                        </div>
    
                                        <label></label>
                                         @if($errors->has('mobile'))
                                        <span class="alert alert-danger" style="margin-left: 13px; padding: 5px;">{{$errors->first('mobile')}}</span>
                                        @endif
    
    	                            </div>
    
                                 </div>
    
    
                                 <div class="row">
                                    <div class="col-lg-6">
                                        <div class="col-lg-12">
                                            <div class="form-group">
                                                <label><strong>Email</strong></label>
                                                <input type="text" class="form-control" name="emailid" placeholder="Enter Your Email">
                                                </div>
                                            </div> 
    
                                        <label></label>
                                        @if($errors->has('emailid'))
                                        <span class="alert alert-danger" style="margin-left: 13px; padding: 5px;">{{$errors->first('emailid')}}</span>
                                        @endif
    
                                    </div>
                                    <div class="col-lg-6">
                                        <div class="col-lg-12">
                                            <div class="form-group">
                                                <label><strong>Location</strong></label>
                                                <select class="form-control" name="location">
                                                        <option value="">Select</option>
                                                        @if(count($locations)>0)
                                                            @foreach($locations as $location)
                                                            <option value={{$location->id}}>{{$location->locationName}}</option>
                                                        @endforeach
                                                    @else
    
                                                    @endif                                                     
                                                </select>
                                            </div>
                                        </div>
                                        <label></label>
                                         @if($errors->has('location'))
                                        <span class="alert alert-danger" style="margin-left: 13px; padding: 5px;">{{$errors->first('location')}}</span>
                                        @endif
                                        
                                    </div>
    
                                 </div>
    
                                    <div class="row">
                                        <div class="col-lg-6">
                                            <div class="col-lg-12">
                                                <div class="form-group">
                                                <label><strong>City</strong></label>
                                                <input type="text" class="form-control" name="city" placeholder="Enter Your City">
                                                </div>
                                            </div> 
    
                                        <label style="padding: 23px;"></label>
                                        @if($errors->has('city'))
                                        <span class="alert alert-danger" style="margin-left: 13px; padding: 5px;">{{$errors->first('city')}}</span>
                                        @endif
    
    
                                    </div>
                                    <div class="col-lg-6">
                                        <div class="col-lg-12">
                                            <div class="form-group">
                                                <label><strong>Upload Photos (Max 4)</strong></label>
                                                <input type="file" class="form-control" name="photos[]" multiple="true">
                                            </div>
                                        </div>
    
                                        <label style="padding: 23px;"></label>
                                        @if($errors->has('photos'))
                                        <span class="alert alert-danger" style="margin-left: 13px; padding: 5px;">{{$errors->first('photos')}}</span>
                                        @endif
    
                                    </div>
                                </div>
    
                                <div class="row">
                                        <div class="col-lg-12">
                                            <div class="form-group" style="text-align: center;">
                                            <button type="submit" class="btn btn-primary">Post Ad</button>
                                            <button id="reset" class="btn btn-default">Reset</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </form>
                        </div>
                    </div>
                  </div>  
                </div>
            </div>
        </div> 
    </div>         
    

    @endsection

VenkatAdapa's avatar

Sorry guy's missing piece of code at top of my UsersController.blade.php. I am not getting any error. data is not saving into db and no errors at all. I need your urgent help to resolve this . Pls help. I am new to this laravel an php

public function postProperty(Request $request){ $this->validate($request, [ 'subcategoryid' => 'required', 'productname' => 'required', 'yearofpurchase' => 'required', 'expsellprice' => 'required', 'name' => 'required', 'mobile' => 'required', 'emailid' => 'required', 'location' => 'required', 'city' => 'required', 'photos' => 'required', 'photos.*' => 'image|mimes:jpg,png,jpeg,gif,svg|max:2048'

        ]);
        $ads = new Advertisement;
        $images = $request->file('photos');
        $count = 0;
        if($request->file('photos')){
            foreach($images as $item){
                if($count < 4){
                    $var = date_create();
                    $date = date_format($var, 'Ymd');
                    echo $imageName = $date.'_'.$item->getClientOriginalName();
                    $item->move(public_path().'/uploads/',$imageName);
                    $url = URL::to("/").'/uploads/'.$imageName;
                    $arr[] = $url;
                    $count++;

                }
                
            }
            $image = implode(",", $arr);
            $ads->maincategoryid = $request->input('maincategoryid');
            $ads->subcategoryid = $request->input('subcategoryid');
            $ads->productname = $request->input('productname');
            $ads->yearofpurchase = $request->input('yearofpurchase');
            $ads->expsellprice = $request->input('expsellprice');
            $ads->name = $request->input('name');
            $ads->mobile = $request->input('mobile');
            $ads->emailid = $request->input('emailid');

            $ads->location = $request->input('location');
            $ads->city = $request->input('city');
            $ads->photos = $image;
            $ads->save();
            return redirect('/')->with('info', 'Advertisement published successfully.');
iftekhs's avatar

What kind of behavior it's giving you? I mean what exactly is happening when you submit the form. does it redirect you back? Try

dd('passed');

this code on top of your controller method/function and check if the form is actually hitting your controller.

VenkatAdapa's avatar

@iftekhs Thanks for your reply. In my project(website) multiple pages are there. Everything working perfectly except form data. when I press Post Ad button after filled all data into form then page got refreshed and then form fields are empty. nothing happened no errors at all and data is not saving and no ad is posted. The page redirect back to same page(just like refreshed the page). I have no idea what kind of behavior is this?. The project done by one of youtuber it is perfectly working for him. It does not work for me. Please help me. if someone can help remotely. I am happy for that. Thank you.

iftekhs's avatar

@VenkatAdapa I think something is redirecting you back. I suggest using dd('passed') on each step of your controller for example after the route hits the controller and after you perform $ads->save(); or before it like this check-in which step of the process, it is redirecting you back.

Make you use the dd function after you performed $ads->save(); and then check your database if the data is inserted.

VenkatAdapa's avatar

@iftekhs Thank you so much for time to reply to my post. You are right. It is a format of the image. I can upload png format image and ad successfully post it to my site but I am using any jpeg or jpg format images not able to postad. same behavior as before as it is going to refresh and nothing happened. this is make me cry. have spent 10 days of my time to resolve this issue. finally found the error as the format of the image. how could I resolve this? Are there any solutions? please let me know. Thanks

iftekhs's avatar

@VenkatAdapa Well, I suggest you take a different way to save your files. also instead of creating your $date like that, you can just simply use the time() function for it.

$filename = time().'.'.$item->getClientOriginalExtension();
$item->move(public_path('uploads'), $filename);

// -- Also you can use this ↓ --

  if($item)
   {
       $image_name = $item->getClientOriginalName();
       $filename = pathinfo($image_name,PATHINFO_FILENAME);
       $image_ext = $item->getClientOriginalExtension();
       $fileNameToStore = $filename.'-'.time().'.'.$image_ext;
       $path =  $item->storeAs('public/uploads',$fileNameToStore);
      
   }

You can also check this laracasts discuss on how you can save in a different way -> https://laracasts.com/discuss/channels/general-discussion/cant-move-uploaded-file-to-public-folder-laravel-6?page=1&replyId=575970

VenkatAdapa's avatar

dd('passed'); I have put this code on top of my user controller and it is passed.

huyvuong's avatar

I noticed that if condition had no closing } and with that if condition you have to upload images whenever you want to test.

1 like
VenkatAdapa's avatar

Hi Thanks for your reply. after change too many things and tried too many ways then now I can post ad. However, now the code not allow me to upload image formats like jpeg, jpg but it is allowing me only png format image. very strange behavior. can you advise what is will be the problem looking at my code at below

public function postProperty(Request $request){ $this->validate($request, [ 'subcategoryid' => 'required', 'productname' => 'required', 'yearofpurchase' => 'required', 'expsellprice' => 'required', 'name' => 'required', 'mobile' => 'required', 'emailid' => 'required', 'location' => 'required', 'city' => 'required', 'photos' => 'required', 'photos.*' => 'image|mimes:jpg,jpeg,png,gif,svg|max:2048']);

            $ads = new Advertisement;
            $images = $request->file('photos');
            $count = 0;
            if($request->file('photos')){
                foreach($images as $item){
                if($count < 4){
                    $var = date_create();
                    $date = date_format($var, 'Ymd');
                    $imageName = $date.'_'.$item->getClientOriginalName();
                    $item->move(public_path().'/uploads/',$imageName);
                    $url = URL::to("/").'/uploads/'.$imageName;
                    $arr[] = $url;
                    $count++;
        }

    }
      
 }                                     
            $image = implode(",", $arr);
            $ads->maincategoryid = $request->input('maincategoryid');
            $ads->subcategoryid = $request->input('subcategoryid');
            $ads->productname = $request->input('productname');
            $ads->yearofpurchase = $request->input('yearofpurchase');
            $ads->expsellprice = $request->input('expsellprice');
            $ads->name = $request->input('name');
            $ads->mobile = $request->input('mobile');
            $ads->emailid = $request->input('emailid');
            $ads->location = $request->input('location');
            $ads->city = $request->input('city');
            $ads->photos = $image;
            $ads->save();
            return redirect('/')->with('info', 'Advertisement published successfully.');
        }
VenkatAdapa's avatar

Small piece of code in the propertyads.blade.php (form data). Is there anything wrong

photos(Max 4)
                                    <label style="padding: 23px;"></label>
                                    @if($errors->has('photos'))
                                    <span class="alert alert-danger" style="margin-left: 13px; padding: 5px;">{{$errors->first('photos')}}</span>
                                    @endif

                                </div>
                            </div>

Please or to participate in this conversation.