I'm making a registration form using Laravel 7, my form has a "preview" step where users first check the details and either submit the details as they are or edit. The actual submission of the details happens in the blade file named preview.blade.php and this is where I have placed the form action for posting the data. This part works well, the edit part is the one with the problem as it gives 404 error. Please help.
In my register.blade.php:
<form method="post" action="{{ url('/preview-details') }}" id="regForm">
{{ csrf_field() }}
<div class="form-group">
<label for="region">Your Region</label><span class="text-danger">*</span>
<input type="text" class="form-control" name="region" id="region" placeholder="e.g Westlands" required>
</div>
<label for="start_date">Date</label><span class="required">*</span>
<input type="date" class="form-control" id="start_date" name="start_date"
value="{{ old('start_date') }}" required>
<!--Other fields here-->
</form>
In my preview.blade.php:
<div>
<a href="{{route('edit_preview')}}">
<button type="button" class="btn btn-primary btn-sm"
style="float:right;"><i class="fas fa-edit"> </i>Edit</button>
</a>
</div>
<form method="post" action="{{ url('/client-registration') }}">
{{ csrf_field() }}
<div class="form-group">
<label for="region">Your Region</label><span class="text-danger">*</span>
<input type="text" class="form-control" name="region" id="region" placeholder="e.g Westlands" required>
</div>
<label for="start_date">Date</label><span class="required">*</span>
<input type="date" class="form-control" id="start_date" name="start_date"
value="{{ old('start_date') }}" required>
<!--Other fields here-->
</form>
In my edit_preview.php (this one shows 404 error):
<form method="post" action="{{ url('/client-registration') }}">
{{ csrf_field() }}
<div class="form-group">
<label for="region">Your Region</label><span class="text-danger">*</span>
<input type="text" class="form-control" name="region" id="region" placeholder="e.g Westlands" required>
</div>
<label for="start_date">Date</label><span class="required">*</span>
<input type="date" class="form-control" id="start_date" name="start_date"
value="{{ old('start_date') }}" required>
<!--Other fields here-->
</form>
In my Routes file:
Route::get('/client-registration', 'Auth\Client\RegisterController#create')->name('client-registration');
Route::post('/client-registration', 'Auth\Client\RegisterController#store');
Route::post('/preview-details', 'Auth\Client\PreviewRegisterDetailsController#confirmation')->name('preview-details');
Route::post('/preview-details-existing', 'Auth\Client\PreviewDetailsExistingClientController#confirmation')->name('preview-existing');
Route::post('/edit_preview', 'Auth\Client\EditRegisterDetailsController#editDetails')->name('edit_preview');
In my Preview Controller (It works well):
class PreviewRegisterDetailsController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest');
}
// Validating the User
public function confirmation(Request $request)
{
$categories = Category::all();
$request->validate([
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'telephone_number' => 'required|digits:10',
'email' => 'required|string|email|max:255|unique:users','regex:/^[\w\-\.\+]+\#[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,6}$/',
'password' => 'required|string|min:6|confirmed',
'password_confirmation' => 'required',
'region' => 'required|string',
'description' => 'required|string|max:2500',
'start_date' => 'required|date',
'client_region' => 'string|max:500',
'client_category' => 'integer|max:255',
]);
$data = $request->all();
return view('auth.client.preview', compact('categories', 'data'));
}
}
In my edit details controller:
class EditRegisterDetailsController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest');
}
// Validating the User
public function editDetails(Request $request)
{
$categories = Category::all();
$request->validate([
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'telephone_number' => 'required|digits:10',
'email' => 'required|string|email|max:255|unique:users','regex:/^[\w\-\.\+]+\#[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,6}$/',
'password' => 'required|string|min:6|confirmed',
'password_confirmation' => 'required',
'region' => 'required|string',
'description' => 'required|string|max:2500',
'start_date' => 'required|date',
'client_region' => 'string|max:500',
'client_category' => 'integer|max:255',
]);
$data = $request->all();
return view('auth.client.edit_preview', compact('categories', 'data'));
}
}
Related
i'm learning laravel.
I have to do a simple store function to create a new post. The form appears to work correctly but the data does not reach the DB.
There's my store function:
public function store(Request $request)
{
$request->validate($this->validationRules);
$formData = $request->all();
$puzzle = new Puzzle();
$puzzle->fill($formData);
$puzzle->save();
// $newPuzzle = Puzzle::create($formData);
// return redirect()->route('puzzles.show', $newPuzzle->id);
}
My model:
class Puzzle extends Model
{
public $timestamps = false;
protected $fillable = [
'title',
'pieces',
'image',
'description',
'brand',
'price',
'available',
'quantity',
];
}
My form:
<form method="POST" action="{{route('puzzles.store')}}">
#csrf
<div class="mb-3">
<label for="title" class="form-label">Title</label>
<input type="text" class="form-control" id="title" name="title" value="{{ old('title') }}">
</div>
<div class="mb-3">
<label for="pieces" class="form-label">Pieces</label>
<input type="number" class="form-control" id="pieces" name="pieces" value="{{ old('pieces')}}">
</div>
<div class="mb-3">
<label for="description" class="form-label">Description</label>
<input type="text" class="form-control" id="description" name="description" value="{{ old('description')}}">
</div>
<div class="mb-3 form-check">
<label class="form-check-label" for="available">Available</label>
<input type="checkbox" class="form-check-input" id="available" name="available" checked>
</div>
<div class="col-md-4">
<label for="quantity" class="form-label">Quantity</label>
<input type="number" min="1" step="any" class="form-control" id="quantity" name="quantity">
</div>
<div class="col-md-4">
<label for="price" class="form-label">Price</label>
<input type="number" min="3.0" step="0.1" class="form-control" id="price" name="price">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
what am I doing wrong?
Thank you
Make changes in your store function as per below
public function store(Request $request)
{
$validated = $request->validate([
'title' => 'min:5|max:250',
'pieces' => 'numeric',
'description' => 'min:5|max:500',
'price' => 'numeric',
'available' => 'boolean',
'quantity' => 'numeric',
]);
$puzzle = Puzzle::create($request->only(
'title', 'pieces', 'description', 'available', 'quantity', 'price'
));
return redirect()->route('puzzles.show', $puzzle->id);
}
in .env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_databse_name
DB_USERNAME=your_databse_user_name
DB_PASSWORD=your_databse_password
there is no problem when I do create new data. everything is ok including uploading image. My image inserted to public/image directory.
But when i try editing or updating, i have a problem. my image that should be inserted to public/image not work on update function.
the controller is below
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'sequence' => 'required',
'image' => 'required|image|max:2048',
'link' => 'required',
'status' => 'required',
]);
$image = $request->file('image');
$new_name = $request->name .rand(). '.' . $image->getClientOriginalExtension();
$image->move(public_path('images'), $new_name);
$form_data = array(
'name' => $request->name,
'sequence' => $request->sequence,
'image' => $new_name,
'link' => $request->link,
'status' => $request->status,
);
Banner::create($form_data);
return redirect()->route('banner.index');
}
public function update(Request $request, Banner $banner)
{
$image_name = $request->hidden_image;
$image = $request->file('image');
if($image != ''){
$request->validate([
'name' => 'required',
'sequence' => 'required',
'image' => 'image|max:2048',
'link' => 'required',
'status' => 'required',
]);
$image_name = $request->name .rand(). '.' . $image->getClientOriginalExtension();
$image->move(public_path('images'), $image_name);
}
else{
$request->validate([
'name' => 'required',
'sequence' => 'required',
'link' => 'required',
'status' => 'required',
]);
}
$form_data = array(
'name' => $request->name,
'sequence' => $request->sequence,
'image' => $image_name,
'link' => $request->link,
'status' => $request->status,
);
Banner::whereId($banner)->update($form_data);
return redirect()->route('banner.index');
}
And my view code is below
<form action="{{ route('banner.update',$banner->id) }}" method="POST">
#csrf
#method('PUT')
<div class="row" style="margin-top: 10px">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong>
<input type="text" name="name" value="{{ $banner->name }}" class="form-control" placeholder="Name">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Sequence</strong>
<input type="number" name="sequence" value="{{ $banner->sequence }}" class="form-control" placeholder="Sequence">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Image</strong>
<div class="col-md-8">
<img src="{{ URL::to('/') }}/images/{{ $banner->image }}" class="img-thumbnail" width="200" />
</div>
<div class="col-md-8">
<input type="file" name="image" />
<input type="text" name="hidden_image" value="{{ $banner->image }}" />
</div>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Link</strong>
<input type="text" name="link" value="{{ $banner->link }}" class="form-control" placeholder="Link">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Status</strong>
<input type="text" name="status" value="{{ $banner->status }}" class="form-control" placeholder="Status">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
your form is missing enctype attribute and so no file is passing with the form. add that attribute and everything will work.
<form action="{{ route('banner.update',$banner->id) }}" method="POST" enctype="multipart/form-data">
I want to after registered a user name, age, mobile and ... get the session of mobile because in next page, I want to get mobile in input hidden.
Attention: I did not do session at all.
I think it's like this:
RegisterController.php
public function register(Request $request, User $user)
{
$code = rand(10000,99999);
session->get('mobile')
$user = \App\User::create([
'first_name' => $request->first_name,
'last_name' => $request->last_name,
'gender' => $request->gender,
'mobile' => $request->mobile,
'code' => $code,
.
.
.
return redirect()->route('code')->with('mobile', $request->mobile);
}
It redirect to this page.
Code
code.blade.php
<form action="{{ route('send') }}" method="post">
{{ csrf_field() }}
<input type="hidden" class="form-control" value="{{ session->mobile }}" name="mobile" id="mobile">
<div class="form-group">
<label for="code">کد</label>
<input type="text" class="form-control" name="code" id="code">
</div>
<div class="form-group">
<button type="submit" class="btn btn-danger" id="btn-ok">OK</button>
</div>
</form>
use Illuminate\Support\Facades\Session;
For saving in session use
Session::put('mobile', $request->mobile);
then retrieve it using
Session::get('mobile');
If I change just the user table items it'll update them (if i change everything in the user table rows). However, it'll only update the user_infos table when I edit everything.
ProfilesController.php
public function update(Request $request)
{
$user_id = Auth()->user()->id;
$id = Auth()->user()->id;
$this->validate($request, array(
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'type' => 'required|string',
'description' => 'string',
'projects' => 'string',
'experience' => 'string',
'links' => 'string',
'status' => 'string'
));
if($request->hasFile('avatar')){
$avatar = $request->file('avatar');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
Image::make($avatar)->resize(300, 260)->save( public_path('/images/uploads/avatars/' . $filename ) );
$user = Auth::user();
$user->avatar = $filename;
$user->save();
}
$user = User::find($id);
$user->first_name = $request->input('first_name');
$user->last_name = $request->input('last_name');
$user->email = $request->input('email');
$user->type = $request->input('type');
$user_info = User_Info::find($user_id);
//$user_info = User_Info::where('user_id', $user->id)->first();
$user_info->description = $request->input('description');
$user_info->projects = $request->input('projects');
$user_info->experience = $request->input('experience');
$user_info->links = $request->input('links');
$user_info->status = $request->input('status');
$user->save();
$user_info->save();
return redirect()->route('profile')->withUser($user);
}
settings.blade.php
<div class="twelve wide column">
<div class="ui segment" data-tab="bio">
<form action="{!! action('ProfilesController#update', ['id' => $user->id]) !!}" method="POST" enctype="multipart/form-data" class="ui form">
{{ csrf_field() }}
<h3>Name</h3>
<div class="inline fields">
<div class="eight wide field">
<input type="text" name="first_name" placeholder="First Name" value="{{ $user->first_name }}">
</div><!-- ./Eight Wide Field -->
<div class="eight wide field">
<input type="text" name="last_name" placeholder="Last Name" value="{{ $user->last_name }}">
</div> <!-- ./Eight Wide Field -->
</div><!-- ./Inline Fields -->
<h3 class="header">Profile Image</h3>
<div class="inline fields">
<div class="sixteen wide field">
<input type="file" name="avatar">
</div> <!-- ./Sixteen Wide Field -->
</div> <!-- ./Inline Fields -->
<h3 class="header">Email</h3>
<div class="inline fields">
<div class="sixteen wide field">
<input type="email" name="email" placeholder="Email" value="{{ $user->email }}">
</div> <!-- ./Sixteen Wide Field -->
</div> <!-- ./Inline Fields -->
<h3>Type</h3>
<div class="inline fields">
<div class="sixteen wide field fluid">
<select name="type" class="ui dropdown fluid registerType">
<option value="Developer">Developer</option>
<option value="Designer">Designer</option>
<option value="Fullstack">FullStack</option>
<option value="Client">Client</option>
</select>
</div> <!-- ./Sixteen Wide Field Fluid -->
</div><!-- ./Inline Fields -->
<h3 class="header">Description</h3>
<textarea name="description" id="" cols="30" rows="10" placeholder="Description">{{ isset($user->userInfo->description) ? $user->userInfo->description : "This User Has No Description" }}</textarea>
<h3 class="header">Projects</h3>
<textarea name="projects" id="" cols="30" rows="3" placeholder="Projects">{{ isset($user->userInfo->projects) ? $user->userInfo->projects : "This User Has No Projects Listed" }}</textarea>
<h3 class="header">Experience</h3>
<textarea name="experience" id="" cols="30" rows="3" placeholder="Experience">{{ isset($user->userInfo->experience) ? $user->userInfo->experience : "This User Has No Experience Listed" }}</textarea>
<h3 class="header">Links</h3>
<input type="text" name="links" placeholder="Links" value="{{ isset($user->userInfo->links) ? $user->userInfo->links : "This User Has No Links Listed" }}">
<h3 class="header">Status</h3>
<input type="text" name="status" placeholder="Status" value="{{ isset($user->userInfo->status) ? $user->userInfo->status : "Available" }}">
<br><br>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" class="ui medium fluid blue button" value="Update">
{!! Form::close() !!}
</div><!-- ./Ui Segment -->
</div> <!-- ./Twelve Wide Column -->
Thanks in advance!
Using: Laravel 5.4
In here:
$user->update(); // <--- try this
$user->save();
$user_info->save();
In my ProfilesController.php I had to change the validation for "email" inside public function update(Request $request) a little bit.
From:
$this->validate($request, array(
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'type' => 'required|string',
'description' => 'string',
'projects' => 'string',
'experience' => 'string',
'links' => 'string',
'status' => 'string',
));
To:
$this->validate($request, array(
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users,email,'.$id,
'type' => 'required|string',
'description' => 'string',
'projects' => 'string',
'experience' => 'string',
'links' => 'string',
'status' => 'string',
));
I am adding few new fields to my registration form. All the default field are stored in database except custom field.
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
'company_name' => 'required',
]);
}
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'company_name' => $data['company_name'],
'role' => 'client',
]);
return $user;
}
Here is my form
<form action="/register" method = "post" id = "reg_form">
{{csrf_field()}}
<div class="form-group">
<input type="text" name="name" id="name" placeholder="Name"/>
</div>
<div class="form-group">
<input type="email" name="email" id="email" placeholder="Email"/>
</div>
<div class="form-group">
<input type="text" name="company_name" id="email" placeholder="Name of your Company"/>
</div>
<div class="form-group">
<input type="password" name="password" id="pass" placeholder="Password"/>
</div>
<div class="form-group">
<input type="password" name="password_confirmation" id="re_pass" placeholder="Re enter password"/>
</div>
<div class="form-group">
<input type="submit" class="btn" id="register_sbt_btn" value="Register"/>
</div>
</div>
</form>
The company_name and role doesn't get stored. I am giving a default value to the role => client whenever a new user registers.
Remember to add the fields on $fillable attribute of User model
protected $fillable = [
'name',
'email',
'password',
'company_name', //<--- HERE
'role', //<--- HERE
];
Also make sure that you have those fields on Users table