using route resource form did not post to store function - laravel

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>

Related

Error when trying to get property 'name' of non-object

hi i am trying to show the value of name which is stored in database but i am getting this error can anyone please guide me how can i rectify my error .
<form action="Store-data" method="POST">
#csrf
<div class="form-group my-2">
<input type="text" name="name" value="{{ $Task ->name }}">
<div class="form-group my-2">
<input type="email" name="email" placeholder="Enter your email" value="{{ $Task ->email }}" >
<div class="form-group my-2 text-center">
<button type="submit" class="btn btn-success"> Add user</button>
</div>
</div>
</div>
</form>
here is my edit controller
public function edit($Taskid)
{
//
$Edit= Superior::find($Taskid);
return view('Myview.edit')->with('Task','$Edit');
}
here is my route for edit
Route::get('Superior/{Task}/edit','SuperiorController#edit');
i am using Task as a key but it is giving me an error
i also tried but didn't work
{{$Task['name']}}
You should remove ' that wraps $Edit.
return view('Myview.edit')->with('Task', $Edit);
Change your code like this.
Form Code
<form action="Store-data" method="POST">
#csrf
<div class="form-group my-2">
<input type="text" name="name" value="{{isset($Edit) ? $Edit->name : '' }}">
<div class="form-group my-2">
<input type="email" name="email" placeholder="Enter your email" value="{{ isset($Edit) ? $Edit->email : '' }}">
<div class="form-group my-2 text-center">
<button type="submit" class="btn btn-success"> Add user</button>
</div>
</div>
</div>
</form>
Route
Route::get('Superior/edit/{id}','SuperiorController#edit');
Controller Code
public function edit($id){
$Edit= Superior::find($id);
return view('Myview.edit',compact('Edit'));
}
Hopefully it'll solve the problem
Create function like this. if Taskid in your database column.
public function edit(Request $Request, $Taskid)
{
//
$Edit= Superior::where('Taskid', $Taskid)->first();
return view('Myview.edit', compact(Edit));
}
In your html it will like this
{{ $Edit->name }}
In your Route
Route::get('Superior/{Taskid}/edit','SuperiorController#edit');

419 Sorry, your session has expired. Please refresh and try again.This error appears when i submit the form

I got this error even when I give {{ csrf_field() }} during form submission.
What could be the issue?
<form action="{{ route('e-account-post') }}" method="POST" id="e-account" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="row">
<div class="form-group col-lg-6 col-md-6 col-xs-12">
<div class="field-label">Full Name <span class="required">*</span></div>
<input type="text" class="form-control" name="full_name" value="" placeholder="Full Name">
</div>
<div class="col-12 text-center">
<button class="btn credit-btn mt-30" type="submit">Send</button>
</div>
</form>
below is route
Route::post('/e-account','FrontController#e_account_action')->name('e-account-post');
below is controller
public function e_account_action(Request $request)
{
dd($request->all());
}
Laravel 5.7 and the project is live.
There is no problem in local. I setup the project in live mode through GitLab.

NotFoundHttpException in RouteCollection.php line 161 in laravel 5.3

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

Laravel Update Function Not Running Correct Response

When I attempt to update a record in my Laravel application, it is running the wrong URL causing an error 404. This function was working fine when I was developing locally however now it is hosted on a one.com server, it has stopped working.
edit.blade.php
<form method="POST" action="gins/{{ $gins->id }}">
#method('PATCH')
#csrf
<div class="field">
<label class="label" for="gin">Gin</label>
<div class="control">
<input type="text" class="input" name="gin"
placeholder="Gin" value="{{ $gins->gin }}">
</div>
</div>
<div class="field">
<label class="label" for="size">Bottle Size(ml)</label>
<div class="control">
<input type="text" class="input" name="size"
placeholder="Size (ml)" value="{{ $gins->size }}">
</div>
</div>
<div class="field">
<label class="label" for="price">Price(£)</label>
<div class="control">
<input type="text" class="input" name="price"
placeholder="Price of Gin" value="{{ $gins->price }}">
</div>
</div>
<div class="field">
<div class="control">
<button type="submit" class="button is-success">Update Record
</button>
</div>
</div>
</form>
Route
Route::patch('gins/{gin}', 'PostsController#update')->middleware('auth');
Auth::routes();
Controller
public function update(Request $request, $id)
{
$gins = \App\Gins::findOrFail($id);
$gins->gin = request('gin');
$gins->size = request('size');
$gins->price = request('price');
$gins->save();
return redirect('gins');
}
The URL for the edit page is Laravel/gins/7/edit. When I click the submit button it's returning the URL Laravel/gins/7/gins/7 when it should be redirecting back to Laravel/gins/7.
The 7 in the Url is the record id from the particular record I'm attempting to update.
It's always a bad idea to hardcode urls like that. The following
<form method="POST" action="gins/{{ $gins->id }}">
in a route like laravel/gins/ would evaluate to laravel/gins/gins/7.
Also, routes change all the time in a dynamic web application. For this reason, I'd suggest you to use Named Routes.
For example:
Route::patch('gins/{gin}', 'PostsController#update')
->middleware('auth')
->name('posts.update');
and then change your form action to this:
<form method="POST" action="{{ route('posts.update', ['gin' => $gins->id]) }}">
I would also clean up your update() method a bit.
public function update(Request $request, $id)
{
$gins = \App\Gins::findOrFail($id);
$gins->gin = request('gin');
$gins->size = request('size');
$gins->price = request('price');
$gins->save();
// change this to a named route as well
return redirect('gins');
// or if you just want to return back to the previous page, you can do
// return back();
}

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');
}

Resources