Posting to the same form in laravel - php

I'm trying to build a search form in Laravel such that when a user presses the search button, all the contacts matching the search criteria are displayed on the same page.
Below is my form:
{{ Form::open(array('url' => '/directory', 'class' => 'form-horizontal')) }}
<div class="form-group">
<label for="lastname" class="col-sm-2 control-label">Last Name</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="lastname" placeholder="Enter Last Name">
</div>
</div>
<div class="form-group ">
<label for="phone" class="col-sm-2 control-label">Phone</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="phone" placeholder="Enter Phone number">
</div>
</div>
<div class="form-group">
<label for="inputEmail1" class="col-sm-2 control-label">Email</label>
<div class="col-sm-6">
<input type="email" class="form-control" id="inputEmail1" placeholder="Enter Email - leave blank if you are not sure">
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<button type="submit" class="btn-u btn-u-green">Search</button>
</div>
</div>
<div class="well">
#foreach($users as $user)
<li>{{$user->firstname}} {{$user->lastname}}</li>
#endforeach
</div>
{{ Form::close() }}
The screenshot below gives a pictorial view of my form:
My route is defined as follow:
Route::get('/directory', 'UsersController#filter');
Now, whenever I press the search button, I am getting a MethodNotFound exception.
What I really want to do is to show the search results below the search button.
Edit ..
Everything is working fine now except for one thing.
I'm displaying the data in my view (getting the list of all the users on my page which match the search criteria) but the for loop is not getting executed.
<li>Count of users = {{ $users->count() }}</li>
<li>{{$users->first()->lastname}}</li>
#foreach ($users as $user)
<p>This is user {{ $user->id }}</p>
#endforeach
So: while I can see the count and the lastname of the first user in the resultant array of records, the foreach loop is not getting executed.
What am I doing wrong?

Laravel routes are bound to the form METHOD, so you need to create also a POST route:
Route::post('/directory', 'UsersController#filter');
You also need to add names to your form fields:
name="lastname"
for
<input type="text" name="lastname" class="form-control" id="lastname" placeholder="Enter Last Name">
And for all the others.
A controller method to handle that query could look like this:
<?php
class UsersController extends Controller {
public function filter()
{
$users = User::query();
if (Input::has('lastname'))
{
$users->where('lastname', Input::get('lastname'))
}
if (Input::has('phone'))
{
$users->where('phone', Input::get('phone'))
}
return View::make('your.view')
->with('userCount', $users->count());
->with('users', $users->get());
}
}
And in your view you can just:
<li>Count of users = {{$userCount}}</li>
#foreach ($users as $user)
<li>
{{ $user->id }} - {{$user->lastname}}
</li>
#endforeach

You have to tell Laravel that you want to use get method for submitting a form to the server. otherwise, Laravel will use post method by default.
Try the following:
{{ Form::open(array('url' => '/directory', 'method' => 'get', 'class' => 'form-horizontal')) }}
^^^

Related

Unable to store in database while using Laravel (version 7.25)

I am trying to get hold of Laravel framework and got stuck while retrieving the values from the page in a Database.
These are the snippets of my controller, routes, view part which might help in understanding what I am doing
web.php (for routes):
<?php
use Illuminate\Support\Facades\Route;
Route::resource('posts', 'PostController');
Postcontroller.php (just a snippet)
<?php
public function create()
{
//
return view('posts.create');
}
public function store(Request $request)
{
//
$this->validate($request , array(
'title' => 'required',
'body' => 'required'
));
$post = new Post;
$post->title = $request->title;
$post->body = $request->body;
$post->save();
return redirect()->route('posts.show', $post->id);
}
create.blade.php(for view)
<div class="row">
<div class="col-mid-8 col-md-offset-2" >
<h1>Create New Post</h1>
<hr>
<form route='posts.store' >
#csrf
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1">
</div>
<div class="form-group form-check">
<input type="checkbox" class="form-check-input" id="exampleCheck1">
<label class="form-check-label" for="exampleCheck1">Check me out</label>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
The problem with your code is that you don't have name properties on your input, so nothing gets passed to your backend code.
Add name to your inputs like this (they need to be different, else they would get overwritten):
<input type="text" class="form-control" id="your-unique-input-id" name"your_unique_input_name">
Then, when you submit your form, the all the inputs that have name attribute in them will get passed to your backend method. You can then retrieve their value, by using the name you gave them earlier. So in this example:
$request->your_unique_input_name // Which will return anything that user typed in that field

How to fix edit interface getting only first word of a column in table in Laravel 5.7?

My edit interface edit.blade.php only gets the first word of the name from my database, this is what the index.blade.php looks like
and when i click on the edit icon of the 3rd line it leads me to edit.blade.php which gives me this
"Nom d'établissement" textfield only gets the first word from the database
Everything looks fine in the database:
this is my edit.blade.php form:
<form method="post" action="{{ route('etablissements.update', $etablissement->id) }}">
#method('PATCH')
#csrf
<div class="col-5">
<div class="form-group">
<label for="nom">Nom Etablissement :</label>
<input type="text" class="form-control" name="nom" value={{ $etablissement->nom }} />
</div>
<div class="form-group">
<label for="price">E-Mail :</label>
<input type="text" class="form-control" name="email" value={{ $etablissement->email }} />
</div>
<div class="form-group">
<label for="quantity">Telephone:</label>
<input type="text" class="form-control" name="telephone" value={{ $etablissement->telephone }} />
</div>
</div>
<button type="submit" class="btn btn-primary">Confirmer</button>
</form>
this is edit function in the controller:
public function edit($id)
{
$etablissement = Etablissement::find($id);
return view('etablissements.edit', compact('etablissement'));
}
and this is update function in the controller:
public function update(Request $request, $id)
{
$request->validate([
'nom'=>'required',
'email'=> 'required',
'telephone' => 'required|numeric'
]);
$etablissement = Etablissement::find($id);
$etablissement->nom = $request->get('nom');
$etablissement->email = $request->get('email');
$etablissement->telephone = $request->get('telephone');
$etablissement->save();
return redirect('/etablissements')->with('success', 'Utilisateur édité');
}
Quote the value attribute.
<input type="text" class="form-control" name="nom" value="{{ $etablissement->nom }}" />
Without quotes, the second word in$etablissement->nom is interpreted as another attribute rather than part of the value of the value attribute.
The email and telephone values are showing up correctly because there are no spaces, but you should quote those as well just in case.

Laravel create custom method to get form data

How do I make custom method to get form data? I want this method same with Laravel update method with parameters request and id. I try this but get error.
In controller
public function updatePassword(Request $request, int $id) {
dd($request->all());
}
In route
Route::post('staffs/{id}/upassword', 'Admin\StaffController#updatePassword')->name('admin.staffs.upassword');
In blade file
<form method="post" accept-charset="utf-8" action="{{ action('Admin\StaffController#updatePassword', ['id' => $staff_id]) }}">
<div class="row">
<div class="col-md-3">
<div class="form-group">
<label class="control-label" for="password">New Password</label>
<input class="form-control" name="password" type="password">
</div>
</div>
</div>
<div class="row">
<div class="col-md-3">
<div class="form-group">
<label class="control-label" for="password_confirmation">Confirm New Password</label>
<input class="form-control" name="password_confirmation" type="password">
</div>
</div>
</div>
<input class="btn btn-primary" type="submit">
</form>
I am using Laravel 5.4.
here are some stuff to fix:
First in the tag you can set the action to :
action="route('admin.staffs.upassword', $staff_id)" since it's
easier to write and since you already gave the route a name, so why
not using it ;)
Second add {{csrf_field() }} right before your form closing tag
</form>
what error are you getting? the error is probably because you are not using {{csrf_field()}} after the form declaration, it is needed so that laravel can validate the request. if you want to get the data from the form you can use:
$request->get('inputname');

Laravel form not submitting data to database

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.

laravel not loading my form section?

im trying to build a user login and registration form and this is my route :
Route::get('/register', function()
{
return View::make('register');
});
Route::get('/register', function()
{
$user = new User;
$user->username = Input::get('username');
$user->password = Hash::make(Input::get('password'));
$user->save();
$username = Input::get('username');
return View::make('registered')->with('username',$username);
});
and this is my html :
<div class="container">
{{ Form::open(array('url' => 'register', 'class' => 'form-horizontal')) }}
<fieldset>
<!-- Form Name -->
<legend>Form Name</legend>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="username"></label>
<div class="col-md-4">
<input id="username" name="username" type="text" placeholder="" class="form-control input-md" required="">
</div>
</div>
<!-- Password input-->
<div class="form-group">
<label class="col-md-4 control-label" for="password"></label>
<div class="col-md-4">
<input id="password" name="password" type="password" placeholder="" class="form-control input-md" required="">
</div>
</div>
<!-- Appended checkbox -->
<div class="form-group">
<label class="col-md-4 control-label" for="appendedcheckbox"> </label>
<div class="col-md-4">
<div class="input-group">
<input id="appendedcheckbox" name="appendedcheckbox" class="form-control" type="text" placeholder="">
<span class="input-group-addon">
<input type="checkbox">
</span>
</div>
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="submit"> </label>
<div class="col-md-4">
<button id="submit" name="submit" class="btn btn-inverse"> </button>
</div>
</div>
</fieldset>
</div>
few problems :
1.
my form does not loads and i see just
the last button for submitting the form and : ' you have registered in $username ' which i design to loads AFTER user submitted
2.my localhost:8000 loaded laravel first page one time but when i began to work on the project i just receiving blank white page and currently accessing my file like this : http://localhost/vendor/bin/crm/public/register
3.
is hashing in laravel secure enough? or should i do something else ?
4.
my way of doing this is alright or there is a better way for login and reg using laravel ?
You have two routes responding to get requests on /register. Change the second one to Route::post(...) and I would also change both to just register. There isn't a need to prepend a slash onto your routes.
Hashing in Laravel is secure and shouldn't be something you have to worry about.
There really isn't a "right" way of doing things, it really depends on how the rest of your app works, how complicated it is, and how easy it should be to maintain. If it were me though, I would have a LoginController with a method for showing the view and a method for creating the user and have those methods respond to the request rather than putting everything right in your routes.php file.
You are also missing a {{ Form::close() }} at the end of your view as well.

Categories