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

laksh's avatar
Level 1

Doesn't save entry in database for file

i created form for saving image and fields data into database but it doesn't save the image data along with fields data How can i do that?

0 likes
33 replies
laksh's avatar
Level 1

Here' s the code of view file

I use dropzone for uploading file

@extends('layouts.header')  

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<script src="https://unpkg.com/dropzone@5/dist/min/dropzone.min.js"></script>
<link
	rel="stylesheet"
	href="https://unpkg.com/dropzone@5/dist/min/dropzone.min.css"
	type="text/css"
/>
@extends('posts.index')
<section class="section">
	<div class="row">
		<div class="col-lg-12">
			<div class="card">
				
					<div class="card-body">
						<h1 class="card-title">Add New Post</h1>
						
						@if(session('status'))
							<div class="alert alert-success">
								{{ session('status') }}
							</div>
						@endif
						<form method="post" enctype="multipart/form-data" action="{{ route('create_save') }}" class="row g-3">
							@csrf
							
							<div class="col-md-12">
								<label for="">Tittle</label>
								<input type="text" name="tittle" class="form-control">
							</div>
							
							<div class="col-md-4">
              					<label for="inputState" class="form-label">PostType</label>
              					<select id="inputState" name="post_type" class="form-select">
                					<option>Image</option>
                					<option>Vedio</option>
              					</select>
            				</div>
            				
            				<div class="col-md-12">
									<div class="dropzone" name="file" id="upload"></div>
							</div>
							
							<div class="col-md-12">
								<label for="">Boost</label>
								<input type="checkbox" name="boost" id="boost">
							</div>
							
							<div class="row mb-3" id="pay_per_click" style="display: none">
              					<label for="pay_per_click" class="col-sm-2 col-form-label" style="width: 70">Pay</label>
              					<div class="col-sm-4">
                					<input type="text" class="form-control" name="pay_per_click">
              					</div>
              					<label for="pay_per_click" class="col-sm-2 col-form-label">Per Click</label>
            				</div>
            				
            				<div class="row mb-3" id="pay_per_share" style="display: none">
              					<label for="pay_per_share" class="col-sm-2 col-form-label" style="width: 70">Pay</label>
              					<div class="col-sm-4">
                					<input type="text" class="form-control" name="pay_per_share">
              					</div>
              					<label for="pay_per_share" class="col-sm-2 col-form-label">Per Share</label>
            				</div>
							
							<div class="col-md-12">
								<label for="">Budget</label>
								<input type="text" name="budget" class="form-control">
							</div>
							
							<div class="text-center">
      							<button type="submit" id="submit" class="btn btn-primary">Submit</button>
      						</div>
						</form>
					</div>
				</div>
			</div>
		</div>
	</section>		
@extends('layouts.footer') Dropzone.autoDiscover = false;
$('#upload').dropzone({
	url: "{{ route('create_save') }}",
	method: "post",
	addRemoveLinks: true,
	acceptedFiles: ".jpg,.jpeg,.png",
	headers: {
		'X-CSRF-TOKEN': '{{ csrf_token() }}'
		} 
	});


jQuery("#boost").on("click",function(){
	if(jQuery(this).is(":checked")){
		jQuery("#pay_per_share").show();
		jQuery("#pay_per_click").show();
		
	}else{
		jQuery("#pay_per_share").hide();
		jQuery("#pay_per_click").hide();
		
		}
	});
laksh's avatar
Level 1

@Tray2 Here's the controller code for saving record

public function create_save(Request $request){

    $request->validate([
        'pay_per_click' => 'sometimes|required',
        'pay_per_share' => 'sometimes|required',
    ]);

    $creator = Auth::user()->name;
    
    $file = $request->file('file');
    
    if (!empty($file)){
        $fileName = $file->getClientOriginalName();
        $file->move(public_path('images'),$fileName);
    }
    
    $campaign = new NewOne();
    $campaign->tittle = $request->input('tittle');
    $campaign->budget = $request->input('budget');
    $campaign->pay_per_share = $request->input('pay_per_share');
    $campaign->pay_per_click = $request->input('pay_per_click');
    $campaign->created_by = $creator;
    if (isset($fileName)){
        $campaign->image_name = $fileName;
    }
    $campaign->post_type = $request->input('post_type');
    $campaign->save();
    
    return redirect('posts')->with('success');
    
}
CODE-AXION's avatar

can you show us the input logic ( the controller ) ?

Ben Taylor's avatar

It looks as though $fileName is not in the same scope as the isset($fileName) check, so will always return false and $campaign->image_name will always be null

laksh's avatar
Level 1

@Ben Taylor but if i remove isset it gives me the error for undefined variable How can i solve that problem?

Ben Taylor's avatar

@laksh add $fileName = null; to the top of the method (I.e. above all the if statements) so that it is in scope.

CODE-AXION's avatar

how bout this ?


    
    $campaign = new NewOne();
    $campaign->tittle = $request->tittle;
    $campaign->budget = $request->budget;
    $campaign->pay_per_share = $request->pay_per_share;
    $campaign->pay_per_click = $request->pay_per_click;
    $campaign->created_by = $creator;

    $file = $request->file('file');
    
    if (!empty($file)){
        $fileName = $file->getClientOriginalName();
        $file->move(public_path('images'),$fileName);
	
//or try to debug the variable like this : dd($fileName) or dd(isset($fileName)) 
// and see if it returns null or not
   		 if (isset($fileName)){
        	$campaign->image_name = $fileName;
   		 }
	
    }


    $campaign->post_type = $request->post_type;
    $campaign->save();

also no need for ->input()

laksh's avatar
Level 1

@CODE-AXION in this case only fields data is saving in database and image name is getting empty i want image name to save into database along with other form fields

CODE-AXION's avatar

@laksh did you debugged the variable of filename is it returning null ?

if (!empty($file)){
        $fileName = $file->getClientOriginalName();
        $file->move(public_path('images'),$fileName);
	
dd($fileName);
	
    }
laksh's avatar
Level 1

@CODE-AXION it doesn't work dd($fileName) and it directly saves the entry into database excluding image name

CODE-AXION's avatar

@laksh can you show me what results you actually want and what it's actually doing .

like can you show us what data is it saving into the database

laksh's avatar
Level 1

@CODE-AXION so basically i want that data of all form fields was saved into single entry including the name of image which we upload but it doesn't doing like that it doesn't saving the name of image into record

laksh's avatar
Level 1

@CODE-AXION id => 55

tittle => new post

budget => 9787545

pay_per_share => 1232

pay_per_click => 3323

created_by => laksh11

image_name => NULL

post_type => Image

Here 's a single entry

1 like
CODE-AXION's avatar

@laksh $fileName = $file->getClientOriginalName(); so if this part is not returning null what its actually returning ? the file name or something else ?

laksh's avatar
Level 1

@CODE-AXION it is not returning null the thing is that when i upload a file it saves that file immediately without submitting the form and create entry for image into databse but when i clicked on submit button and the form submits but then image is already submitted

1 like
laksh's avatar
Level 1

@CODE-AXION No, in this only file is saving into db i want to save fields data along with the image name into db

CODE-AXION's avatar

@laksh i think its probably a plugin issue just try without plugin and see if everything works perfectly ...

if it works without plugin than maybe we have to add some kind of parameter in the plugin or it might have a different method to do it with server side

laksh's avatar
Level 1

@CODE-AXION yeah, it is working like that how i want without using plugin but i have to use plugin for the dropzone view so how should i do that?

laksh's avatar
Level 1

@CODE-AXION But in my project we need dropzone filepond doesn't use

Tray2's avatar

@laksh If you are uploading the image in some other way, other than sending it along with the request, the file will always be null.

Then you need to pass the filename some other way.

This code is never ran

$file = $request->file('file');
    
    if (!empty($file)){
        $fileName = $file->getClientOriginalName();
        $file->move(public_path('images'),$fileName);
    }
laksh's avatar
Level 1

@Tray2 no basically there are two requests generating one is when we click on dropzone and select file then a request is generate and file is uploaded and second request generates when we submit the form and at this request all form data is saved except of file name

laksh's avatar
Level 1

@Tray2 can you suggest any other way for passing the filename?

Tray2's avatar

@laksh You need to pass that in the request.

You can probably get the filename from the dropbox component.

laksh's avatar
Level 1

@Tray2 umm i don't understand what do you mean to say about dropbox component how can i get from that?

Tray2's avatar

@laksh Read the manual, there must be a file name property somewhere.

Please or to participate in this conversation.