I'm trying to Import an excel file and save the info into the database table. Right now I'm getting an error
The POST method is not supported for this route. Supported methods: GET, HEAD, PUT, PATCH, DELETE.
All the tutorials I saw didn't get this error and I'm doing it the same way, I don't know what the issue is. I'm using this package "maatwebsite/excel": "~2.1.3".
This is my vue
<el-form :action="'impteachers/import'" method="post" enctype="multipart/form-data">
<el-input type="file" name="file"/>
<el-input type="submit" value="upload"/>
</el-form>
This is the controller
public function import(Request $request)
{
$request->validate([
'file' => 'required|mimes:xls,xlsx'
]);
$path = $request->file('file')->getRealPath();
$data = Excel::load($path)->get();
if ($data->count()) {
foreach ($data as $key => $value){
$arr[] = [
'NOMBRE' => $value->name,
'CEDULA' => $value->card,
'CARNET' => $value->scard,
'TIPO-USUARIO' => $value->user_type_id,
'CORREO' => $value->email,
'PASSWORD' => $value->password,
];
}
if (!empty($arr)) {
User::insert($arr);
}
}
return redirect('/imports');
}
These are the routes in web.php
Route::resource('impteachers', 'ImportTeacherController');
Route::post('impteachers/import', 'ImportTeacherController#import');
What am I doing wrong?
Try submitting to '/impteachers' only like this
<el-form :action="'/impteachers'" method="post" enctype="multipart/form-data">
<el-input type="file" name="file"/>
<el-input type="submit" value="upload"/>
</el-form>
And set your route to this
Route::post('/impteachers', 'ImportTeacherController#import');
Related
i need to create a condition that clears the record but with a password.
if the password is correct execute the delete();
controller:
public function eliminar($id){
$registros = \App\Models\Registro::findOrFail($id);
$registros->delete();
return redirect('sistema')->with('mensaje', 'Registro Borrado con exito');
}
public function borrar($id){
// return $request->all();
$data = [
'category_name' => 'datatable',
'page_name' => 'multiple_tables',
'has_scrollspy' => 0,
'scrollspy_offset' => '',
'fechax' => Carbon::now(),
'borrar' => \App\Models\Registro::findOrFail($id),
'password' => 'PASSCODE',
];
return view('borrar')->with($data);
}
blade.php:
<h1>Do you want to delete the record?</h1>
<form action="{{ route('eliminar', $borrar) }}" class="d-inline" method="POST">
#method('DELETE')
#csrf
<button type="submit" class="btn btn-danger btn-sm">DELETE</button>
<div class="form-group col-md-6">
<label for="telefono">Password</label>
<input name="password" type="password" class="form-control" id="telefono" required>
</div>
</form>
the password is obtained statically
How can I make it delete without the password is identical?
help please
If the password is saved statically, inside in a variable, the following should do the job for you.
routes/web.php
Route::delete('/path/here', 'SomeController#destroy');
SomeController.php
public function destroy($id)
{
$model = YourModel::find($id);
if (! $model) {
session()->flash('error_message', 'Model not found with the given id: ', . $id);
return back();
}
// $password is the password that you have saved somewhere
if (request()->password_field_value == $password) {
$model->delete();
session()->flash('success_message', 'Model deleted successfully.');
return back();
}
session()->flash('error_message', 'Invalid password. Try again');
return back();
}
I try to create crud in laravel 6. Create, Read and Delete process is running well. But when Update process, the data in table not change. Could anyone help me to find the problem ? The following my code.
Route
Route::get('/blog', 'BlogController#index');
Route::get('/blog/add','BlogController#add');
Route::post('/blog/store','BlogController#store');
Route::get('/blog/edit/{id}','BlogController#edit');
Route::post('/blog/update','BlogController#update');
Controller
public function index()
{
$blog = DB::table('blog')->get();
return view('blog',['blog' => $blog]);
}
public function edit($id)
{
$blog = DB::table('blog')->where('blog_id', $id)->get();
return view('edit', ['blog'=>$blog]);
}
public function update(Request $request)
{
DB::table('blog')->where('blog_id',$request->blog_id)->update([
'blog_title' => $request->title,
'author' => $request->author]);
return redirect('/blog');
}
View
#foreach ($blog as $n)
<form method="post" action="/blog/update" />
{{ csrf_field() }}
Title <input type="text" name="title" value="{{ $n->title}}">
Author<input type="text" name="author" value="{{ $n->author}}">
<button type="submit" class="btn btn-secondary">Update</button>
</form>
#endforeach
You must provide id in your route
Route::post('/blog/update/{id}','BlogController#update');
In update method add parameter id and then find product against id
public function update(Request $request, $id)
{
DB::table('blog')->where('blog_id',$id)->update([
'blog_title' => $request->title,
'author' => $request->author]);
return redirect('/blog');
}
#foreach ($blog as $n)
<form method="post" action="{{ route('your route name'), ['id' => $$n->id] }}" />
{{ csrf_field() }}
Title <input type="text" name="title" value="{{ $n->title}}">
Author<input type="text" name="author" value="{{ $n->author}}">
<button type="submit" class="btn btn-secondary">Update</button>
</form>
#endforeach
try separating the update into two statements like so
$blog = DB::table('blog')->where('blog_id',$id)->first();
$blog->update([
'blog_title' => $request->title,
'author' => $request->author]);
Also you might want to use models in the future so you can do it like
$blog = Blog::where('blog_id',$id)->first();
Doesn't really shorten your code but it improves the readibility.
Do your update like this:
public function update(Request $request)
{
$post = DB::table('blog')->where('blog_id',$request->blog_id)->first();
$post->blog_title = $request->title;
$post->author = $request->author;
$post->update();
return redirect('/blog');
}
I want to upload multiple files in Laravel but it's not working I tried in my controller
Edit: I means page refresh but no error or registred data in the db
I followed https://www.youtube.com/watch?v=wk5usyevYj0
public function storescan(Request $request){
$this->validate($request,[
'file' => 'required|file|mimes:gif,jpg,png,jpge,jpeg|max:102048'
]);
$files=$request->file('file');
foreach($files as $file){
Document::create([
'doc'=>$file->getClientOriginalName(),
'path'=>$file->store('public/storage'),
'support'=>$request->input('support'),
'ecole'=>$request->input('ecole'),
'jour'=>$request->input('jour'),
'cycle'=>$request->input('cycle'),
'type'=>'Image',
'niveau'=>$request->input('niveau'),
'matiere'=>$request->input('matiere'),
'user_id'=>Auth::user()->id
]);
}
return redirect()->route('prof.document.create')->withMessage('✅ Image(s) ajoutée(s) avec succès!');
}
And here is my route and formular
Route::post('/professeur/myDoccy/scan/create','DocumentController#storescan')->name('prof.document.createscan');
Formular everything is correct I minimized the code and left only the input file
<form enctype="multipart/form-data" class="form" action="{{ route('prof.document.createscan') }}" method="POST">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
{{ csrf_field() }}
<label class="purple" >Charger l’image </label><br/>
<input required type="file" multiple name="file[]" accept='image/*' class="form-control-file border-or m-1" >
</div>
</form>
You should check $errors variable (read more https://laravel.com/docs/5.8/validation).
You have problem with validation. You have rule
'file' => 'required|file|mimes:gif,jpg,png,jpge,jpeg|max:102048'
But it will work for single file. You have multiple of them, so it should be:
'file.*' => 'required|file|mimes:gif,jpg,png,jpge,jpeg|max:102048'
Also, you can use "image" rule instead of file with all mimes.
I solved the issue by deleting the validate part
And adding to all fillable attributes in my model but still no validation possibility
class Document extends Model
{
use SoftDeletes;
public function user(){
return $this->belongsTo('App\User');
}
protected $fillable = [
'doc','support','ecole',
'path',
'jour',
'cycle',
'type',
'niveau',
'matiere',
'user_id'
];
}
function
public function storescan(Request $request){
$files=$request->file('file');
foreach($files as $file){
Document::create([
'doc'=>$file->getClientOriginalName(),
'path'=>$file->store('public/storage'),
'support'=>$request->input('support'),
'ecole'=>$request->input('ecole'),
'jour'=>$request->input('jour'),
'cycle'=>$request->input('cycle'),
'type'=>'Image',
'niveau'=>$request->input('niveau'),
'matiere'=>$request->input('matiere'),
'user_id'=>Auth::user()->id
]);
}
return redirect()->route('prof.document.create')->withMessage('✅ Image(s) ajoutée(s) avec succès!');
}
I want to upload an image using Laravel storage file system in my admin data. However, there's an error when I attempt to upload an image.
Call to a member function getClientOriginalName() on null
Controller
public function store(Request $request)
{
$admin = $request->all();
$fileName = $request->file('foto')->getClientOriginalName();
$destinationPath = 'images/';
$proses = $request->file('foto')->move($destinationPath, $fileName);
if($request->hasFile('foto'))
{
$obj = array (
'foto' => $fileName,
'nama_admin' => $admin['nama_admin'],
'email' => $admin['email'],
'jabatan' => $admin['jabatan'],
'password' => $admin['password'],
'confirm_password' => $admin['confirm_password']
);
DB::table('admins')->insert($obj);
}
return redirect()->route('admin-index');
}
View
<div class="form-group">
<label for="" class="col-md-4">Upload Foto</label>
<div class="col-md-6">
<input type="file" name="foto">
</div>
</div>
Error
You can check wheather you are getting file or not by var_dump($request->file('foto')->getClientOriginalName());
And make sure your form has enctype="multipart/form-data" set
<form enctype="multipart/form-data" method="post" action="{{ url('/store')}}">
<div class="form-group">
<label for="" class="col-md-4">Upload Foto</label>
<div class="col-md-6">
<input type="file" name="foto">
</div>
</div>
</form>
Error because of client Side
<form enctype="multipart/form-data" method="post" action="{{ url('/store')}}">
<div class="form-group">
<label for="" class="col-md-4">Upload Foto</label>
<div class="col-md-6">
<input type="file" name="foto">
</div>
</div>
</form>
you ned to add enctype="multipart/form-data" inside the form
If You are using the form builder version
{!! Form::open(['url' => ['store'],'autocomplete' => 'off','files' => 'true','enctype'=>'multipart/form-data' ]) !!}
{!! Form::close() !!}
Then In your Controller You can check if the request has the file
I have Created the simple handy function to upload the file
Open Your Controller And Paste the code below
private function uploadFile($fileName = '', $destinationPath = '')
{
$fileOriginalName = $fileName->getClientOriginalName();
$timeStringFile = md5(time() . mt_rand(1, 10)) . $fileOriginalName;
$fileName->move($destinationPath, $timeStringFile);
return $timeStringFile;
}
And the store method
Eloquent way
public function store(Request $request)
{
$destinationPath = public_path().'images/';
$fotoFile='';
if ($request->hasFile('foto'))
{
$fotoFile= $this->uploadFile($request->foto,$destinationPath );
}
Admin::create(array_merge($request->all() , ['foto' => $fotoFile]));
return redirect()->route('admin-index')->with('success','Admin Created Successfully');
}
DB Facade Version
if You are using DB use use Illuminate\Support\Facades\DB; in top of your Controller
public function store(Request $request)
{
$admin = $request->all();
$destinationPath = public_path().'images/';
$fotoFile='';
if ($request->hasFile('foto'))
{
$fotoFile = $this->uploadFile($request->foto,$destinationPath );
}
$obj = array (
'foto' => $fotoFile,
'nama_admin' => $admin['nama_admin'],
'email' => $admin['email'],
'jabatan' => $admin['jabatan'],
'password' => $admin['password'],
'confirm_password' => $admin['confirm_password']
);
DB::table('admins')->insert($obj);
return redirect()->route('admin-index');
}
Hope it is clear
I want to delete only checked tasks. At the moment I have this:
<form method="POST" action="/destroy">
#foreach($tasks as $t)
<label>
<input type="checkbox" name="checked[]" value="$t->id">
</label>
#endforeach
<button type="submit">Submit!</button>
</form>
This is my Controller
public function destroy(Request $request)
{
$this->validate($request, [
'checked' => 'required',
]);
$checked = $request->input('checked');
Task::destroy($checked);
}
And this is my route
Route::post('/destroy', [
'uses' => 'Controller#destroy',
]);
I don't get no error but the system does not work
I fixed the problem! Thanks for your support!
The problem was that my id variable was a hash.