Routes in laravel5 don't work correctly - php

Today I have the following problem with this routes , It has never happened to me before now.
{!! Form::open(array('route' => 'subastas/creado', 'class' => 'form')) !!}
<div class="form-group">
{!! Form::label('Your Name') !!}
{!! Form::text('name', null,
array('required',
'class'=>'form-control',
'placeholder'=>'Your name')) !!}
</div>
<div class="form-group">
{!! Form::label('Your E-mail Address') !!}
{!! Form::text('email', null,
array('required',
'class'=>'form-control',
'placeholder'=>'Your e-mail address')) !!}
</div>
<div class="form-group">
{!! Form::label('Your Message') !!}
{!! Form::textarea('message', null,
array('required',
'class'=>'form-control',
'placeholder'=>'Your message')) !!}
</div>
<div class="form-group">
{!! Form::submit('Contact Us!',
array('class'=>'btn btn-primary')) !!}
</div>
{!! Form::close() !!}
In my route controller
Route::post('subastas/creado', array(
'as' => 'subastas/creado',
'uses' => 'SubastaController#creado'
));
My controller
public function creado()
{
$usuario = new Subasta();
$usuario->name= \Request::input('name');
$usuario->save();
}
When I send the form I recieve this url ? Any idea about this problem ?
http://localhost/laravel30/public/subastas/create?_token=X93VGoFhFL9YaPYZfrTlyvn0ph9KE6Om00KmMaiv&name=asdafs&email=kfh1992%40gmail.com&message=

I assume you have another route of subastas/creado for the GET request to display the form.
In your Form::open() you're using that to generate the URL, laravel is seeing that as a GET route as thats the first one registered in your routes.php and changing the form method to GET rather than the expected POST
The solution is to change the name of the route and use that in your Form::open()
Route::post('subastas/creado', [
'as' => 'subastas/creado/post',
'uses' => 'SubastaController#creado',
]);
Then you can use the following to generate the correct form opening tag.
Form::open(['route' => 'subastas/creado/post'])

Related

Showing dropdown value in laravel form select List?

Here is my code:
controller file: EmergencyContactsController.php
$surnames = DB::table('salutations')->pluck('name');
return view('patient/emergencycontacts', ['salutation' => $surnames]);
Blade file: patient/emergencycontacts.blade.php
{!! Form::open(array('route' => 'emergencycontacts_store', 'class' => 'form')) !!}
<div class="form-group">
{!! Form::label('Salutation') !!}
{{ Form::select('role', ['' => 'Select Role'] + $salutation, null, ['class' => 'form-control']) }}
</div>
<div class="form-group">
{!! Form::label('First Name') !!}
{!! Form::text('firstname', null, array('required', 'class'=>'form-control', 'placeholder'=>'First Name')) !!}
</div>
<div class="form-group">
{!! Form::label('Last Name') !!}
{!! Form::text('lastname', null, array('required', 'class'=>'form-control', 'placeholder'=>'Last Name')) !!}
</div>
<div class="form-group">
{!! Form::label('Relationship') !!}
{{ Form::select('relationship', ['Father', 'Mother', 'Husband','Wife','Son','Daughter','Uncle','Aunty','Other']) }}
</div>
<div class="form-group">
{!! Form::label('Phone') !!}
{!! Form::text('phone', null, array('required', 'class'=>'form-control', 'placeholder'=>'Phone')) !!}
</div>
<div class="form-group">
{!! Form::label('Fax') !!}
{!! Form::text('fax', null, array('class'=>'form-control', 'placeholder'=>'Fax')) !!}
</div>
<div class="form-group">
{!! Form::submit('Save',array('class'=>'btn btn-primary')) !!}
</div>
{{ Form::close() }}
When I go to url http://localhost:8000/patient/emergency-contacts/create it gives me error:
"Unsupported operand types"
You need to change 2 things
In your controller:
$surnames = DB::table('salutations')->pluck('name', 'id')->toArray();
So you get an array as [id => 'value'] and not only ['value']. In the View:
{!! Form::select('role', $salutation, null, ['class' => 'form-control']) !!}
{!! Form::select('relationship', ['Father', 'Mother', 'Husband','Wife','Son','Daughter','Uncle','Aunty','Other']) !!}
{!! Form::close() !!}
Always 'escape' the Form tags, if not, the HTML will be printed on screen, not parsed.
please use view('patient.emergencycontacts', ['salutation' => $surnames]);

Form model data not running correctly on #yield Laravel 5

all I want is to define the form model on template and then put a yield as the content of form data. But it cannot assign the model data correctly into each field that has been defined.
This is my code:
template.detail.blade.php
#extends('admin.template.lte.layout.basic')
#section('content-page')
{!! Form::model($model, ['url' => $formAction]) !!}
#yield('data-form')
{!! Form::close() !!}
#if ($errors->any())
#stop
partial.edit.blade.php
#extends('template.detail')
#section('data-form')
<div class="form-group">
{!! Form::label('Dal_Name', 'Alternative Name', ['class' => 'required']) !!}
{!! Form::text('Dal_Name', null, ['required', 'class' => 'form-control', 'placeholder' => 'Enter Alternative Name']) !!}
</div>
<div class="form-group">
{!! Form::label('Dal_DssID', 'DSS Period', ['class' => 'required']) !!}
{!! Form::select('Dal_DssID', $dssOptions, null, ['class' => 'form-control']) !!}
</div>
<div class="checkbox">
<label for="Dal_Active">
{!! Form::hidden('Dal_Active', 'N') !!}
{!! Form::checkbox('Dal_Active', 'Y') !!}
Active
</label>
</div>
#stop
My Controller part:
/**
* Show the form for editing the specified resource.
*
* #param int $id
*
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
$this->data['model'] = DssAlternative::find($id);
$this->data['formAction'] = \Request::current();
$this->data['dssOptions'] = Dss::lists('Dss_Name', 'Dss_ID');
return view('partial.edit', $this->data);
}
But the model data does not propagate to form correctly.
Sorry for my bad english.
It won't work since you are passing the $model object to partial/edit.blade.php file, and hoping to use it within template/detail.blade.php
exactly at this line
{!! Form::model($model, ['url' => $formAction]) !!}
Solution:
in template/detail.blade.php Take the form model line inside like this:
#extends('admin.template.lte.layout.basic')
#section('content-page')
#yield('data-form')
#if ($errors->any())
#stop
So partial/edit.blade.php would be like below:
#extends('template.detail')
#section('data-form')
{!! Form::model($model, ['url' => $formAction]) !!}
<div class="form-group">
{!! Form::label('Dal_Name', 'Alternative Name', ['class' => 'required']) !!}
{!! Form::text('Dal_Name', null, ['required', 'class' => 'form-control', 'placeholder' => 'Enter Alternative Name']) !!}
</div>
<div class="form-group">
{!! Form::label('Dal_DssID', 'DSS Period', ['class' => 'required']) !!}
{!! Form::select('Dal_DssID', $dssOptions, null, ['class' => 'form-control']) !!}
</div>
<div class="checkbox">
<label for="Dal_Active">
{!! Form::hidden('Dal_Active', 'N') !!}
{!! Form::checkbox('Dal_Active', 'Y') !!}
Active
</label>
</div>
{!! Form::close() !!}
#stop

Laravel 5.1 formfacade can't get value from select

I have created a form to change the user name and its role in the website.
#extends('layout/layoutAdmin')
#section('content')
<div>
<h1>{{ $user -> name }}<h1>
<p>{{ $user -> email }}<p>
</div>
{!! Form::model($user, ['url' => 'admin/menu/user_profiles/' . $user->id, 'method' => 'PATCH']) !!}
<div class="row">
<div class ="form-group">
{!! Form::label('name', 'Name:') !!}
{!! Form::text('name', $user->name,['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('role', 'Role:') !!}
{!! Form::select('role', array('admin' => 'admin', 'super_admin' => 'super admin',
'super_researcher' => 'super researcher', 'researcher' => 'researcher',
'consultant' => 'consultant', 'user' => 'user'), $user->role)
!!}
</div>
<div class="form-group col-xs-8 col-md-7">
{!! Form::submit('Update', ['class' => 'btn btn-primary']) !!}
</div>
</div>
{!! Form::close() !!}
#stop
Everything is correct with the controller and I can change the name just fine, but I cannot save a new value for the role. it always stays the same.
Could anyone tell me how to save the role value?
EDIT!!!!
I did not have role field as fillable!

mismatch token laravel on add form

I have this form to create a new article. User inputs everything correctly but i get a return of TokenMismatchException in VerifyCsrfToken.php line 53:. I saw another post saying that because i used the open form Tag it will already add this for me and to take it out. How do i take this out? or is there some other problem that im overlooking?
here is my controller
public function store( AddArticleController $request)
{
$request= $request->all();
$request['user_id']=Auth::id();
Article::create($request->all());
return redirect('/user');
}
and here is the html
<form class="form-horizontal" role="form" method="POST" action="{{ url('/article/add') }}">
{!! Form::open(
array(
'class' => 'form',
'novalidate' => 'novalidate',
'files' => true)) !!}
<div class = "form-group">
{!! Form::label('title','Title:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::text('title',null, ['class' => 'form-control']), !!}
</div>
</div>
<div class = "form-group">
{!! Form::label('title','Image:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::file('image') !!}
</div>
</div>
<div class="form-group">
{!! Form::label('title','Type:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-lg-1">
{!! Form::select('type', array('select' => 'select','fashion' => 'Fashion', 'music' => 'Music', 'dance' => 'Dance', 'event' => 'Event'))!!}
</div>
</div>
<div class = "form-group">
{!! Form::label('body','Comment:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::textarea('body',null, ['class' => 'form-control']) !!}
</div>
</div>
<div class = "form-group">
{!! Form::label('body',' ', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form:: submit('Sumbit', ['class' => 'btn btn-primary form-control']) !!}
</div>
</div>
{!! Form::close() !!}
</form>
First, which version of Laravel do you use?
The Laravel's HTML Helper has been removed in the version 5 of Laravel and is now not updated. If you still want to use this helper I recommend you to use the version updated by the community https://github.com/LaravelCollective/html.
For your problem, if you don't use the helper, you need to add the token manualy by adding {!! csrf_token() !!} in your form. You can find more information in the documentation: http://laravel.com/docs/5.1/routing#csrf-protection
You should get rid of either the <form> tag or from {{ Form::open() }} from your code.
The Laravel's form generator (which is depricated since Laravel 5) allows you to write that syntax {{ Form::open() }} which is rendered to plain html tag <form>.
so in your case the right way to go would be either
<form class="form form-horizontal" novalidate="novalidate" enctype="multipart/form-data" role="form" method="POST" action="{{ url('/article/add') }}">
{!! csrf_token() !!} // note that in this case we need to put csrf token manuall
......
</form>
OR
{!! Form::open(['action' => url('/article/add'), 'novalidate' => 'novalidate' 'files' => true, 'class' => 'form form-horizontal']) !!}
... //your fields and you don't have to explicitly include csrf token. Form::open() will do that for you
{!! Form::close() !!}

Patch update Authenticated User in Laravel

I'm trying to edit/update profile information into my Users table.
My idea is for the authenticated user to be able to edit his own profile in the Users table.
During the registration, you are only required to fill out a couple specific items (username, name, lastname, email, password) but I've also added a couple extra columns to the User table (city, country, phone, twitter, facebook).
I have a profile user page (route= '/profile') where all the information is shown. Of course all columns that aren't required during the registration are not filled in:
I also have an edit page where the columns that need info added are editable:
Here is the code for this editprofile.blade.php (where I try to send out a PATCH method):
...
{!! Form::model($user, ['route' => 'user/' . $user , 'method' => 'PATCH']) !!}
<div class="form-group form-horizontal">
<div class="form-group">
{!! Form::label('username', 'Username:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
<label class="align-left">{{ $user->username}}<label>
</div>
</div>
<div class="form-group">
{!! Form::label('email', 'E-mail:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
<label class="align-left">{{ $user->email}}<label>
</div>
</div>
<div class="form-group">
{!! Form::label('name', 'Name:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
<label class="align-left">{{ $user->name}} {{ $user->lastname}}<p>
</div>
</div>
<br />
<div class="form-group">
{!! Form::label('city', 'City:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::Text('city', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('country', 'Country:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::Text('country', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('phone', 'Phone:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::Text('phone', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('twitter', 'Twitter link:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::Text('twitter', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('facebook', 'Facebook link:', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::Text('facebook', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
{!! Form::submit('Save Profile', ['class' => 'btn btn-primary']) !!}
</div>
</div>
</div>
</div>
{!! Form::close() !!}
...
I have this is my Http/routes.php:
# Profile
Route::get('/profile', 'PagesController#profile');
Route::get('/profile/edit', 'ProfileController#edit');
Route::bind('user', function($id) {
$user = App\User::find($id)->first();
});
Route::patch('user/{user}', 'ProfileController#update');
I have an Http/Requests/UpdateUserRequest.php to validate the request:
<?php namespace App\Http\Requests;
use App\Http\Requests\Request;
use Illuminate\Foundation\Http\FormRequest;
class UpdateUserRequest extends Request {
public function authorize()
{
return false;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'city' => 'max:30',
'country' => 'max:30',
'phone' => 'max:30',
'twitter' => 'max:30',
'facebook' => 'max:30'
];
}
}
And my Http/Controllers/ProfileController.php with the update fuction:
<?php namespace App\Http\Controllers;
use Auth;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class ProfileController extends Controller {
public function edit()
{
$user = Auth::user();
return view('pages.editprofile')->withUser($user);
}
public function update(UpdateUserRequest $request, User $user)
{
$user->fill($request->all());
$user->save();
return redirect()->view('pages.editprofile')->withUser($user);
}
}
At the moment it seems I can't even open my 'editprofile.blade.php' page anymore, unless I remove the 'route' & 'methode' from my form.
I keep getting this error:
Could anyone guide me on how I should exactly trigger the PATCH methode?
change your form opening tag to
{!! Form::model($user, ['route' => 'user/' . $user->id , 'method' => 'PATCH']) !!}
you forgot '->id'
UPDATE
Change you Route
from
Route::patch('user/{user}', 'ProfileController#update');
to
Route::patch('user/{user}', 'as' => 'profile.patch', 'ProfileController#update');
and your form opening tag to
{!! Form::model($user, ['route' => array('profile.patch', $user->id), 'method' => 'PATCH']) !!}
You need to change:
{!! Form::model($user, ['route' => 'user/' . $user , 'method' => 'PATCH']) !!}
into:
{!! Form::model($user, ['route' => 'user/' . $user->id , 'method' => 'PATCH']) !!}
At the moment you create URL in your form with User object converted to Json - that's why it doesn't work.

Categories