For some reason, my form isn't submitting any data to the database, but it looks fine from where I'm standing and the database can call the information to the form fine.
Since there's a lot of information for people to submit, I'm making the profile details not part of the login process. Still unfamiliar with how Laravel does these but I roughly get the process now that I've been fiddling.
One thing I'm wondering, is there a specific syntax for forms to write to the database, should I be naming the database's respective table names in the form? Or is that part of the Controller?
Should I be using Form Model Binding? It's a little hard to find information on that that's for the current version of Laravel though.
What am I missing?
//Route::get('NewUser', 'UserEntryController#create');
Route::post('NewUser', 'UserEntryController#UserForm');
//Route::get('NewUser', 'UserEntryController#create')->name('NewUser');
Route::post('NewUser', 'UserEntryController#UserForm')->name('submit');
Controller:
<?php
namespace App\Http\Controllers;
use App\UserEdit;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
class UserEntryController extends Controller
{
protected function create()
{
$id = UserEdit::find(715)->toArray();
return view('NewUser', compact('id'));
//return $array;
}
public function UserForm(Request $request) {
$email = $request['email'];
$first_name = $request['first_name'];
$password_hint = $request['password_hint'];
$last_name = $request['last_name'];
$user = UserEdit::find(715)->first();
$user->email = $email;
$user->First_Name = $first_name;
$user->Last_Name = $last_name;
$user->Password_Hint = $password_hint;
$user->save();
$id = UserEdit::find(715)->toArray();
return view('NewUser', compact('id'));
}
}
Blade:
#extends('layout')
#section('content')
<h1> Add Your Information {{ $id['name'] }}</h1>
<div class="row">
<div class="col-md-6">
<h3>Edit</h3>
<form action="{{ route('submit') }}" method="post">
<div class="form-group">
{{ csrf_field() }}
<label for="email">Your E-Mail</label>
<input class="form-control" type="text" name="email" id="email">
</div>
<div class="form-group">
{{ csrf_field() }}
<label for="first_name">Your First Name</label>
<input class="form-control" type="text" name="first_name" id="first_name">
</div>
<div class="form-group">
{{ csrf_field() }}
<label for="last_name">Your Last Name</label>
<input class="form-control" type="text" name="last_name" id="last_name">
</div>
<div class="form-group">
{{ csrf_field() }}
<label for="password_hint">Your Password Hint</label>
<input class="form-control" type="text" name="password_hint" id="password_hint">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<input type="hidden" name="_token" value="{{ Session::token() }}">
</form>
</div>
</div>
#foreach ($id as $key=>$value)
{{ $value }}<br>
#endforeach
#stop
You should use $request->get('email') instead of $request['email'] and the same for everything else you want from the request, and I don't think you have to use ->first() when using ->find
There could be something wrong with the _token field, since there are five csrf fields in the form.
Try to remove this line
<input type="hidden" name="_token" value="{{ Session::token() }}">
and just leave one {{ csrf_field() }} in the form.
Related
I want some help with my code.
I try to update user data but it's not update anything.
User Name, Email, Posisson, Image.
Any help please.
My Route :
I used URL because route didn't work.
Route::get('editusers/{id}','UsersController#update');
My Controller:
public function edit($id)
{
$editusers=User::findOrFail($id);
return view('admin.users.EditUser', compact('editusers'));
}
public function update(Request $request, $id)
{
$this->validate($request, [
'name' => 'required',
'email' => 'required',
'posission' => 'required',
]);
$useredit = User::find($id);
$useredit->name = $request->input('name');
$useredit->email = $request->input('email');
$useredit->posission = $request->input('posission');
if($request->hasFile('file'))
{
$file = $request->file('file');
$filename = time().'.'.$file->getClientOriginalExtension();
Image::make($file)->resize(150, 150)->save(public_path('/admin/images/'.$filename));
$useredit->UserImg = $filename;
}
$useredit->save();
return redirect()->back();
}
HTML :
<form class="" action="{{url('editusers',Auth::user()->id)}}" role="form" enctype="multipart/form-data">
{!! csrf_field() !!}
<input type="hidden" name="_method" value="PUT">
<label>Edit Your Profile :</label>
<div class="form-group">
<label>Name :</label>
<input class="form-control" value="{{$editusers->name}}" name="Name">
</div>
<div class="form-group">
<label>Email :</label>
<input class="form-control" value="{{$editusers->email}}" name="email">
</div>
<div class="form-group">
<label>Posisson :</label>
<input class="form-control" value="{{$editusers->posission}}" name="posission">
</div>
<div class="form-group">
<label>Image :</label>
<img src="{{ asset('admin') }}/images/{{$editusers->UserImg}}" alt="avatar" class="img-circle" style="max-height: 100px;">
<input type="file" id="file" name="file"/>
</div>
<input class="btn btn-success btn-mini deleteRecord" type="submit" name="submit" value="Update">
What I expect is that it updates my database.
As your form has
<form class="" action="{{url('editusers',Auth::user()->id)}}" role="form" enctype="multipart/form-data" method="POST">
{!! csrf_field() !!}
<input type="hidden" name="_method" value="PUT">
so your route must have put(),
so it should be Route::put('editusers/{id}','UsersController#update');
also you can use #method('PUT') instead of <input type="hidden" name="_method" value="PUT"> and #csrf instead of {!! csrf_field() !!}
either change $useredit->name = $request->input('name'); to $useredit->name = $request->input('Name'); or in form
<input class="form-control" value="{{$editusers->name}}" name="Name"> to
<input class="form-control" value="{{$editusers->name}}" name="name">
Your route is wrong
Route::get('editusers/{id}','UsersController#update');
it supposed to be PUT
Route::put('editusers/{id}','UsersController#update');
The problem because you put wrong method at your route. Change it
// From
Route::get('editusers/{id}', 'UsersController#update')
// To
Route::put('editusers/{id}', 'UsersController#update')
Anyways, you should change your route to be standard. It should be:
//To show data you should use:
Route::get('users/edit/{id}', 'UsersController#show');
//To update user data.
Route::put('users/edit', 'UsersController#update');
i think you should use resources route to solve this:
Route::resource('editusers','UserController');
but first you need to run this command
php artisan make:controller UserController --resource
I have a form with two input fields, that should route to search.servicecity with the two input fields as parameter (service and city). How can I achieve to say, that by clicking the submit button, the input fields are used as the parameters?
{!! Form::open(['route' => ['search.servicecity',service,city]]) !!}
<div class="row">
<div class='col-md-5'>
<input type="text" class='form-control form-control-lg' name="service" id="service" placeholder="activity" data-action="{{ route('search.autocompleteservice') }}"/>
<div id='searchresultservice' style='text-align:left'></div>
</div>
<div class='col-md-5'>
<input type="text" class='form-control form-control-lg' name="city" id="city" placeholder="city or zip" data-action="{{ route('search.autocompletecity') }}"/>
<div id='searchresultcity' style='text-align:left'></div>
</div>
{{ Form::submit('Suchen', array('class'=>'btn btn-success btn-lg btn-block col-md-2'))}}
</div>
{!! Form::close() !!}
You should have code similar to below. You would obviously need to validate your inputs before trusting anything from the client. You could easily add validation in the view also. The routes file could be routes.php depending on your version of laravel.
File: routes\web.php
Route::get('search/servicecity', 'SearchController#index')->name('searchServiceCityForm');
Route::post('search/servicecity', 'SearchController#process')->name('processServiceCity');
File: app\Http\Controller\SearchController.php
class SearchController extends Controller
{
public function index()
{
return view('search.ServiceCity');
}
public function process(Request $request)
{
$service = $request->input('service');
$city = $request->input('city');
/* Do something with data */
return view(search.result, compact('service','city'));
}
File: resources\views\search\ServiceCity.blade.php
<html><head><title>Search for City and Service</title></head><body>
<form method="post" action="{{url('search/servicecity')}}">
{{csrf_field()}}
<div>
<label for="Service">Service:</label>
<input type="text" name="service">
</div>
<div>
<label for="city">City:</label>
<input type="text" name="city">
</div>
</body></html>
File: resources\views\search\Result.blade.php
<html><head><title>Result of Service City Search</title></head><body>
<div><span>Searched for service: {{ $service }}</span></div>
<div><span>Searched for city: {{ $city }}</span></div>
</body></html>
</html>
I have a controller TourCategoryController.php and has edit method:
public function edit(TCategory $tCategory)
{
return view('admin.manage.tour.category.edit')->withCategory($tCategory);
}
And below is the code from my view edit:
<div class="col-sm-4">
{{Form::model($category,['route' => ['tour-category.update', $category->id ], 'method' => "PUT"]) }}
<input type="text" class="form-control" id="name" name="name">
<label for="name">Name</label>
{{ Form::close() }}
</div>
The trouble I'm having is, the input field is not being filled with form modal binding.
While inspecting the edit form action attribute shows action="http://localhost:8000/manage/tour-category" while it should be like action="http://localhost:8000/manage/tour-category/{id}"
Route for the controller:
Route::prefix('manage')
->middleware('role:superadministrator|administrator|user')
->group(function () {
Route::resource('tour-category','TourCategoryController');
});
Use laravel text field instead of plain form text field.
{{ Form::text('name',null,['class'=>'form-control','id'=>'name']) }}
If you are not using Form Facades
<div class="col-sm-4">
<form method="POST" action="{{ route('tour-category.update', $category->id) }}">
{{ method_field('PUT') }}
{{ csrf_field() }}
<label for="name">Name</label>
<input type="text" id="name" name="name" class="form-control">
</form>
</div>
use
{{ Form::text('name',null,['class'=>'form-control','id'=>'name']) }}
instead of
<input type="text" class="form-control" id="name" name="name">
I am trying to get the old input after cilcking submit button using get method in laravel filter?
in my view blade
{!! Form::open(['url'=>'/jobseekers','method'=>'GET', 'class'=>'form', 'id'=>'search_data']) !!} <div class="form-group col-md-4">
<input type="text" name="fullname" placeholder="Name" value="{{ Session::get('fullname') }}" class="form-control"/>
</div>
<div class="form-group col-md-4"><button class="btn btn-flat btn-primary">Search</button>
</div>{!! Form::close() !!}
and in my controller
$result = null;
if(count($request->all())!=0){
if ($request->has('sub_search')) {
$jobseekers = Jobseeker::Subsearch($request)->paginate(10);
dd($applicant_information);
}else{
$result=Jobseeker::Search($request)->paginate(10);
// dd($orders);
}
}
else{
$jobseekers = Jobseeker::with('calllogs')->orderBy('jobseekers.created_at','DESC')->paginate(16);
}
return view('backend.jobseekers.index',compact('jobseekers','result'));
I cant get the old input value in my view file after clicking submit button.Please guide me, how to do it?
<input type="text" name="fullname" placeholder="Name" value="{{ request()->input('fullname') }}" class="form-control"/>
You could use the very simple to use Laravel helper function which is old("fullname")
I will process the data from the form
then I click the add button and get an error
Whoops, looks like something went wrong.
TokenMismatchException in VerifyCsrfToken.php line 67:
i have view
<form action="{{ url('siswa') }}" method="post">
<div class="form-group">
<label for="nisn" class="control-label">NISN</label>
<input name="nisn" id="nisn" type="text" class="form-control">
</div>
<div class="form-group">
<label for="nama_siswa" class="control-label">Nama Siswa</label>
<input name="nama_siswa" id="nama_siswa" type="text" class="form-control">
</div>
<div class="form-group">
<label for="tanggal_lahir" class="control-label">Tanggal Lahir</label>
<input name="tanggal_lahir" id="tanggal_lahir" type="date" class="form-control">
</div>
<div class="form-group">
<label for="jenis_kelamin" class="control-label">Jenis Kelamin</label>
<div class="radio">
<label><input name="jenis_kelamin" type="radio" value="L" id="jenis_kelamin"> Laki-laki</label>
</div>
<div class="radio">
<label><input name="jenis_kelamin" type="radio" value="P" id="jenis_kelamin"> Perempuan</label>
</div>
</div>
<div class="form-group">
<input class="btn btn-primary form-control" type="submit" value="Tambah Siswa">
</div>
</form>
and then this is my controller
public function create()
{
return view('siswa.create');
}
public function store(Request $request)
{
$siswa = $request -> all();
return $siswa;
}
you need to add {{csrf_field()}} inside the form. it will create a csrf token, which is needed to submit a form
You need to add this {{ csrf_field() }} between your form tags.Read here for more information https://laravel.com/docs/5.4/csrf
There are many options to solve this problem.
1) You can take hidden input field for token inside your form like:
<input type="hidden" name="_token" value="{{ csrf_token() }}" />
2) Add following code before the closing tag of your form:
{{ Form::token() }}
3) Or use laravel form syntax to avoid token mismatch problem like below.
{{ Form::open(array('url' => 'foo/bar')) }}
//
{{ Form::close() }}
4) Or in the html form structure you can also use csrf field like below.
<form method="POST" action="/profile">
{{ csrf_field() }}
...
</form>
5) Or lastly.
<form method="POST" action="/profile">
{!! csrf_field() !!}
...
</form>
This will definately work for you.
Thanks