Submitting form input after logging in with laravel - 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?

Related

How to develop custom login by auth middleware using laravel

I want to make a custom login system by auth middleware.
I don't have an idea how to do that.
controller
public function dologin(Request $request)
{
$request->validate([
'username' => 'required',
'password' => 'required'
]);
$username = $request->username;
$password = $request->password;
if (Auth::attempt(['username' => $username, 'password' => $password])) {
}
}
Blade view
<form action="{{route('login.action')}}">
<br>
<div class="form-row">
<div class="col">
<label>USERNAME</label>
<input type="text" class="form-control" placeholder="Enter Username" name="username">
</div>
</div>
<!--form-row-->
<br>
<div class="form-row">
<div class="col">
<label>PASSWORD</label>
<input type="password" class="form-control" placeholder="Enter password" name="password">
</div>
</div>
<!--form-row-->
<br>
<div class="form-group">
<input type="submit" name="btnsubmit" class="btn btn-success col-md-3" id="signup-btn" value="Login">
</div>
</form>
Route
// Login
Route::get('/login', 'LoginController#create')->name('login');
Route::get('/login/action', 'LoginController#dologin')->name('login.action');
Create your LoginController by runing this command in terminal:
php artisan make:controller LoginController
Then find your newly created controller under App\Http\Controllers\ and add a the use of Auth at the top of your controller
use Illuminate\Support\Facades\Auth;
Then add your dologin function to the controller and you're ready to go.

POST method is not supported for this route

First, I've checked other question topic, but couldn't find the solution.
when I try to post my form. I am getting this error.
The POST method is not supported for this route. Supported methods:
GET, HEAD.
Form:
<div class="card-body">
<form action="{{route('profile.update', ['id' => $id])}}" method="post">
#csrf
#put
<div class="form-group">
<label for="location">Location</label>
<input class="form-control" type="text" name="location" value="{{$info->location}}">
</div>
<div class="form-group">
<label for="about">About</label>
<textarea name="about" id="about" rows="10" cols="50" class="form-control">{{$info->about}}</textarea>
</div>
<div class="form-control">
<p class="text-center">
<button class="btn btn-primary btn-md" type="submit">Update Your Info</button>
</p>
</div>
</form>
</div>
Routes:
Route::group(["middleware" => "auth"], function(){
route::get("/profile/edit", [
"uses" => "ProfilesController#edit",
"as" => "profile.edit"
]);
route::get("/profile/{slug}", [
"uses" => "ProfilesController#index",
"as" => "profile"
]);
route::put("/profile/update/{id}", [
"uses" => "ProfilesController#update",
"as" => "profile.update"
]);
});
in controller:
public function update(Request $request, $id)
{
dd($request->all());
}
From your question, i can understand that you're trying to update a profile using POST method or may be PUT method earlier. Since, the resource you are editing is unique, you're not passing any parameters for the controller to find that single resource so as to update it.
therefore modify your your route like
route::put("/profile/update/{id}", [
"uses" => "ProfilesController#update",
"as" => "profile.update"
]);
And your form like
<form action="{{route('profile.update', ['id' => $id])}}" method="post">
#csrf
#method('put')
You'll need to pass the ID of the profile you want to update as parameter
then at the controller
public function update(Request $request, $id){
//edit the profile with id = $id
}
You have an error in your form definition
<form class="{{route('profile.update', ['id' => $id])}}" method="post">
should be
<form action="{{route('profile.update', ['id' => $id])}}" method="post">
Since you made a form for PUT request, you have to change
route::post("/profile/update/profile", [
"uses" => "ProfilesController#update",
"as" => "profile.update"
]);
to this
route::put("/profile/update/profile", [
"uses" => "ProfilesController#update",
"as" => "profile.update"
]);
Here is the correction in your provided example.
In form route('profile.update', ['id' => {here you have to place id of record which you want to update}]).
View File
$info->id])}}" method="post">
<div class="form-group">
<label for="location">Location</label>
<input class="form-control" type="text" name="location" value="{{$info->location}}">
</div>
<div class="form-group">
<label for="about">About</label>
<textarea name="about" id="about" rows="10" cols="50" class="form-control">{{$info->about}}</textarea>
</div>
<div class="form-control">
<p class="text-center">
<button class="btn btn-primary btn-md" type="submit">Update Your Info</button>
</p>
</div>
</form>
</div>
In Route
Route::group(["middleware" => "auth"], function(){
route::get("/profile/{slug}", [
"uses" => "ProfilesController#index",
"as" => "profile"
]);
route::get("/profile/edit/profile", [
"uses" => "ProfilesController#edit",
"as" => "profile.edit"
]);
route::post("/profile/update/profile/{id}", [
"uses" => "ProfilesController#update",
"as" => "profile.update"
]);
});
In Controller
public function update(Request $request, $id)
{
dd($id, $request->all());
}

How to get session mobile in laravel

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

Laravel post request not works

I have Form in Laravel and when i submit the form to redirect another page("action = panel") with inputs's value. but problem is that when i enter in another's link it displays error. what is wrong?
This is form
this is another page when submit form
this is error when i enter in link again
this is form code:
<form action="{{route('adminPanel')}}" class="form" method="POST">
<p>Name:</p>
<input type="text" name="name"><br>
<p>Password:</p>
<input type="password" name="password"><br>
<input type="submit" name="submit" value="Enter As Admin" class="submit">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
this is routes:
Route::get('/', [
'uses' => 'AdminController#getAdminIndex',
'as' => 'index.admin'
]);
Route::post('/panel', [
'uses' => 'AdminController#getAdminPanel',
'as' => 'adminPanel'
]);
this is controller:
class AdminController extends Controller
{
public function getAdminIndex(){
return view('admin/index');
}
public function getAdminPanel(Request $request){
return view('admin/admin', ['name' => $request->name]);
}
}
this is because when you enter an address in address bar, your are actually sending a get request. but you've defined your route with post method!
to fix this you can use any:
Route::any('/panel', [
'uses' => 'AdminController#getAdminPanel',
'as' => 'adminPanel'
]);
and in controller:
use Illuminate\Support\Facades\Auth;
class AdminController extends Controller
{
public function getAdminIndex(){
return view('admin/index');
}
public function getAdminPanel(Request $request){
$name = $request->name ?: Auth::user()->name;
return view('admin/admin', ['name' => $name]);
}
}
Try to use the following statement in form as {{ csrf_field() }}
Sometimes routes may create you an issue. Try below snippet.
<form action="{{route('adminPanel')}}" class="form" method="POST">
<p>Name:</p>
<input type="text" name="name"><br>
<p>Password:</p>
<input type="password" name="password"><br>
<input type="submit" name="submit" value="Enter As Admin" class="submit">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
Correct your Routes as below and try.
Route::post('/adminPanel', ['uses' => 'AdminController#getAdminPanel', 'as' => 'adminPanel' ]);

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
}

Resources