I want to show registered user data after his/her id# is entered in the input field and on hit enter or when search button is pressed bring his/her data in the fields. I just want to show user data when the search button is pressed. I think the problem is in my controller. This is my controller:
use Illuminate\Http\Request;
use App\Helpers\Helper;
use Illuminate\Support\Facades\DB;
use App\User;
use App\Models\patient;
class Helpercontroller extends Controller
{
function search(Request $request){
$patientid = $request->input('patientid');
$data = DB::table('patients')->where(['patientid'=>$patientid])->first();
return $data;
}
}
}
this is my route file:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\login;
use App\Http\Controllers\MainController;
use App\Http\Controllers\add1;
use Illuminate\Http\Request;
use App\Http\Controllers\Helpercontroller;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/registration', function () {
return view('registration');
});
Route::post('/registration/search',[Helpercontroller::class,'search'])->name("search-registration");
this is my blade file:
#extends('layouts.theme')
#section('content')
<div class="container">
<div class="row">
<div class="col-lg-12">
<div class="col-md-3 col-md-offset-3 d4 " style="margin-top: 50px">
<h4>Patient Registration</h4>
</div><br>
#if(Session::get('success'))
<div class="alert alert-success">
{{Session::get('success')}}
</div>
#endif
#if(Session::get('fail'))
<div class="alert alert-danger">
{{Session::get('fail')}}
</div>
#endif
<form action="{{ route("search-registration") }}" method="POST" autocomplete="off">
#csrf
<label>Search Patient :</label>
<input placeholder="Enter Patient ID" type="number" name="patientid" class="form-control form-control-sm d2" >
</div>
</div>
<div class="col-lg-2">
<button type="submit" class="btn-block col-lg-12 ">Search</button>
</div>
</form>
[![THIS IS MY BLADE FILE OUTPUT][1]][1]
change your route for search, a good practice is name the routes for better organization:
Route::post('/registration/search',[Helpercontroller::class,'search'])->name("search-registration");
In the form:
<form action="{{ route("search-registration") }}" method="POST" autocomplete="off">
Problem is in your route file.
Route::get('/registration', function () {
return view('registration');
});
Route::get('registration',[Helpercontroller::class,'search']);
Both the route are same. So Laravel will return the first matching route. You need to change the route or at least change the route method.
see below for more details.
Router-Methods
Related
This question already has answers here:
The POST method is not supported for this route. Supported methods: GET, HEAD. Laravel
(19 answers)
Closed 8 months ago.
First off, I'm a beginner and I know this question has been asked a few times, and answered. I've attempted the solutions, or I've had the suggestion implemented, so I've come here for help from the experts!
What I've tried:
-Adding #csrf to my blade file inside my form element.
-Checking that my post and get's in my routes are lowercase.
-Changing the gets to post and so fourth.
The above has not helped in solving the problem, and alas I'm still scratching my head.
Effectively, I'm trying to upload an image, give the image a caption and submit. It should go to another page displaying the data in a drop down in the top left hand corner, but I'm greeted with the image in the title.
EDIT: Solution: I ran the following and it removed the error:
php artisan route:clear
I've included images below of the error and my folder structure:
Image of error appearing.
Project directory structure
Here's the code:
Web.php
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/p/create', 'App\Http\Controllers\PostsController#create');
Route::post('/p/', 'App\Http\Controllers\PostsController#store');
Route::get('profile/{user}', 'App\Http\Controllers\ProfilesController#index')-
>name('profile.show');
PostsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class PostsController extends Controller
{
public function constructor()
{
$this->middleware['auth'];
}
public function create()
{
return view('posts.create');
}
public function store() {
$data = request()->validate([
'caption' => 'required',
'image' => 'required|image',
]);
dd(request('image')->store('uploads','public'));
auth()->user()->posts()->create($data);
dd(request()->all());
}
}
Model/Posts.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
}
views/posts/create.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<form action="/p" enctype="multipart/form-data" method="post">
#csrf
<div class="row">
<div class="col-8 offset-2">
<div class="row mb-3">
<div class="row pt-5">
<h1>Add New Post</h1>
</div>
<label for="caption" class="col-md-4 col-form-label">Post Caption</label>
<input id="caption"
type="text"
class="form-control #error('caption') is-invalid #enderror"
name="caption"
value="{{ old('caption') }}"
autocomplete="caption" autofocus>
#error('caption')
<strong>{{ $message }}</strong>
#enderror
</div>
<div class="row">
<label for="image" class="col-md-4 col-form-label">Post Image</label>
<input type="file" class="form-control-file" id="image" name="image">
#error('image')
<strong>{{ $message }}</strong>
#enderror
<div class="pt-3">
<button class="btn btn-primary btn-sm">Add New Post</button>
</div>
</div>
</div>
</div>
</form>
#endsection
Thank you in advance for your help!
Could this be caused by your route being defined as "/p/", while your form action is just "/p" (without the trailing slash)? I'd try ensuring those are an exact match first. Change your route to say:
Route::post('/p', 'App\Http\Controllers\PostsController#store');
You could also try broadening that route definition.
Route::post('/p/', 'App\Http\Controllers\PostsController#store');
would become
Route::any('/p/', 'App\Http\Controllers\PostsController#store');
EDIT: Solution: I ran the following and it removed the error:
php artisan route:clear
you need to use and name for your router, and now route will look like
Route::post('p',[PostsController::class, 'store'])->name('p.name');
And action form:
<form action="{{route('p.name')}}" enctype="multipart/form-data" method="post">
And clear cache route then change:
php artisan route:cache
I'm coding a quiz app and I'm trying to add an edit function to it. With a click on the 'Submit' Button I get the error Missing required parameters for [Route: quiz/show] [URI: quiz/{quiz}].
How can I fix that? I'm a beginner in Laravel so it would be nice if you could help me.
My web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProductController;
use App\Http\Controllers\MainController;
use App\Http\Controllers\QuizController;
use App\Http\Controllers\DashboardController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
return view('dashboard');
})->name('dashboard');
Route::get('dashboard', 'App\Http\Controllers\DashboardController#index')->name('dashboard');
Route::get('quiz/create', 'App\Http\Controllers\QuizController#create');
Route::post('quiz', 'App\Http\Controllers\QuizController#store');
Route::get('quiz/{quiz?}', 'App\Http\Controllers\QuizController#show')->name('quiz/show');
Route::get('quiz/{quiz}/questions/create', 'App\Http\Controllers\QuestionController#create');
Route::post('quiz/{quiz}/questions', 'App\Http\Controllers\QuestionController#store');
Route::delete('quiz/{quiz}/questions/{question}', '\App\Http\Controllers\QuestionController#destroy');
Route::get('question/edit', '\App\Http\Controllers\QuestionController#edit')->name('question/edit');
Route::patch('question/{question}', '\App\Http\Controllers\QuestionController#update')->name('question/update');
Route::get('startquiz/{quiz}-{slug}', 'App\Http\Controllers\StartQuizController#show');
Route::post('startquiz/{quiz}-{slug}', 'App\Http\Controllers\StartQuizController#store');
My QuestionController
<?php
namespace App\Http\Controllers;
use App\Models\Question;
use App\Models\Quiz;
use Illuminate\Http\Request;
class QuestionController extends Controller
{
public function create(Quiz $quiz) {
return view('question.create', compact('quiz'));
}
public function store(Quiz $quiz) {
$data = request()->validate([
'question.question' => 'required',
'answers.*.answer' => 'required',
]);
$question = $quiz->questions()->create($data['question']);
$question->answers()->createMany($data['answers']);
return redirect('/quiz/'.$quiz->id);
}
public function destroy(Quiz $quiz, Question $question) {
$question->answers()->delete();
$question->delete();
return redirect($quiz->path());
}
public function edit(Question $question) {
return view('quiz.edit', compact('question'));
}
public function update(Request $request, Question $question, Quiz $quiz) {
//$request->validate([
//'question' => 'required',
//]);
$question->update($request->all());
//dd($quiz);
return redirect()->route('quiz/show', ['quiz' => $question->quiz])
-> with('success', 'Question updated successfully');
}
}
My edit.blade
<html>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<body>
<h1>Edit Question</h1>
<form action="{{ route('question/update',$question->id) }}" method="POST">
#csrf
#method('PATCH')
<div class="container">
<div class="row justify-content-center">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<label for="question">Question</label>
<input type="text" class="form-control" name="question" value="{{ $question->name }}"placeholder="Enter Question">
<small id="questionHelp" class="form-text text-muted">Type in your edited question.</small>
</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>
</div>
</form>
</body>
</html>
Your route for "update" does not take any parameters. Your update method of the Controller looks like it wants to use Route Model Binding but there are no parameters so you are getting a non existing instance of Quiz which doesn't have an id so its trying to use null which you can not do for route parameters when generating URLs.
You need to adjust your route URI definition to take a parameter for question and quiz. Maybe:
Route::patch('quiz/{quiz}/question/{question}', ...)
If you don't want to use route parameters for quiz or question you will have to make adjustments to the controller to get the information from the request inputs.
Other quick option:
You should be able to derive the quiz from the question so you don't need to pass the quiz along:
Route::patch('question/{question}', ...)
public function update(Request $request, Question $question)
{
$question->update($request->all());
return redirect()->route('quiz/show', ['quiz' => $question->quiz])
->with('success', 'Question updated successfully');
}
I am working on a login system for two users - admin and customer. For the admins, I've used the default auth files, but created new ones for the customer. My question is that the auth::check for admins works, but it doesn't for customers. So it doesn't query the database and logs in any email and any password. How can I fix this?
customerlogin.php
#extends('layouts.app')
#section('title','CustomerLogin')
#push('css')
#endpush
#section('content')
<div class="content">
<div class="container-fluid">
<div class="row">
<div class="col-md-8 col-md-offset-1">
#include('layouts.partial.msg')
<div class="card">
<div class="card-header" data-background-color="purple">
<h4 class="title">Customer Login</h4>
</div>
<div class="card-content">
<form method="POST" action="{{ route('customerLogin') }}">
#csrf
<div class="row">
<div class="col-md-12">
<div class="form-group label-floating">
<label class="control-label">Email</label>
<input type="email" class="form-control" name="email" value="{{ old('email') }}" required>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group label-floating">
<label class="control-label">Password</label>
<input type="password" class="form-control" name="password" required>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary">Login</button>
Back
</form>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
#push('scripts')
#endpush
CustomerLogController
<?php
namespace App\Http\Controllers;
use App\Category;
use App\Item;
use App\Slider;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AutenticatesUser;
use Illuminate\Support\Facades\Hash;
use Auth;
use Redirect;
use Session;
use Validator;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
class CustomerLogController extends Controller
{
use AutenticatesUser;
/**
* Where to redirect users after login.
*
* #var string
*/
protected function authenticated() {
if($customers = Auth::customers()){
return redirect()->route('homepage');
}
}
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
/**
* Create a new controller instance.
*
* #return void
*/
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$sliders = Slider::all();
$categories = Category::all();
$items = Item::all();
return view('homepage',compact('sliders','categories','items'));
}
public function logout(Request $request) {
$this->guard()->logout();
$request->session()->invalidate();
return redirect('/');
}
}
web.php
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::redirect('lara-admin','login');
Route::get('/','HomeController#index')->name('welcome');
Route::get ('/homepage', 'HomepageController#index')->name('homepage');
Route::post('/reservation','ReservationController#reserve')->name('reservation.reserve');
Route::post('/contact','ContactController#sendMessage')->name('contact.send');
Auth::routes();
Route::resource ('customerLogin', 'CustomerLoginController');
Route::post('homepage', 'CustomerLogController#index')->name('customerLogin');
Route::get('/logout', 'CustomerLogController#logout');
Route::group(['prefix'=>'admin','middleware'=>'auth','namespace'=>'Admin'], function (){
Route::get('dashboard', 'DashboardController#index')->name('admin.dashboard');
Route::resource('slider','SliderController');
Route::resource('category','CategoryController');
Route::resource('item','ItemController');
Route::get('reservation','ReservationController#index')->name('reservation.index');
Route::post('reservation/{id}','ReservationController#status')->name('reservation.status');
Route::delete('reservation/{id}','ReservationController#destory')->name('reservation.destory');
Route::get('contact','ContactController#index')->name('contact.index');
Route::get('contact/{id}','ContactController#show')->name('contact.show');
Route::delete('contact/{id}','ContactController#destroy')->name('contact.destroy');
});
Any help would be much appreciated!
In an older version of Laravel, when we created new auth files, we had the same issue as you're having - that Auth::Check() did not work. At all. Could not work out why.
We ended up manually checking the login, ensuring that it was correct, by using Hash::check() and then processing the login using Auth::LoginUsingId().
if(Hash::check($password, $user->u_pass)){
Auth::LoginUsingId($user->id);
return redirect()->intended('/');
}
At this point, all the Auth:: functions worked as intended.
It was a hacky solution, but it did work.
I am using model biding, but I am basically trying to create a form that can edit and update comments. It runs perfectly and even prompts me to edit the form. However, every time I try to update it the POST method is not supported for the root. which is bogus as I did the spoofing correctly, someone help me please.
Here us my CommentsController methods
public function edit($id)
{
$comment = Commenting::find($id);
return view('comments.edit', compact('comment'));
}
public function update(Request $request, $id, $posts)
{
$comment = Commenting::find($id);
$comment->update($request->all());
$comment->posts_id = $posts;
return view('post.show', compact('comment'));
}
Here is the routing in my web.php
Route::get('/posts/comment/{comment}', 'CommentsController#edit')->name('comments.edit')->middleware('auth');
Route::put('/posts/comment/{comment}', 'CommentsController#update')->name('comments.update')->middleware('auth');
Here's what I call the link to edit the form in my show.blade
Edit
lastly this is my edit.blade file
#extends ('layouts.home')
#section ('content')
<div class="card">
<h1> Edit Comment </h1>
<div class="card-block">
<form method="POST" action="{{route('comments.update', ['comments' => $comment])}}">
#csrf
#method('PUT')
<div class="form-group">
<textarea name="body" placeholder="Enter you comment here..." class="form-control"> {{$comment->body}}</textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-success">Update</button>
</div>
</form>
#include ('layouts.errors')
</div>
</div>
#endsection
Take a look your update method
public function update(Request $request, $id, $posts)
Try to delete the $posts variable on update function
Try
{{method_field('put')}}
or
<input type="hidden" name="_method" value="PUT">
add patch method in blade file:
#extends ('layouts.home')
#section ('content')
<div class="card">
<h1> Edit Comment </h1>
<div class="card-block">
<form method="POST" action="{{route('comments.update', ['comments' => $comment])}}">
{{ csrf_field() }}
<input type="hidden" name="_method" value="PATCH">
<div class="form-group">
<textarea name="body" placeholder="Enter you comment here..." class="form-control"> {{$comment->body}}</textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-success">Update</button>
</div>
</form>
#include ('layouts.errors')
</div>
</div>
#endsection
write post method in route file :
Route::post('/posts/comment/{comment}', 'CommentsController#update')->name('comments.update')->middleware('auth');
Your route binding is named comment not comments. This will not generate a URL to your comments.update route:
route('comments.update', ['comments' => $comment])
This will generate a URL to /posts/comment?comments=3 not /posts/comment/3. You want to use the same name as the route parameter so it knows to replace that route parameter:
route('comments.update', ['comment' => $comment])
Which would generate a URL to /posts/comment/3.
Also your controller method
public function update(Request $request, $id, $posts)
has a $posts variable but there is no parameter for that defined for the route, so where does that come from?
If you actually want to use Model Binding, which it would appear you are not, you could use Implicit Bindings by type-hinting a $comment argument on that controller method:
public function update(Request $request, Comment $comment)
as the route pararemeter is named comment not id.
Laravel 5.8 - Routing - Route Model Binding - Implicit Binding
I'm learning Laravel and I got stuck trying to get data from a form.
I already am able to get data back with GET, but with POST I've been having a ton of trouble. Here's what I'm working with:
Form:
<form id="forms" method="POST" action="sugestoes" novalidate>
{{ csrf_field() }}
<div class="form-row">
<div class="form-group col-md-12">
<label for="obs">Observações:</label>
<textarea type="text" class="form-control" name="obs" placeholder="Observações" required></textarea>
</div>
</div>
<hr>
<button type="submit" class="btn btn-primary">Enviar</button>
</form>
#php
if (isset($_POST["obs"])) {
echo "IN";
}
#endphp
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function store(Request $request)
{
$name = $request->input('obs');
return redirect('sugestoes');
//
}
}
Route:
Route::post('sugestoes', 'PostController#store');
The intended behaviour that I'm trying to reach is for the post to be submitted, and then returning to the same page with an empty form. Later on I'll be sending the input data into a database, but for now I just want to get the post to work.
I guess I'm missing something really basic, but I've been following guides and looking online, I've done some progress but I'm really stuck here.
(some more info, this is Laravel 5.4, and I'm using XAMPP)
First, you need to call the model, use App/Your_model_name; then you have to save the data.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Suggest; //Suggest model, let's hope you have suggest table
class PostController extends Controller
{
public function store(Request $request)
{
$suggest = new Suggest; //model
$suggest->name = $request->obs; //name is DB name, obs is request name
$suggest->save(); //save the post to DB
return redirect()->back()->with('success', 'Saved successfully'); //return back with message
}
}
Then if you want to flash the message on the HTML page
#if(session('success'))
<div class="alert alert-warning alert-dismissible" id="error-alert">
<strong style="color: white;">{{session('success')}}</strong>
</div>
#endif
<form id="forms" method="POST" action="{{ route('sugestoes') }}" novalidate>
{{ csrf_field() }}
<div class="form-row">
<div class="form-group col-md-12">
<label for="obs">Observações:</label>
<textarea type="text" class="form-control" name="obs" placeholder="Observações" required></textarea>
</div>
</div>
<button type="submit" class="btn btn-primary">Enviar</button>
</form>
Remove the #php tag below the form, then in router.php
Route::post('/sugestoes', 'PostController#store')->name('sugestoes');
Then in Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function store(Request $request)
{
$name = $request->input('obs');
return redirect('/sugestoes'); // you should have GET in Route.php
//
}
}
Add the following code in your action attribute on the form. It will capture the post URL. When you submit the form it will send the form data to the URL end-point.
action="{{ url('sugestoes')}}"
Then die and dump in your controller store function
public function store(Request $request)
{
dd($request->all());
}