I am having a issue with Laravel 5.2 accessing the $errors variable in my partial.
routes.php
I have wrapped my routes in the middleware web.
and in my auth.blade.php
<div class="col-md-4">
<div class="panel panel-{{ $errors->all() ? 'danger' : 'default'}}">
<div class="panel-heading">
`enter code here`<h2 class="panel-title">#yield('heading')</h2>
</div>
<div class="panel-body">
#if($errors->all())
<div class="alert alert-danger">
<strong>We found some errors</strong>
<ul>
#foreach($errors->all() as $errors)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
#yield('content')
</div>
</div>
</div>
here is the code for my login partial
#extends('layouts.auth')
#section('title','Login')
#section('heading','Welcome Please login')
#section('content')
{!! Form::open() !!}
<div class="form-group">
{!! Form::label('email') !!}
{!! Form::text('email', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('password') !!}
{!! Form::password('password', ['class' => 'form-control']) !!}
</div>
{!! Form::submit('Login', ['class' => 'btn btn-primary']) !!}
Forgot password?
{!! Form::close() !!}
#endsection
when i click login it just redirects to the same page buit not flashing the errors?
When i click login it shows this error
enter image description here
Remove web middleware, this may cause empty $errors. Since 5.2.27, web middleware aplies automatically to all routes and you shouldn't add it again manually.
Related
I want to update the user using this Form:
{!! Form::model($user ,['method'=>'PATCH', 'action'=>['AdminController#update',$user->id], 'files'=>true]) !!}
<div class="form-group">
{!! Form::label('first_name','First Name:') !!}
{!! Form::text('first_name',null,['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('last_name','Last Name:') !!}
{!! Form::text('last_name',null,['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('username','Username (Student ID):') !!}
{!! Form::number('username',null,['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('email','Email:') !!}
{!! Form::email('email',null,['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('address','Address:') !!}
{!! Form::text('address','null',['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('phone_number','Phone Number:') !!}
{!! Form::number('phone_number','null',['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('password','Password:') !!}
{!! Form::password('password',['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Update User', ['class'=>'btn btn-primary col-sm-6']) !!}
</div>
{!! Form::close() !!}
and this is my Route:
Route::prefix('admin')->group(function(){
Route::post('/users/{id}','AdminController#update');
)};
when I press update button I receive this error:
Please help me, I am Using Laravel 5.4
try following
{{ Form::model($user, array('route' => array('users.update', $user->id), 'method' => 'PUT')) }}
// form fiedls come here
{!! Form::close() !!}
in route file add following route:
Route::resource('users', "UserController");
and in user controller add following code:
public function update(Request $request, $id){
// Add form validation here
$user = User::find($id);
$user->first_name = $request->first_name;
$user->last_name = $request->last_name;
$user->save();
Session::Flash('success', 'The User have been updated successfully!');
return redirect()->route('users.index');
}
don't user AdminController to manage users.
Please use PATCH in routes for example :
Route::patch('/users/{id}','AdminController#update');
your method is post , but you send patch request
you should change to this
'method'=>'post'
you can use this code to fix every thing
form blade
{{ Form::model($user, array('route' => array('update-user', $user->id), 'method' => 'PUT')) }}
route
Route::prefix('admin')->group(function(){
Route::PUT('/users/{id}','AdminController#update')->name('update-user');
)};
I have created a form in Laravel so here are the following files:
The form that someone should submit some details,
contact.blade.php:
#extends('layouts.layout')
#section('content')
<main role="main">
<section class="jumbotron text-center">
<div class="container">
<h1 class="jumbotron-heading">Laravel demo</h1>
<p class="lead text-muted">Please fill the form</p>
#if(count($errors) > 0)
#foreach($errors->all() as $error)
<div class="alert alert-danger">
{{$error}}
</div>
#endforeach
#endif
</div>
</section>
<div class="album text-muted">
<div class="container">
{!! Form::open(['url' => 'contact/submit']) !!}
{!! csrf_field() !!}
<div class="form-group">
{{Form::label('name', 'Name') }}
{{Form::text('name', 'Enter Name', ['class'=> 'form-control'])}}
</div>
<div class="form-group">
{{Form::label('email', 'E-Mail Address') }}
{{Form::text('email', 'example#gmail.com', ['class'=> 'form-control'])}}
</div>
<div class="form-group">
{{Form::label('message', 'Enter Message') }}
{{Form::textarea('message', 'Enter Message', ['class'=> 'form-control'])}}
</div>
<div>
{{Form::submit('Submit', ['class'=> 'btn btn-primary'])}}
</div>
{!! Form::close() !!}
</div>
</div>
</main>
#endsection
Controller :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class MessageController extends Controller
{
public function submit(Request $request){
$this->validate($request, [
'name' => 'required',
'email' => 'required'
]);
return 'SUCCESS';
}
}
*
In the Routes web.php file i have included the method as post:
Route::get('/', function () {
return view('home');
});
Route::get('/contact', function () {
return view('contact');
});
Route::post('/contact/submit', 'MessageController#submit');
The error message is " RouteCollection.php (line 251)" .After searching for similar occassions here the problem ussually occurs when in the Routes you use a different method to the specified route method. I'm using POST method for submiting details, I still cannot understand why I get this.
Any help would be appreciated.
Instead of this {!! Form::open(['url' => 'contact/submit']) !!}
Try this.
{!! Form::open(['method'=>'POST','action'=>'MessageController#submit']) !!}
Add the back slash to the url of the form like so :
{!! Form::open(['url' => '/contact/submit']) !!}
Parent blade template:
#extends('layouts.master')
#section('content')
<div class="panel-body">
#if(count($errors))
#include('common.errors')
#endif
{!! Form::model($entry, ['method' => $method, 'class' => 'form-horizontal', 'route' => [$route]]) !!}
#section('fields')
#foreach (['title', 'url', 'content', 'meta_tags', 'meta_description', 'is_active'] as $field)
#include('entries.fields.' . $field)
#endforeach
#show
<div class="form-group">
<div class="col-sm-offset-3 col-sm-6">
#section('submit')
{!! Form::submit('Добавить', ['class' => 'btn btn-default']) !!}
#show
</div>
</div>
{!! Form::close() !!}
</div>
#endsection
Child template:
#extends('entries.create')
#section('title')
Добавить статью / {{ config('site.site_name') }}
#endsection
#section('fields')
#foreach ([
'entries.fields.title',
'entries.fields.url',
'articles.fields.description',
'entries.fields.content',
'entries.fields.meta_description',
'entries.fields.is_active',
'articles.fields.enable_comments',
'articles.fields.show_on_main',
'articles.fields.rank',
] as $field)
#include($field)
#endforeach
#endsection
Model binding works for parent template, but doesn't work in child template.
For example, I have enable_comments checkbox:
{!! Form::label('enable_comments', 'Включить комментарии', ['class' => 'col-sm-3 control-label']) !!}
<div class="col-sm-6">
{!! Form::checkbox('enable_comments', null, null, ['class' => 'checkbox']) !!}
</div>
</div>
And it is always unchecked, while the $entry->enable_comments === true
I don't know why this is happening. Maybe anyone can go through the code and find the problem.
Thanks in advance for your help.
The problem is that form model binding works only inside the current template (with included files), but doesn't fill the values in child temlates. So the only solution is to write form start ( {!! Form::model(... )again, in the child template
I'm trying to update the fields in the database, but I couldn't
here is my routes :
Route::get('orders', [
'uses' => 'OrderController#postOrder',
'as' => 'order.show'
]);
here the controller:
public function postOrder()
{
$this->orderForm->validate(Input::all());
$order = $this->orders->getNew([
'link' => Input::post('link'),
'size' => Input::post('size'),
'color' => Input::post('color')
]);
$this->orders->save($order);
return Redirect::back()->withMessage('Order has been updated');
}
here is the blade:
{{ Form::open() }}
<div class="box-body">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
{{ Form::label('title', 'Product:') }}
{{ Form::text('title', $order->title, ['class' => 'form-control', ]) }}
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
{{ Form::label('link', 'Link:') }}
{{ Form::text('link', $order->link, ['class' => 'form-control']) }}
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
{{ Form::label('size', 'Size:') }}
{{ Form::text('size', $order->size, ['class' => 'form-control']) }}
</div>
</div>
<div class="col-lg-6">
</div>
</div>
<div class="box-footer">
{{ Form::submit('Save', ['class' => 'btn btn-primary']) }}
</div>
{{ Form::close() }}
so each time I try to update the order I got error "MethodNotAllowedHttpException ", I tried a lot of methods but I'm lost. I'm still beginner in php and this problem drive me crazy, so I'll be glad if you can help guys.
thanks
*** I've updated the code
So you're posting to the route, /orders. Therefor you need a HTTP POST request. You're now assigning a GET request to the /orders route.
You need to change your code to:
Route::post('orders', [
'uses' => 'OrderController#postOrder',
'as' => 'order.show'
]);
Also you need to add a CSRF Token, this can be done through adding {!! csrf_field() !!} in your blade (inside your Form open and close).
{{ Form::open() }}
{!! csrf_field() !!}
<div class="box-body">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
{{ Form::label('title', 'Product:') }}
{{ Form::text('title', $order->title, ['class' => 'form-control', ]) }}
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
{{ Form::label('link', 'Link:') }}
{{ Form::text('link', $order->link, ['class' => 'form-control']) }}
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
{{ Form::label('size', 'Size:') }}
{{ Form::text('size', $order->size, ['class' => 'form-control']) }}
</div>
</div>
<div class="col-lg-6">
</div>
</div>
<div class="box-footer">
{{ Form::submit('Save', ['class' => 'btn btn-primary']) }}
</div>
{{ Form::close() }}
Hope this works!
You must specify the method in the Form::open method.
{{ Form::open(array('method' => 'post')) }}
Just added this in the repository:
public function updateOrder($id, array $data)
{
$orders = $this->getById($id);
if (!empty($data['title'])) {
$orders->title = $data['title'];
}
if (!empty($data['link'])) {
$orders->link = $data['link'];
}
(AND SO ON)
$orders->save();
and in the controller:
public function postOrder($id)
{
$this->orders->updateOrder($id, Input::all());
return Redirect::back()->withMessage('Updated');
}
and that's it
i have this view and when i request it it shows me a strange error, well strange for me i guess.
my create.blade.php
#extends('layouts.app')
#section('content')
<div class="col-md-6">
{!! Form::open([action('PropertiesController#update', $property->id), 'files'=>true]) !!}
<div class="form-group">
{!! From::label('category', 'category') !!}
{!! Form::select('category', {{ $categories }},['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('city', 'located city') !!}
{!! Form::select('city', {{ $citys }},['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('street', 'Street adress: ') !!}
{!! Form::text('street', 'Adress', ['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::file('images') !!}
</div>
<div class="form-group">
{!! Form::label('description', 'Add description') !!}
{!! Form::textarea('description', null, ['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Done', ['class'=>'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
</div>
#stop
the error is:
ErrorException in 83c90d4b665ecd8385b3b6b2e7203d1ef06a1339.php line 8:
Parse error: syntax error, unexpected '<' (View:
/home/vagrant/Code/housing/resources/views/properties/create_property.blade.php)
please help am stuck :(
You are trying to use blade syntax within blade syntax, which you don't need to do.
{!! Form::select('category', {{ $categories }},['class'=>'form-control']) !!}
It can simply be
{!! Form::select('category', $categories, ['class'=>'form-control']) !!}
You've done it in a couple locations, so make sure to fix them all up. You'll notice the error mentions line 8, which is the line immediately after this error occurs.