1

This is my web.php file and i have used the correct get and post methods and have specified the path correctly <?php

 use Illuminate\Support\Facades\Route; namespace App\Http\Controllers\FileController; use Illuminate\Support\Facades\Route; Route::get('/', function () { return view('welcome'); }); Route::post('store', [FileController::class,'store']); Route::view('imgupload','imageUpload'); 

blade file--imageUplods;where i get like 2 images and store it as a multiple entry in the db

<form method="post" action="store" enctype="multipart/form-data"> @csrf <div class="input-group realprocode control-group lst increment" > <input type="file" name="filenames" class="myfrm form-control" id="filenames"> <div class="input-group-btn"> <button class="btn btn-success" type="button"> <i class="fldemo glyphicon glyphicon-plus"> </i>Add</button> </div> </div> <div class="clone hide"> <div class="realprocode control-group lst input-group" style="margin-top:10px"> <input type="file" name="filenames" class="myfrm form-control" id="filenames"> <div class="input-group-btn"> <button class="btn btn-danger" type="button"><i class="fldemo glyphicon glyphicon-remove"> </i> Remove</button> </div> </div> </div> <button type="submit" class="btn btn-success" style="margin- top:10px">Submit</button> </form> </div> 

model: filemodel - filesnames is the only attribute to enter

<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class file extends Model { use HasFactory; protected $fillable = [ 'filenames' ]; public function setFilenamesAttribute($value) { $this->attributes['filenames'] = json_encode($value); } } 

Controller-FileController- imageUploder is the blade file

<?php namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; use Illuminate\Http\Request; use App\Models\File; use Illuminate\Support\Facades\Auth; class FileController extends Controller { /** * Show the application dashboard. * * @return \Illuminate\Http\Response */ public function create() { return view('imageUpload'); } /** * Show the application dashboard. * * @return \Illuminate\Http\Response */ public function store(Request $request) { $this->validate($request, [ 'filenames' => 'required', 'filenames.*' => 'image' ]); $files = []; if($request->hasfile('filenames')) { foreach($request->file('filenames') as $file) { $name = time().rand(1,100).'.'.$file->extension(); $file->move(public_path('files'), $name); $files[] = $name; } } $file= new File(); $file->filenames = $files; $file->save(); return redirect('imageUpload')->with('success', 'Your images has been successfully added'); } } 

2 Answers 2

0

You are mixing up 'use' and 'namespace'. 'namespace' is for defining the namespace of the current class

Change this line

namespace App\Http\Controllers\FileController; 

to

use App\Http\Controllers\FileController; 
Sign up to request clarification or add additional context in comments.

3 Comments

i get this when i change it to use Sorry! Here have some issue please check The filenames field is required......
That means your route works now, but you get a validation error...
You have 2 inputs that have the name filenames. The second input will overwrite the first. Note that they both have the same ID, A id must be unique for the document. If you want to upload multiple files, you can use name="filenames[]". You can do that with one field with the 'multiple' attribute. Your hidden clone element is inside the form, so that field is posted too.
0
  1. Change what Gert said about the namespace in web.php

  2. Change

class file extends Model 

to

class File extends Model 

2 Comments

web.php doen't have a namespace. The naming for the File class needs to change idd.
changed the file class name to"File"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.