0

I want to migrate a file in MySQL database but I can't seem to get the data type of the file right in Laravel. Help if you can please

I have read Link but it does not solve my problem.

I am a beginner so kindly be patient with me if you find this question immature (We all must start from somewhere remember)

An excerpt of my migration is below:

public function up()
{
    Schema::create('student', function (Blueprint $table) {
        $table->id();
        **$table->string('file');**
        $table->string('semester');
        $table->timestamps();
    });
}

this is my request file also

 {
    return [
        'file' => 'required',
        'semester' => 'required|string|max:255',
    ];
}

This is also my controller file

public function store(CEStoreRequest $request)
{
    CE::create([
        'file' => $request->file('file'),
        'semester' => $request->semester,
    ]);

    return redirect()->route('home.index')->with('success', 'Project created successfully');
}
Kennedy Owusu
  • 5,690
  • 4
  • 15
  • 20
  • 1
    Have you checked the documentation on File Storage in Laravel? https://laravel.com/docs/8.x/filesystem. Typically, you upload your file _somewhere_, like a local folder within Laravel's `storage/framework`, or an external Service like Amazon S3, then simply store the file's information in the database, typically a string for the filename/storage location, and some metadata like filetype, filesize etc. – Tim Lewis Jan 24 '22 at 19:55
  • I am not sure, but maybe the string`s size is too small for the file, maybe you need bigger? you can choose data types here https://laravel.com/docs/8.x/migrations#available-column-types – Vaso Gamdelidze Jan 24 '22 at 19:55
  • Let me check the documentation as suggested by @TimLewis – Kennedy Owusu Jan 24 '22 at 20:36
  • @VasoGamdelidze I appreciate your comment. – Kennedy Owusu Jan 24 '22 at 20:37

1 Answers1

0

Usually the datatype should be just a varchar since you will store the name of file and the path of the file (where it will store the file in local folder). So it's better for you to add another field name called 'path'. To store the file:

$file = $request->file;
$originalName = $file->getClientOriginalName();
$path = storage_path()."/savemyimage/";

CE::create([
        'file'     => $originalName,
        'semester' => $request->semester,
        'path'     => "app/savemyimage/".$fileName,
    ]);
zerhez
  • 28
  • 6