I'm using Xampp and Laravel and my app is throwing the following error while I'm trying to make a post route:
ERROR
Declaration of App\Http\Controllers\HandleClient::validate() should be compatible with App\Http\Controllers\Controller::validate(Illuminate\Http\Request $request, array $rules, array $messages = Array, array $customAttributes = Array)
Form
<form action="{{route('handle')}}" method="POST">
<label for="cn">Customer Name</label>
<input type="text" name="cn" placeholder="Customer Name" />
<input type="submit" value="Add Request"/>
<input type="hidden" value="{{Session::token()}}" name="_token" />
</form>
Controller HandleClient.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HandleClient extends Controller
{
public function validate(Request $request){
return view('finish',$request);
}
}
web.php routes file:
<?php
Route::get('/', function () {
return view('welcome');
})->name('home');
Route::post('/Request_handled',[
'uses' => 'HandleClient#validate',
'as' => 'handle'
]);
By default, the base Controller class usesValidatesRequests which provides a validate function to the Controller class. Naming your function validate overrides this function.
Rename your function from validate to something else and update your route, then you shouldn't have a conflict anymore.
Related
I'm Trying to solve this error but not getting any proper solution for this as i'm beginner in the laravel, Please help with the code..
error--> 1
Thank You
This is my Web.php file
Route::get('/', function() {
return view('welcome');
});
Route::get('user',function(){
return view('user');
});
Route::get('user/register',['uses'=> 'usercontroller#create']);
Route::post('/user',['uses'=> 'usercontroller#store']);
Register.blade.php
<form method="POST" method="/user">
{{ csrf_field() }}
name<input type="text" name="name">
email<input type="email" name="email">
pass<input type="password" name="password">
<input type="submit" value="register">
</form>
UserController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class usercontroller extends Controller
{
public function create()
{
return view('register');
}
public function store(Request $request)
{
User::create($request->all());
return 'Sucess';
return $request->all();
}
}
You have a mistake in your form tag (the second method should be action):
Change
<form method="POST" method="/user">
to
<form method="POST" action="/user">
I was trying to send an array through a route to another view, but when i used the function get_defined_vars(), i realized that i was sending a string with the information. Is it possible to do that?
this form from my view should send the array to my route
<form action="/trans" method="POST">
#csrf
<div class="input-group">
<input type="hidden" class="form-control" name="r" value="{{$cooperado}}">
<button type="submit" class="btn btn-primary">
<span>+</span>
</button>
</span>
</div>
</form>
then this route should send the array to the other view
Route::post('/trans', function(){
$j = Input::get('r');
return view('movs.create')->with(['j'=>$j]);
});
this is the controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Movimentacoes;
class MovimentacoesController extends Controller
{
public function create()
{
//
return view('movs.create');
}
}
routes.php
Route::post('/trans', 'MovimentacoesController#create');
controller
use Illuminate\Http\Request;
use App\Movimentacoes;
class MovimentacoesController extends Controller
{
public function create(Request $request)
{
$j = $request->request->get('r');
return view('movs.create')->with(['j' => $j]);
}
}
Code like this In the form tag:
<input type="hidden" class="form-control" name="r[]" value="{{$cooperado}}">
<input type="hidden" class="form-control" name="r[]" value="{{$cooperado}}">
<input type="hidden" class="form-control" name="r[]" value="{{$cooperado}}">
submit this form
then the Input::get('r') will be Array!
I hope it helps you.
I have a project that is going to have 8 different forms all updating the same 'user' table in my database. I have the user authentication working and it makes a user in the table on my localhost mysql database. However when I start updating the table I keep getting errors such as email is not unique or http errors or ReflectionException in RouteDependencyResolverTrait.php line 57: Internal error: Failed to retrieve the default value.
I have tried everything, my create works but it makes a new row and doesn't update the existing row which the user is signed in on.
I'm only new to Laravel 5.4 and finished going through all the Laracasts, so I'm absolutely stumped at what to do.
Does anyone have any thoughts or know how to fix it or restructure it better? Please let me know if I have missed anything out. I have been trying to get this working for 2 days.
Basics.php
<?php
namespace App;
class Basics extends Model
{
public $table = "users";
protected $fillable = [
'family_name',
'given_names'
];
}
BasicsController.php
class BasicsController extends Controller
{
public function index()
{
$user = \Auth::user();
return view('/details/basics', compact('user'));
}
public function update(Request $request, $id)
{
$basics = Basics::find($id);
$basics->family_name = $request->input('family_name');
$basics->given_names = $request->input('given_names');
$basics->save();
return redirect("/details/basics");
}
}
basics.blade.php
#extends ('layouts/app')
#section ('content')
{{--Do #includes for all form components with the components file--}}
#include ('layouts/header')
<main class="main">
<form action="/details/basics" method="POST">
<input type="hidden" name="_method" value="PATCH">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<fieldset>
<label>Family name</label>
<input type="text" name="family_name" placeholder="Family name" value="{{ old('family_name') }}" />
</fieldset>
<fieldset>
<label>Given names</label>
<input type="text" name="given_names" placeholder="Given names" value="{{ old('given_names') }}" />
</fieldset>
<button type="submit" value="Save" name="save" class="button button-primary button-wide">Save</button>
</form>
</main>
#endsection
web.php
Route::get('/', function () {
return view('welcome');
});
// Authentication Routes
Auth::routes();
Route::get('/logout', 'Auth\LogoutController#destroy');
Route::get('/home', 'DashboardController#index');
Route::get('/dashboard', function () {
$user = Auth::user();
return view('dashboard', compact('user'));
});
// Eligibility Assessments
Route::get('/assessment/student', 'AssessmentController#index');
Route::post('/assessment/results', 'AssessmentController#store');
// Details
Route::get('/details/basics', 'BasicsController#index');
Route::patch('/details/basics', 'BasicsController#update');
You need to add a rule in your post request which will exclude the email field when updating the model. This is why you're getting the email is not unique error. Although you're not posting the email field but still the save method is doing that. Try using post instead of patch. Just for debugging purposes in your Route.php
I am trying to add a record to a database utilizing a resource controller, however, I'm getting MethodNotAllowedHttpException error. I'm using a pivot table. I have gone through several similar questions like a link! but none seem to answer me. This is my code:
Routes.php
Route::group(['prefix' => 'user'], function () {
Route::resource('categories', 'User\CategoriesController');
});
CategoriesController.php
<?php
namespace App\Http\Controllers\User;
use Session;
use Sentinel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Sections;
use App\Models\Categories;
use App\Models\Users;
use App\Models\CategorieUser;
class CategoriesController extends Controller
{
public function __construct()
{
$this->middleware('sentinel.auth');
}
public function index()
{
$categories = Categories::all();
return view('user.categories.index', ['categories' => $categories]);
}
public function create()
{
$sections = sections::all();
return view('user.categories.create', ['sections' => $sections]);
}
public function store(Request $request)
{
// records in table categories
$categories = new Categories();
$categories->name = $request->name;
$categories->sections_id = $request->sections_id;
$categories->save();
// records in pivot table users_categories
$user = Sentinel::getUser()->id;
$users_categories = new CategorieUser();
$users_categories->user_id = $user;
$users_categories->categorie_id = $categories->id;
$users_categories->save();
return redirect()->route('categories.index');
}
}
This is the form:
<form action="store" method="POST">
<div class="form-group">
<label for="section">Choose section:</label>
<select class="form-control" name="sections_id">
#foreach($sections as $section)
<option value="{{ $section->id }}">{{ $section->name }}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="name">Category name:</label>
<input type="text" class="form-control" name="name" required>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<button type="submit" class="btn btn-default">Submit</button>
</form>
This is model Categories.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Categories extends Model
{
public function sections()
{
return $this->belongsTo('App\Models\Sections');
}
public function users()
{
return $this->belongsToMany('App\Models\Users', 'categorie_user');
}
}
And this is model Users.php:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Users extends Model
{
public function categories()
{
return $this->belongsToMany('App\Models\Categories', 'categorie_user');
}
}
When I add this route under routes as above
Route::group(['prefix' => 'user'], function () {
Route::resource('categories', 'User\CategoriesController');
Route::post('categories/store', ['uses' => 'User\CategoriesController#store']);
});
then everything works like a charm. I'm newbie in Laravel but I think that everything must work with out that route because I use restfull controller. Any sugestions I will appriciated. Thank you.
Finally, I find the answer. According to RESTfull resource controller the URL for form action must be the main route, not /store or /create. So in form action I wrote:
<form action="/user/categories" method="POST">
And that worked for me. Anyway, thanks for help. I hope this will help someone.
change the form action to "/user/categories" like
<form action="/user/categories" method="POST">
Im trying to POST data to my controller but I'm getting a
MethodNotAllowedHttpException in RouteCollection.php line 219:
error message, here are my files.
my route file
<?php
Route::get('/', function () {
return view('welcome');
});
// Authentication routes
Route::get('auth/login', 'Auth\AuthController#getLogin');
Route::post('auth/login', 'Auth\AuthController#postLogin');
Route::get('auth/logout', 'Auth\AuthController#getLogout');
// Registration routes
Route::get('register', 'Auth\AuthController#getRegister');
Route::post('auth/register', 'Auth\AuthController#postRegister');
Route::controllers(['password' => 'Auth\PasswordController',]);
Route::get('/home', 'HomeController#index');
// Using A Route Closure
Route::get('profile', ['middleware' => 'auth', function() {
// Only authenticated users may enter...
Route::auth();
}]);
// practicing using forms for sending data to the DB & populating form fields with DB data
Route::get('profile', 'ProfileController#index');
Route::post('profile/update', 'ProfileController#updateProfile');
profile.blade.php
<form method="POST" action="/profile/update/">
<div class="form-group hidden">
<input type="hidden" name="id" value="<?php echo $users[0]->id;?>">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input name="_method" type="hidden" value="PATCH">
</div>
<div class="form-group">
<label for="email"><b>Name:</b></label>
<input type="text" name="name" placeholder="Please enter your email here" class="form-control"/>
</div>
<div class="form-group">
<label for="email"><b>Email:</b></label>
<input type="text" name="email" placeholder="Please enter your email here" class="form-control"/>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default"> Submit </button>
</div>
</form>
& my ProfileController.php
<?php
namespace App\Http\Controllers;
use Auth;
use App\User;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class ProfileController extends Controller
{
/**
* Update user profile & make backend push to DB
**/
public function index()
{
if(Auth::check()) {
// connecting to the DB and accessing
$users = User::all();
//var_dump($users);
return view('profile', compact('users'));
}
return view('auth/login');
}
public function updateProfile(Requests $request) {
return $request->all();
}
}
not sure what the issue is. Thanks for all the help everyone
A couple of issues that we managed to address here:
Over-use of HTTP Verbs
At your view, you have: <form method="POST" but also <input name="_method" type="hidden" value="PATCH"> which may conflict between a POST and a PATCH. Since your routes.php only declares POST, let's remove the patch definition.
Routing Mistake
Still at your view, your action points to action="/profile/update/" while your route is defined as Route::post('profile/update'), notice the extra / at the end in your form. That slash should not be there.
Controllers Request
You have a here: use App\Http\Requests; is probably incorrect because that's a folder within Laravel, not a class. Let's remove that and keep use Illuminate\Http\Request; for now. In the near future, you'll be learning how to create your own Form Requests and you'll probably want a UpdateProfileRequest.php.