NotFoundHttpException in RouteCollection.php line 161 in laravel 5.3 - laravel

none of solutions on net, didn't solve my problem.how can i correct this error?
form.blade.php:
<div class="row">
#include('admin.partials.errors')
<div class="col-xs-12 col-md-6">
<form action="{{ url('file') }}" name="POST" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group">
<label for="file_title">عنوان فایل:</label>
<input type="text" class="form-control" name="file_title" id="file_title" value="{{ old('file_title', isset($fileItem)? $fileItem->file_title:'') }}">
</div>
<div class="form-group">
<label for="file_description">توضیحات فایل:</label>
<textarea name="file_description" class="form-control" id="file_description" cols="30" rows="10" value="{{ old('file_description', isset($fileItem)? $fileItem->file_description:'') }}"></textarea>
</div>
<div class="form-group">
<label for="file_title">فایل:</label>
<input type="file" name="fileItem">
</div>
<div class="form-group">
<button class="btn btn-success" type="submit">ذخیره اطلاعات</button>
</div>
</form>
</div>
</div>
create.blade.php
#extends('layouts.admin')
#section('content')
#include('admin.file.form')
#endsection
FilesController.php
public function create()
{
return view('admin.file.create')->with('panel_title', 'ایجاد فایل جدید');
}
public function store(Request $request)
{
dd($request->all());
}
public function edit()
{
}
web.php
Route::get('/file','FilesController#index')->name('admin.file.list)//admin/user
Route::get('/file/create', 'FilesController#create')->name('admin.file.create');//admin/user
Route::post('/file/store','FilesController#store')->name('admin.file.store');//admin/file
Route::get('/file/edit/{file_id}','FilesController#edit')->name('admin.file.edit');//admin/file
Route::post('/file/edit/{file_id}','FilesController#update')->name('admin.file.update');//admin/file
});
when i click on save button to store file details on create file page in admin panel, i get this error!

i think you have to change this line
Route::post('/file/store','FilesController#store')->name('admin.file.store');//admin/file
to this
Route::post('/file','FilesController#store')->name('admin.file.store');//admin/file

Try changing
<form action="{{ url('file') }}" name="POST" method="post" enctype="multipart/form-data">
to
<form action="{{ url('file/store') }}" name="POST" method="post" enctype="multipart/form-data">

For Upload file on Laraval Public/Storage
Put this Code in Controller
FilesController.php
public function store(Request $request)
{ //after file store
return redirect(url('//**Put your Own url which you want**'));
}
Or Give another url which present in Your Web.php

Related

a form with the PUT method is sent with the GET method

I have 2 controller with resource clients and sumsubs.
being on the clients.show route I created a form that I would like to send to the sumsubs.update endpoint but it does not work; the form still contacts sumsubs.show
here are the routes that I have defined
here is the form
<form accept-charset="UTF-8" class="form-horizontal" name='recheck' method="PUT" action="{{ route('sumsubs.update', $client->id) }}">
#csrf
<div class="form-group row">
<div class="col-sm-10">
<input type="number" class="form-control" id="candidat" name="candidat" value="{{ $client->id }}" hidden required>
</div>
</div>
<div class="card-footer">
<button type="submit" class="btn btn-success float-right"> {{ __('Recheck') }} </button>
</div>
</form>
the following is my sumbsubs controller
public function show($id)
{
return 'not ok';
}
public function update(Request $request, $id)
{
return 'ok ';
}
this is what i get when i submit the form
I would like to know where is my mistake
I read the laravel documentation for form submission and tried with url and route methods but I still get the same result
You have to use method spoofing.
Change your method in form tag to Post:
<form accept-charset="UTF-8" class="form-horizontal" name='recheck' method="POST" action="{{ route('sumsubs.update', $client->id) }}">
and add this after #csrf:
#method('PUT')

PostsController#store not working when trying to create a post

This is my section and I want to send the form to the store function in the PostsController but when I hit submit it shows a 419 page expired however it should return to the posts page and the post should be stored in the database.
#section('content')
<form action="{{ action('PostsController#store') }}" method="POST">
<div class="form-group">
<label for="title">Title</label>
<input type="text" id="title" name="title" placeholder="Title">
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea id="body" name="body" placeholder="Body"></textarea>
</div>
<button type="submit" class="btn btn-default">Save</button>
</form>
#endsection
and this is my store function in the PostsController
public function store(Request $request)
{
//create post
$post = new Post;
$post->title = $request->input('title');
$post->body = $request->input('body');
$post->save();
return redirect('/posts');
}
The 419 page expired error generally comes from a csrf token issue.
Try adding #csrf within your form:
<form action="{{ action('PostsController#store') }}" method="POST">
#csrf
// etc
</form>

Laravel input loop

i try to looping my edit form. but the foreach loop doesn't work. anyone can help?
#foreach($siswas as $siswa)
<form id="editform" action="{{route(siswa.update, $siswa->id}}" method="post">
#method('PATCH')
#csrf
<div class="modal-body">
<div class="form-group">
<label for="nama" class="col-form-label">Nama</label>
<input type="text" value="{{$siswa->nama}}" id="nama" name="editNama" class="form-control" >
</div>
</div>
</form>
#endforeach
Several errors there:
route name should be a string (in quotes),
route parameter should be an associative array,
in provided example, closing parenthesis missed
(Assuming that parameter in Route is id), try with
{{ route('siswa.update', ['id' => $siswa->id]) }}
Laravel named routes.
You should try this:
your edit function like this:
public function edit($id)
{
$siswas = Yourmodel::find($id);
return view('yourviewpath',compact('siswas'));
}
Your view file like this:
<form id="editform" action="{{route(siswa.update, [$siswas->id]}}" method="post">
#method('PATCH')
#csrf
<div class="modal-body">
<div class="form-group">
<label for="nama" class="col-form-label">Nama</label>
<input type="text" value="{{ old('editNama', $siswas->nama) }}" id="nama" name="editNama" class="form-control" >
</div>
</div>
</form>
You should incorporate siswa in your form id:
#foreach($siswas as $siswa)
<form id="editform-{$siswa->id}" action="{{route(siswa.update, $siswa->id}}" method="post">
#method('PATCH')
#csrf
<div class="modal-body">
<div class="form-group">
<label for="nama-" class="col-form-label">Nama</label>
<input type="text" value="{{$siswa->nama}}" id="nama" name="editNama" class="form-control" >
</div>
</div>
</form>
#endforeach

Laravel | Delete function - how to delete photo from calendar's event

How can I remove photo from calendar's event in edit calendar's event view? In list of events I did delete method and it works. Now when I try to do the same in edit.blade.php it gives error:
Call to a member function photos() on null
I have two tables in relationship one calendar to many photos, file upload works, but I stucked on edit part.
Look at my controller function:
public function deletePhoto(CalendarRepository $calRepo, $id)
{
$calendars = $calRepo->find($id);
$calendars->photos($id)->delete();
return redirect()->action('CalendarController#edit');
}
and here is fragment of edit.blade.php:
<div class="form-group">
<label for="photo">Photo:</label>
<div class="row">
#foreach(($calendar->photos) as $photo)
<div class="col-md-3">
<div class="admin-thumbnail">
<img class="img-responsive" src="/storage/{{ $photo->filename }}" style="width:100px; height:auto;"/>
</div>
<i class="fas fa-times"></i>Remove
</div>
#endforeach
</div>
</div>
I need to remove photo from Photo table and redirect to edit.blade.php (about the specific event id of the calendar)
Thanks for any help.
EDIT:
<div class="card-body">
<form action="{{ action ('CalendarController#editStore')}}" method="POST" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{csrf_token() }}"/>
<input type="hidden" name="id" value="{{ $calendar->id }}"/>
<input type="hidden" name="_token" value="{{csrf_token() }}"/>
<div class="form-group">
<label for="photo">Photo:</label>
<div class="row">
#foreach(($calendar->photos) as $photo)
<div class="col-md-3">
<div class="admin-thumbnail">
<img class="img-responsive" src="/storage/{{ $photo->filename }}"/>
</div>
<form method="POST" action="{{ route('photo.delete', ['calendar' => $calendar, 'photo' => $photo]) }}">
#csrf
#method("DELETE")
<a onClick="return confirm('Are you sure?')"><i class="fas fa-times"></i>Remove</a>
</form>
</div>
#endforeach
</div>
</div>
<div class="form-group">
<label for="header">Header</label>
<input type="text" class="form-control" name="header" value="{{ $calendar->header }}"/>
</div>
<div class="form-group">
<label for="description">Description</label>
<input type="text" class="form-control" name="description" value="{{ $calendar->description }}"/>
</div>
<div class="form-group">
<label for="date">Date</label>
<input type="date" class="form-control" name="date" value="{{ $calendar->date }}"/>
</div>
<input type="submit" value="Save" class="btn btn-primary"/>
</form>
</div>
You use the same $id to find the photo and the calendar instance.
GET request is not recommended for deleting a resource, so a better approach would be in your routes you can have something like this:
Route::delete('photo/{photo}', 'PhotosController#delete')->name('photo.delete');
Then in your view, you should surround the button with a Form, for example:
<form method="POST" action="{{ route('photo.delete', $photo) }}">
#csrf
#method("DELETE")
<a onClick="return confirm('Are you sure?')"><i class="fas fa-times"></i>Remove</a>
</form>
Then your confirm function in JS should submit the form if the user accepts to delete the photo. And also remember to return false as default in the confirm function so it does not submits the form by default.
Your controller will then be:
public function delete(Photo $photo)
{
$photo->delete();
return redirect()->back();
}
--- EDIT
Route::delete('calendar/{calendar}/photo/{photo}', 'CalendarController#deletePhoto')->name('photo.delete');
and the action in the form can be:
{{ route('photo.delete', ['calendar' => $calendar, 'photo' => $photo]) }}
The method in the controller:
public function deletePhoto(Calendar $calendar, Photo $photo)
{
$calendar->photos()->where('id', $photo->id)->delete();
return redirect()->action('CalendarController#edit');
}

using route resource form did not post to store function

I have a form which upon successful validation should show the desired result. But on button press the browser displays The page has expired due to inactivity. Please refresh and try again.
view page
<form class="form-horizontal" method="POST" action="{{action('BookController#store')}}">
<div class="row" style="padding-left: 1%;">
<div class="col-md-4">
<div class="form-group">
<label>Book Name</label><span class="required">*</span>
<input type="text" maxlength="100" minlength="3" required="required" runat="server" id="txtBookName" class="form-control" autocomplete="off" autofocus="autofocus" />
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</div>
</form>
Route code
//web.php
Route::resource('book','BookController');
controller code
class BookController extends Controller
{
public function index()
{
//
}
public function create()
{
return view('pages.book');
}
public function store(Request $request)
{
$validatedInput = $request -> validate([
'txtBookName' => 'required|string|min:3|max:100'
]);
return $validatedInput;
}
}
Form url
http://127.0.0.1:8000/book/create
on submit button press, the page is redirected to
http://127.0.0.1:8000/book and it displays The page has expired due to inactivity. Please refresh and try again.
You can either post a CSRF token in your form by calling:
{{ csrf_field() }}
Or exclude your route in app/Http/Middleware/VerifyCsrfToken.php:
protected $except = [
'your/route'
];
Implement like this to avoid Expired message.
<form action="{{ action('ContactController#store') }}" method="POST">
#csrf <!-- this is the magic line, works on laravel 8 -->
<!--input components-->
<!--input components-->
<!--input components-->
</form>
you should use {{ csrf_field() }} in your form.
Please do this after the form class, because the resource take the #method('PUT')
<form class="form-horizontal" method="POST" action="{{action('BookController#store')}}">
{{csrf_field()}}
#method('PUT')
<div class="row" style="padding-left: 1%;">
<div class="col-md-4">
<div class="form-group">
<label>Book Name</label><span class="required">*</span>
<input type="text" maxlength="100" minlength="3" required="required" runat="server" id="txtBookName" class="form-control" autocomplete="off" autofocus="autofocus" />
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</div>
</form>

Resources