Muhammad Arslan Muhammad Arslan - 6 months ago 17
MySQL Question

Failed to save file name in database in laravel

this is my addData method in UploadController

public function addData(Request $request)
{
$this->validate($request, [
'name' => 'required|min:1',
'email' => 'required|min:2',
]);

if(Input::hasFile('file'))
{
$file = array('file' => Input::file('file'));
$rules = array('file' => 'required|mimes:jpg,png,jpeg');
$validator = Validator::make($file, $rules);

if ($validator->fails()) {
echo 'Not allowed!!';
}
else
{
if (Input::file('file')->isValid()) {
$destinationPath = 'uploads';
$name = Input::get('name');
$email = Input::get('email');
$extension = Input::file('file')->getClientOriginalExtension();
$fileName = Input::file('file')->getClientOriginalName();
$displayImage = Input::file('file')->move($destinationPath, $fileName);
$user = Profile::create([
'name' => $name,
'email' => $email,
'file_name' => $fileName,
]);

$fileName]));

echo 'Upload Succesfully <br>';
}

else {
Session::flash('error', 'uploaded file is not valid');
return Redirect::to('upload');
}
}
}
else
{
echo 'Nothing to upload';

}

}


this is working fine but when i change to this it save name and email but not file name what is the problem with this i am failed to find out problem

Profile::create($request->all(['file_name' => $fileName]));


it is possible that it take everything automatically with filename or not with
$request->all() function any other solution if you have for me or t is necessary to write every field name ?

Answer

Try to do it like this:

$request['file_name'] = $fileName;
Profile::create($request->all());