Laravel download files with two tables in database

405 views Asked by At

I hope so im getting close to final stage. I uploading file to storage/files/ and create uniq folder for each upload file with id_message without problem and store file data in table files

Final path of file is /storage/files/{id_message}/{file_name} both variables id_message and file_name are in table files.

FileController function for fileUpload:

    function fileUpload(Request $request)
    {
    $request->validate([
        'id_message' => 'required|min:6'
    ]);
    
    $idParameter = $request->id_message=$request->id_message;

    $result=$request->file('file_path')->store('files/'.$idParameter);

    $file = new File;
    $file->id_message=$idParameter;
    $file->file_path=$result;
    $file->file_name=$request->file('file_path')->getClientOriginalName();
    $file->save();

after upload i have this data in table files:

id    id_message    file_name
1     000001        Myfile.zip 

/storage/app/files/000001/Myfile.zip

FileController : getDownload

   public function getDownload($id)
   {
      $resultFile = DB::table('files')->where('id_message',$id)->first();

      $attachment = store_path().'/' . $resultFile->id_message . '/' . $resultFile->file_name;

      return response()->download($attachment);
   }

route

Route::get('download/{id}/{fileName}', 'FileController@getDownload')->name('downloadFile');

view.blade

   <td><a href="{{ route ('downloadFile', $resultFile->file_name)}}">Download</a></td> 

error

Undefined variable:resultFile

do i getting closer to finaly download file in laravel ?

controller for view table users

public function postLogin(Request $request)
{
    request()->validate([
        'id_message' => 'required',
        'sms_code' => 'required',
    ]);
    
    $credentials = $request->only('id_message', 'sms_code');
    $request->session()->flash('success','');
                
    if ($user=User::where($credentials)->first()) {
        auth()->login($user);
    
        return redirect()->intended('dashboard')->with('success','');
        }
        
    return Redirect::to("login")->with(['url_attribute' => $url_attribute,'id_message' => $id_message])->with('error','');
    }

public function dashboard()
{
  if(Auth::check())
  {
    return view('dashboard');
  }
   return Redirect::to("login")->withSuccess('Opps! You do not have access');
}
1

There are 1 answers

2
Richard Simpson On BEST ANSWER

Leading zeros may be getting stripped if its converted to integer.

Try adding padding to $id once it's passed to getDownload:

str_pad($id, 6, '0', STR_PAD_LEFT);