How to get session mobile in laravel - laravel-5.8

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

Related

How can I solve 404 errors in Laravel 7?

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

Laravel routes, form won't submit data into the database

beginner trying to submit a basic form into the database using laravel. I know I should be using 'get' to display the registerform but when I do that I get the 'methodnotallowedhttpexception error.
Currently when I enter data into the form and press submit it just refreshes the page. Any help would greatly be appreciated, thanks
web.php
Route::post('registerForm', 'AuthController#viewregisterForm');
Route::post('registerUser', 'AuthController#registerUser');
AuthController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
//use Illuminate\Support\Facades\Auth;
//use App\Http\Requests;
use App\User;
class AuthController extends Controller
{
function viewregisterForm()
{
return view('register/registerForm');
}
function registerUser(Request $request)
{
$this->validate($request, [
'name' => 'required',
'email' => 'required',
'password' => 'required',
'dateofbirth' =>'required',
]);
//create a Film object
$user = new $User();
$user->name = $request->name;
$user->email = $request->email;
$user->password = $request->password;
$user->dateofbirth = $request->dateofbirth;
$user->role = 1;
$user->save();
return redirect('all');
}
}
?>
registerForm.blade
#extends('layouts.master')
#section('title', 'Register user')
#section('content')
<form action="{{url('registerForm')}}" method="POST">
{{ csrf_field() }}
<h1>Register user</h1>
<div>
<label for="title">Enter name</label>
<input type="text" name="name" id="name">
</div>
<div>
`enter code here`<label for="title">Enter email</label>
<input type="text" name="email" id="email">
</div>
<div>
<label for="title">Enter password</label>
<input type="text" name="password" id="password">
<label for="title">Enter date of birth</label>
<input type="text" name="dateofbirth" id="dateofbirth">
</div>
<input type="submit" name="submitBtn" value="Add User">
</form>
#endsection
You need to change the action of your form submit
from
<form action="{{ url('registerForm') }}" method="POST">
to
<form action="{{ route('register-user') }}" method="POST">
and change the route from
Route::post('registerUser', 'AuthController#registerUser');
to
Route::post('registerUser', ['as' => 'register-user', 'uses' => 'AuthController#registerUser']);
Try to change your routes to this:
Route::get('registerForm', 'AuthController#viewregisterForm');
Route::post('registerForm', 'AuthController#registerUser');

Submitting form input after logging in with laravel

I have the view:
<form class="text-center" action="{{route('PostComment')}}" method="POST">
<div class="form-group">
<textarea class="form-control" name="Comment" id="exampleTextarea" placeholder="Write down your thought here..." rows="4"></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<input type="hidden" value="{{ Session::token() }}" name="_token">
</form>
And also the route:
Route::post('/Comment', [
'uses' => 'CommentController#Comment',
'as' => 'PostComment',
'middleware' => 'auth'
]);
And the controller(not so important):
public function Comment(Request $request)
{
$this->validate($request, [
'Comment' => 'required|min:10|max:100',
]); // validation of comment
$NewComment = new Comment();
$NewComment->user_id = Auth::user()->id;
$NewComment->text = $request['Comment'];
$NewComment->save();
return redirect()->route('Debate');
}
My question is that when you submit the form data and you are not logged in, you have to log in but the form isn't submitted?(edited:using laravel auth)
How to make this thing work, you fill the textarea, you forgot to log in, you log in and the form is submited?

redirect to section of one-page site in laravel not working

I am working on a site in laravel. The email sending form is at the footer of the website. When I send an image, for now it just saves the email in the database. After saving the email, I want to redirect to the footer section of the one-page website but it doesn't. Funny thing is, it works right when validation is removed.
here is my form:
<form class="form" method="POST" action="{{ action('IndexController#email') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="text" name="name" class="form-control">
<input type="email" name="email" class="form-control">
<input type="text" name="subject" class="form-control">
<textarea class="form-control" name="message" style="height: 100px;"></textarea>
<button type="submit" class="btn btn-primary btn-block">Send</button>
</form>
Here is the post route:
Route::post('new/sendemail', ['as' => 'sendemail', 'uses' => 'IndexController#email']);
and here is the controller method:
public function email(Request $request){
$this->validate($request, [
'name' => 'required',
'email' => 'required',
'subject'=>'required',
'message' => 'required'
]);
$input = $request->all();
Email::create($input);
return redirect('new/#contact'); //this is where i want to redirect to that section but it doesn't work
}

Laravel 5 how to use get parameter from url with controller's sign in method

I'm trying to implement a login system where the user will be redirect back only if there is a get parameter in the url, else it will be redirect to the profile page.
So, if the uri is something like this (no get parameter)
/login
if success, the user will be redirect to the profile page.
But if the uri has the get parameter like for example
/login?r=articles
the user will be redirected to the articles page instead of the default route to the profile page.
Question is, in the controller, how can do this, if possible, or how can I check for the get parameter?
routes.php
// Signin
Route::post('/account/signin', [
'uses' => 'UserController#postSignIn',
'as' => 'user.signin',
]);
UserController.php
// Signin
public function postSignIn(Request $request)
{
$this->validate($request, [
'login-email' => 'required|email',
'login-password' => 'required'
]);
if ( Auth::attempt(['email' => $request['login-email'], 'password' => $request['login-password']]) )
{
// Tried this, isn't working... (maybe something's missing ??)
$redirect = $request->input('r');
if ($redirect) {
return redirect()->route($redirect);
}
// -->
return redirect()->route('user.account');
}
return redirect()->back();
}
signin.blade.php
<form role="form" action="{{ route('user.signin') }}" method="post" class="login-form" name="login">
<div class="form-group {{ $errors->has('login-email') ? 'has-error' : '' }}">
<label class="sr-only" for="email">Email</label>
<input type="text" name="login-email" value="{{ Request::old('login-email') }}" placeholder="Email..." class="form-username form-control" id="form-username">
</div>
<div class="form-group {{ $errors->has('login-password') ? 'has-error' : '' }}">
<label class="sr-only" for="password">Password</label>
<input type="password" name="login-password" value="{{ Request::old('login-password') }}" placeholder="Password..." class="form-password form-control" id="form-password">
</div>
<div class="form-group">
<input type="checkbox" name="remember" value="{{ Request::old('remember') }}" id="remember">
Remember
</div>
<button type="submit" class="btn">Sign in!</button>
<input type="hidden" name="_token" value="{{ Session::token() }}">
</form>
Thanks.
Updated
Thank you all for your replies, the fact is that I'm still getting to know Laravel and that's probably why I can't implement it right the solutions that you guys shared.
So this said, I got it working by creating a conditional hidden field that holds the query value and this way once the user submits the form, it will be passed with the rest of the $response arguments.
signin.blade.php
<form role="form" action="{{ route('user.signin') }}" method="post" class="login-form" name="login">
<div class="form-group {{ $errors->has('login-email') ? 'has-error' : '' }}">
<label class="sr-only" for="email">Email</label>
<input type="text" name="login-email" value="{{ Request::old('login-email') }}" placeholder="Email..." class="form-username form-control" id="form-username">
</div>
<div class="form-group {{ $errors->has('login-password') ? 'has-error' : '' }}">
<label class="sr-only" for="password">Password</label>
<input type="password" name="login-password" value="{{ Request::old('login-password') }}" placeholder="Password..." class="form-password form-control" id="form-password">
</div>
<div class="form-group">
<input type="checkbox" name="remember" value="{{ Request::old('remember') }}" id="remember">
Remember
</div>
<button type="submit" class="btn">Sign in!</button>
<input type="hidden" name="_token" value="{{ Session::token() }}">
<!-- Verify condition -->
#if(isset($_GET['referer']))
<input type="hidden" name="referer" value="{{ $_GET['referer'] }}">
#endif
</form>
UserController.php
// Signin
public function postSignIn(Request $request)
{
$this->validate($request, [
'login-email' => 'required|email',
'login-password' => 'required'
]);
if ( Auth::attempt(['email' => $request['login-email'], 'password' => $request['login-password']]) )
{
// Check for the new argument 'referer'
if (isset($request->referer)) {
return redirect()->route($request->referer);
}
// -->
return redirect()->route('user.account');
}
return redirect()->back();
}
Like so, it works.
Don't know if it's a viable and secure way to do it in Laravel 5, but it is working.
When you have an URI such as login?r=articles, you can retrieve articles like this:
request()->r
You can also use request()->has('r') to determine if it's present in the URI.
And request()->filled('r') to find out if it's present in the URI and its value is not empty.
// only query
$query_array = $request->query();
or
$query = $request->query('r');
// Without Query String...
$url = $request->url();
// With Query String...
$url = $request->fullUrl();
If you are using Laravel then use their helper which works just out of the box, i.e. if your route or url has a auth middlewere and user is not logged in then it goes to login and in your postSign or inside attempt just
return redirect()->intended('home'); //home is the fallback if no intended url is provide
UserController
public function postSignIn(Request $request){
$this->validate($request, [
'login-email' => 'required|email',
'login-password' => 'required'
]);
if ( Auth::attempt(['email' => $request['login-email'], 'password' => $request['login-password']]) )
{
return redirect()->intended('user.account);
}
return redirect()->back();
}
first use this
use Illuminate\Support\Facades\Input;
then you can get any data from your url
$name=Input::get('term', false); //term is the parameter name we get from URL
If you are still in this quest you can use
Request
like
$request->r

Resources