var $errors in the view with the laravel framework - php

i use the validated method for validate request, but the var errors is empty in the view :/.
in the controller, i have:
Route::group(['middleware' => ['web']], function () {
Route::get('/', function(){
return view('home');
})->name('home');
Route::group(['prefix' => 'do'], function($name = null){
Route::get('/{action}/{name?}', ['uses' => 'controllers#get', 'as' => 'get']);
Route::post('/', ['uses' => 'controllers#post', 'as' => 'post' ]);
});
});
for the controller, i have:
<?php namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class controllers extends Controller
{
public function get($action, $name=null)
{
return view('actions.' . $action, ['name' => $name]);
}
public function post(Request $request)
{
$this->validate($request, [
'action' => 'required',
'name' => 'alpha|required'
]);
return view('actions.'.$request['action'] , ['action' => $request['action'], 'name'=>$this->transformName($request['name'])]);
}
private function transformName($name)
{
$add = "king ";
return $add.strtoupper($name);
}
}
and for the view, i have:
#extends('layouts.master')
#section('content')
<div class="centered">
greet
hug
kiss
<br >
<br >
#if (isset($errors) && count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
{!! $errors->first() !!}
#endforeach
</ul>
</div>
#endif
<form action="{{ route('post')}}" method="post">
<label for="select-action">I want to...</label>
<select id="select-action" name="action">
<option value="greet">greet</option>
<option value="hug">hug</option>
<option value="kiss">kiss</option>
</select>
<input type="text" name="name">
<button type="submit">action</button>
<input type="hidden" name="_token" value="{{Session::token()}}">
</form>
</div>
#endsection
if i delete the middleware of the route, the var errors work correctly.
how i can fix this?
thanks.

If you're using latest Laravel 5.2 build, you should remove web middleware from routes. Now it applies automatically to all routes and if you're trying to add it manually, it causes different errors.

Related

Route [admin.distroyDish] not defined. (View: /home/abdalrazag/MyResutrant/resources/views/admin/manager.blade.php)

I am using Laravel 7 to build Restaurant system
I made form that go to specific route
and I get name route but it is display error as the title
manager view
<form method="post" action="{{route('admin.distroyDish')}}" >
#csrf
<div class="form-group">
<select class="form-control" name="dish">
#for ($i = 0 ; $i < count($InetialData['dish']); $i++))
<option value="{{ $InetialData['dish'][$i]->id }}">
{{ $InetialData['dish'][$i]->name }}
</option>
#endfor
</select>
</div>
<input type="submit" name="delete_dish" value="Delete" class="btn btn-danger">
<br>
</form>
web Route file
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/manager', 'ManagerController#dashboard');
Route::post('/manager', 'ManagerController#addItem');
Route::get('/manager/{id}', 'ManagerController#deleteTable')->name('admin.deleteTable');
Route::post('/manager', 'ManagerController#destroyDish')->name('admin.distroyDish');
Route::post('/manager', 'ManagerController#addIngrediant')->name('admin.addIngrediant');
Route::get('/kitchen', 'KitchenController#index')->name('kitchen.home');
Route::get('/kitchen/{id}', 'KitchenController#submitDish')->name('kitchen.submit.dish');
Route::post('/kitchen', 'KitchenController#addIngrediant')->name('kitchen.addIngrediant');
ManagerController
private function initData()
{
$InetialData = array(
'category' => DB::table('Category')->get(),
'dish' => DB::table('Items')->get(),
'users' => DB::table('users')->get(),
'Ingrediant' => DB::table('Ingrediant')->get(),
'IngrediantHistory' => DB::table('IngrediantHistory')->get()
);
return $InetialData;
}
public function destroyDish(Request $request)
{
DB::table('Items')
->where('id', '=', $request->dish)
->delete();
return redirect('/manager')->with('InetialData' , $this->initData());
}
other route like kitchen is working
why this route is not working ??
You've got duplicate routes:
Route::post('/manager', 'ManagerController#addItem');
Route::post('/manager', 'ManagerController#destroyDish')->name('admin.distroyDish');
Route::post('/manager', 'ManagerController#addIngrediant')->name('admin.addIngrediant');
Change the endpoint structure.
As I can see you have more routes calling POST /manager. Try to use routes like resources:
GET /entities list
GET /entities/{entity} view
POST /entities create
PUT /entities/{entity} update
DELETE /entities/{entity} delete
Elegant and easy to mantain and read.

Use input from one view to print result into another view

I want to use input from a form in one view and print results into another view. I get the following error: Undefined variable: users Thanks in advance!
The form (in a view called 'dashboard') that I am using to get email address:
...
<div class="search">
<header><h3>Search Friend</h3></header>
<form action="{{ route('search.friend') }}" method="post">
<div class="form-group">
<input class="form-control" type="text" name="email" id="email" placeholder="Friend's email">
</div>
<button type="submit" class="btn btn-primary">Post</button>
<input type="hidden" value="{{ Session::token() }}" name="_token">
</form>
</div>
...
The route to send data from 'dashboard' to Controller:
Route::post('/searchfriend',[
'uses' => 'FriendController#getSearchFriend',
'as' => 'search.friend',
'middleware' => 'auth'
]);
The controller where I use the email to find user:
class FriendController extends Controller
{
public function getSearchFriend(Request $request)
{
$this->validate($request,[
'email' => 'required | email'
]);
$email = $request['email'];
$users = User::where('email',$email)->get();
return view('userlist',['$users' => $users]);
}
}
The route to send the result to a 'userlist' view:
Route::get('/userlist',[
'uses' => 'FriendController#getSearchFriend',
'as' => 'userlist',
'middleware' => 'auth'
]);
Finally, the 'userlist' view:
#extends('layouts.master')
#section('title')
Users
#endsection
#section('content')
<section class="row new-post">
<div class="col-md-6 col-md-offset-3">
<header><h3>Users</h3></header>
<div class="userlist">
<header><h2>Click to Add Friend</h2></header>
#foreach($users as $user)
Name: {{ $user->username }}
#endforeach
</div>
</div>
</section>
#endsection
Change:
class FriendController extends Controller
{
public function getSearchFriend(Request $request)
{
$this->validate($request,[
'email' => 'required | email'
]);
$email = $request['email'];
$users = User::where('email',$email)->get();
return view('userlist',['$users' => $users]);
}
}
to:
class FriendController extends Controller
{
public function getSearchFriend(Request $request)
{
$this->validate($request,[
'email' => 'required | email'
]);
$email = $request['email'];
$users = User::where('email',$email)->get();
return view('userlist',['users' => $users]);
}
}
You don't need the $ when passing the name of the variable to the view.
#Ryan J Field is correct. Also, you can pass the variable in many different ways. Such as -
return view('userlist')->with('users', $users);
Or,
return view('userlist', compact(users));

Laravel form validation doesn't seem to work

I'm making my first form in Laravel and the generation of the form is all working. It's just that the store function seems to blindly return the user to my contact page irrelevant of the result of the form validation.
So if the email address posted isn't an email but a random string I still get returned to the /contact page with the thank you message being sent to the view.
My controller looks like this:
namespace App\Http\Controllers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\DB;
use App\Http\Requests\ContactFormRequest;
class ContactController extends Controller {
public function create(){
return view('contact');
}
public function store(ContactFormRequest $request) {
return \Redirect::route('contact')
->with('message', 'Thanks for contacting us!');
}
}
And the form handler like this:
namespace App\Http\Requests;
use Illuminate\Http\Request;
class ContactFormRequest extends Request {
public function authorize() {
return true; // don't need to be registered to run form so true
}
public function rules() {
return [
'email' => 'required|email',
];
}
}
This is controlled by the following routes
if (config('app.tan_site_page_contact')===true){
Route::get('/contact', ['as' => 'contact', 'uses' => 'ContactController#create']);
Route::post('/contact', ['as' => 'contact_store', 'uses' => 'ContactController#store']);
});
And the form like this:
<ul>
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
#if(Session::has('message'))
<div class="alert alert-info">
{{Session::get('message')}}
</div>
#endif
{!! Form::open(array('route' => 'contact_store', 'id' => 'contactCallMeForm')) !!}
{!! Form::label('Your E-mail Address') !!}
{!! Form::text('email', null, array('required', 'class'=>'form-control', 'placeholder'=>'Your e-mail address')) !!}
{!! Form::submit('Contact Us!', array('class'=>'btn btn-primary')) !!}
{!! Form::close() !!}
The form html seems fine,with a token and valid url
<form method="POST" action="http://localhost/contact" accept-charset="UTF-8" id="contactCallMeForm" novalidate="">
<input name="_token" type="hidden" value="VNHchLZhfsXadVZXCZWHGdAuJ4zgmO6cDJIGhR59">
<label for="Your E-mail Address">Your E-mail Address</label>
<input required="required" class="form-control" placeholder="Your e-mail address" name="email" type="text">
<input class="btn btn-primary" type="submit" value="Contact Us!">
</form>
The problem here is your ContactFormRequest class.
You extends it from invalid Request class. You extend it from \Illuminate\Http\Request class but you should extend it from \Illuminate\Foundation\Http\FormRequest (or \App\Http\Requests\Request class)

Laravel NotFoundHttpException in RouteCollection.php line 161:

I'm having a hard time on this error when I encounter this error, My solution is to create new project. I know this question is alway ask here. I followed all tutorial line by line and is always error.
Sign Up:
#extends('layouts.master')
#section('content')
<div class="col-md-6">
<form method="POST" action="{{ route('signup') }}">
<div class="form-group">
<input type="text" name="username" placeholder="Username" class="form-control"></input>
</div>
<div class="form-group">
<input type="password" name="password" placeholder="Password" class="form-control"></input>
</div>
<div class="form-group">
<input type="submit" value="Submit" class="btn btn-primary pull-right"></input>
<input type="hidden" name="_token" value="{{ Session::token() }}"></input>
</div>
</form>
</div>
#endsection
routes.php
<?php
Route::get('/', function () {
return view('welcome');
});
Route::post('/signup', [
'uses' => 'UserController#postSignUp',
'as' => 'signup'
]);
UserController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
class UserController extends Controller
{
public function postSignUp(Request $request){
$username = $request['username'];
$password = bcrypt($request['password']);
$user = new User();
$user->username = $username;
$user->password = $password;
$user->save();
return redirect()->back();
}
}
I don't see a get route for your form. Your post route will work only if you submit the form.
So, in your routes.php, there should something like
<?php
Route::get('/', function () {
return view('welcome');
});
Route::get('/signup', [
'uses' => 'UserController#getSignUp',
'as' => 'signup'
]);
Route::post('/signup', [
'uses' => 'UserController#postSignUp',
'as' => 'signup'
]);
And, in UserController#getSignUp you just load the view of the form.
//your blade file is OK ,you can use same
//you route
Route::get('/signup', function () { //route1 to view form
return view('signUp');
});
Route::post('/signup', [ // route2 to handle form request
'uses' => 'UserController#postSignUp',
'as' => 'signup'
]);
to view your form you have to use get method which is route1 ,after submitting the form your postSignUp method will redirect to get route which is route1.
//your controller
public function postSignUp(Request $request){
$username = $request->username; //to access username
$password = bcrypt($request->password); //to access password
$user = new User();
$user->name = $username;
$user->password = $password;
$user->save();
return redirect()->back();
}
in your blade file you use name filed as username and password for user's username and password respectively.so In user Usercontroller you can directly access them as $request->username and $request->password for User's username and password respectively.

Laravel 5.2 : MethodNotAllowedHttpException in RouteCollection.php line 219

I want to save one form data through my task controller. But when i go to url to access my form. it's showing the following Error:
MethodNotAllowedHttpException in RouteCollection.php line 219:
Here is my Routes.php
<?php
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/', function () {
return view('welcome');
});
Route::get('/all_item','TestController#index');
Route::post('/create_item','TestController#create');
Route::get('/home', 'HomeController#index');
});
Here is my TaskController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Test;
use App\Http\Requests;
use Redirect;
class TestController extends Controller
{
public function index()
{
$alldata=Test::all();
// return $alldata;
return view('test.itemlist',compact('alldata'));
}
public function create()
{
return view('test.create_item');
}
public function store(Request $request)
{
$input = $request->all();
Test::create($input);
return redirect('test');
}
}
Here is the create_item page( post form / view page)
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="panel panel-default">
<div class="panel-heading">Create Item</div>
{!! Form::open(array('route' => 'Test.store','class'=>'form-horizontal','method' => 'patch')) !!}
{!! Form::token(); !!}
<?php echo csrf_field(); ?>
<div class="form-group">
<label>Item Code</label>
<input type="text" name="item_code" class="form-control" placeholder="Code">
</div>
<div class="form-group">
<label>Item Name</label>
<input type="text" name="item_name" class="form-control" placeholder="Name">
</div>
<button type="submit" class="btn btn-default">Submit</button>
{!! Form::close() !!}
</div>
</div>
</div>
</div>
#endsection
You're using PATCH method in form, but route with POST method
try
'method' => 'patch'
change to
'method' => 'post'
LaravelCollective's HTML only supports methods POST, GET, PUT DELETE
so you might want to change that to POST or PUT
'method' => 'POST'
You haven't declared a Test.store route in your Routes.php, so try adding a resource or a named route:
Route::post('/store_item', [
'as' => 'Test.store', 'uses' => 'TestController#store'
]);
As i can see TestController#create is a post method.But it behaves like a get method.Try passing Request $request parameter to the create method.Or else if you really need a get method for the create method, change the method as get in Routes.php as like this,
Route::get('/create_item','TestController#create');

Categories