I'm new to laravel. When I try to generate a form, the problem occurred.
My route
Route::get('post/add', "Post1Controller#add");
Route::post('post/store', "Post1Controller#store");
My Post1Controller
function add()
{
return view('post/create');
}
function store(Request $request)
{
echo "Had file";}
My form
{!! Form::open(['url'=> 'post/store','method'=>'POST', 'files'=> true]) !!}
<div class="form-group">
{!! Form::file('image', ['class' => 'form-control'])!!}
</div>
<div class="form-group">
{!! Form::text('title', '', ['class' => 'form-control', 'placeholder' => 'Title in here'])!!}
</div>
<div class="form-group">
{!!form::textarea('content','',['class'=> 'form-control', 'placeholder'=> 'Text in here'])!!}
</div>
<div class="form-group">
{!! Form::submit('Add', ['name'=>'sm-add', 'class' => 'btn btn-danger'])!!}
</div>
{!! Form::close() !!}`enter code here`
My issue: The GET method is not supported for this route. Supported methods: POST. Thank you for your support.
Related
I have a Laravel site running on a GoDaddy share hosting plan. I'm on PHP version 7.1. It has functioned fine up until yesterday. Now the blade views no longer show changes that occur on the database.
I see that the POST functions are working, as the changes are reflected in the database. They're just not showing up in the views themselves.
I've tried to clear the view cache as suggested here: Blade view not reflecting changes
I have also changed the timezone in config/app to 'America/Phoenix' to try and match godaddy's servers: https://www.godaddy.com/community/cPanel-Hosting/How-To-Change-Timezone-on-a-shared-server/td-p/102712
I've contacted GoDaddy's tech support and they couldn't find anything they believed could cause it.
Example Route:
//Resource
Route::resource('beer', 'BeerController');
Example Controller:
public function update(Request $request, Beer $beer)
{
$this->validate($request, ['name' => 'required']);
$beer->update(request(['name', 'beer_style_id', 'style', 'abv', 'ibus', 'srm', 'brewery_id', 'on_tap']));
return view('beers.show', compact('beer'));
}
Example View
#extends('layouts.master')
#section('content')
<div class="row">
<div class="col-sm-12">
<h1>Edit {{ $beer->name }}</h1>
<hr />
{!! Form::model($beer, ['route' => ['beer.update', $beer->id], 'method' => 'patch']) !!}
<div class="form-group">
{!! Form::label('name', 'Name') !!}
{!! Form::text('name', $value = null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('brewery_id', 'Brewery') !!}<br />
{!! Form::select('brewery_id', $breweries, null, ['placeholder' => 'Select Brewery', 'class' => 'custom-select mb-2', 'style' => 'width:100%;']) !!}
</div>
<div class="row" style="margin-bottom: 1rem;">
<div class="col">
{!! Form::label('beer_style_id', 'Style Family') !!}<br />
{!! Form::select('beer_style_id', $beerstyles, null, ['placeholder' => 'Select Style', 'class' => 'custom-select mb-2', 'style' => 'width:100%;']) !!}
</div>
<div class="col">
{!! Form::label('style', 'Style') !!}
{!! Form::text('style', $value = null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="row" style="margin-bottom: 1.5rem;">
<div class="col">
{!! Form::label('abv', 'ABV') !!}
<div class="input-group">
{!! Form::number('abv', $value = null, ['class' => 'form-control', 'step' => '.1']) !!}
<span class="input-group-addon">%</span>
</div>
</div>
<div class="col">
{!! Form::label('ibus', 'IBUs') !!}
{!! Form::number('ibus', $value = null, ['class' => 'form-control']) !!}
</div>
<div class="col">
{!! Form::label('srm', 'SRM') !!}
{!! Form::number('srm', $value = null, ['class' => 'form-control', 'step' => '.1']) !!}
</div>
</div>
<div class="form-group" id="on-tap-checkbox">
{!! Form::checkbox('on_tap', '1') !!} On Tap
</div>
<div class="form-group">
{!! Form::submit('Save', ['class' => 'btn btn-primary']) !!}
Cancel
</div>
#include('layouts.errors')
{!! Form::close() !!}
</div>
</div>
#endsection
I'm currently working on a simple blog system for my website, I'm using Laravel 5.5 for this, but I'm having a problem. Whenever I create a new article using the form I created, it is storing a empty row in my database instead of the data from the form...
This is my function to store a article:
public function newsStore()
{
$input = Request::all();
Article::create($input);
return $input;
}
According to the tutorial that I'm following should this do the trick.. This is the form that I'm currently using:
{!! Form::open(['url' => 'news']) !!}
<!-- Article Title -->
<div class="form-group row">
{!! Form::label('title', 'Title:', ['class' => 'col-2 col-form-label']) !!}
<div class="col-10">
{!! Form::text('title', null, ['class' => 'form-control']) !!}
</div>
</div>
<!-- Article Description -->
<div class="form-group row">
{!! Form::label('description', 'Description:', ['class' => 'col-2 col-form-label']) !!}
<div class="col-10">
{!! Form::text('description', null, ['class' => 'form-control']) !!}
</div>
</div>
<!-- Article Excerpt -->
<div class="form-group row">
{!! Form::label('excerpt', 'Excerpt:', ['class' => 'col-2 col-form-label']) !!}
<div class="col-10">
{!! Form::text('excerpt', null, ['class' => 'form-control']) !!}
</div>
</div>
<!-- Article Image -->
<div class="form-group row">
{!! Form::label('image', 'Image:', ['class' => 'col-2 col-form-label']) !!}
<div class="col-10">
{!! Form::file('image') !!}
</div>
</div>
<!-- Article Message -->
<div class="form-group row">
{!! Form::label('message', 'Message:', ['class' => 'col-2 col-form-label']) !!}
<div class="col-10">
{!! Form::textarea('message', null, ['class' => 'form-control', 'rows' => '7']) !!}
</div>
</div>
<!-- Article Submit -->
<div class="form-group">
{!! Form::button('Add Article', ['class' => 'btn btn-primary waves-effect waves-light float-right', 'type' => 'submit']); !!}
</div>
{!! Form::close() !!}
This is the route I'm using:
Route::post('news', 'DashboardController#newsStore'); // The articles store route
This is my model:
class Article extends Model
{
protected $fillable = [
'title',
'description',
'excerpt',
'image',
'body',
'published_at'
];
}
I have done this once before and have tried to redo it all but can't seem to figure out what is going wrong.
Thanks in advance!
To make create() work make sure you're using columns with same names. For example, if your column name is title, you need to use title instead of news-title in the form too:
{!! Form::text('title', null, ['class' => 'form-control']) !!}
As you use this Article::create(); it means all your input names should be same as table column name.
Ex:
<input name="title" ... same be same to column call news-title in respective table
If not same
Use Query Builder
DB::table('table_name')->insert(
[
'name' => $input->news-title
.....
]
);
Read More about Hyphens in column names in MySQL DB
I have two form in one view when I leave the text field empty it redirects to wrong action. I am looking forwrad for the solution of this problem. Thanks in hope.
Form one works fine when I enter data in ll the input fields. /inquiry/store also contains http request validation.
View code which contains two forms.
<div class="col-lg-6">
{!! Form::open(['role' => 'form', 'url' => '/inquiry/store', 'class' => 'form-horizontal', 'method'=>'POST']) !!}
<div class='form-group'>
{!! Form::label('name', 'Student Name *', ['class' => 'control-label col-md-4']) !!}
<div class="col-md-8">
{!! Form::text('name', $student->name,['autocomplete'=>'off' , 'placeholder' => 'Student Name', 'class' => 'form-control']) !!}
</div>
</div>
<div class='form-group'>
{!! Form::label('father_name', 'Father Name *', ['class' => 'control-label col-md-4']) !!}
<div class="col-md-8">
{!! Form::text('father_name',$student->father_name, ['autocomplete'=>'off' , 'placeholder' => 'Father Name', 'class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('class', 'Class', ['class' => 'control-label col-md-4']) !!}
<div class="col-md-8">
{!! Form::text('class', $student->admission_class,['autocomplete'=>'off' , 'placeholder' => 'Class', 'class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('roll_no', 'Roll Number', ['class' => 'control-label col-md-4']) !!}
<div class="col-md-8">
{!! Form::text('roll_no', $student->roll_no,['autocomplete'=>'off' , 'placeholder' => 'Roll Number', 'class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('date', 'Date',['class' => 'control-label col-md-4']) !!}
<div class="col-md-8 date">
<div class="input-group input-append date" id="dateRangePicker">
{!! Form::input('date', 'date', null, ['autocomplete'=>'off' , 'placeholder' => 'Date of Birth', 'class'=>'form-control col-height datepicker']) !!}
<span class="input-group-addon add-on"><span class="glyphicon glyphicon-calendar"></span></span>
</div>
</div>
</div>
<div class="form-group">
{!! Form::label('teacher_name', 'Teacher Name', ['class' => 'control-label col-md-4']) !!}
<div class="col-md-8">
{!! Form::select('teacher_name', $teacher, ['autocomplete'=>'off' , 'placeholder' => 'Teacher Name', 'class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('remarks', 'Remarks', ['class' => 'control-label col-md-4']) !!}
<div class="col-md-8">
{!! Form::textarea('remarks', null, ['autocomplete'=>'off' ,
'placeholder' => 'Remarks',
'class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
<div class="col-xs-offset-3 col-md-12">
{!! Form::submit('Save', ['class' => 'btn btn-primary']) !!}
<input type="reset" class="btn btn-default" value="Reset">
</div>
</div>
{!! Form::close() !!}
second form for searching the student. When I submit the first form with out some empty field it redirect to /inquiry/search_stu. and displays MethodNotAllowedHttpException .
<form role="form" id="search_form" action="/inquiry/search_stu" method="post" class="form-inline">
<div class='form-group'>
<label class="control-label col-md-4">Search Student *</label>
<input class="form-control" type="text" name="search" placeholder="Admission No" required>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class='form-group'>
<input type="submit" form="search_form" class="btn btn-primary" name="submit">
<input type="reset" class="btn btn-default">
</div>
</form>
Controller function for search student.
public function search_student(Request $request)
{
$teacher = \App\Teacher::lists('name', 'name');
$adminid = $request['search'];
$student = Admission::where('admission_no',$adminid)->first();
return View::make('/inquiry/create', ['student'=> $student,'teacher' => $teacher]);
}
When I submit the store form it redirects to search student.
store function .
public function store(Requests\StoreInquiryRequest $request) {
$input = Input::all();
$inquiry = new Inquiry();
$inquiry->name = $input['name'];
$inquiry->father_name = $input['father_name'];
$inquiry->date = $input['date'];
$inquiry->class = $input['class'];
$inquiry->teacher_name = $input['teacher_name'];
$inquiry->roll_no = $input['roll_no'];
$inquiry->remarks = $input['remarks'];
try {
$inquiry->save();
return redirect()->to('inquiry')->with('message', 'success| Student details have been saved.');
} catch (Exception $ex) {
\Log::error($ex);
return redirect()->to('inquiry')->with('message', 'error| There was an error adding new student, please try again later.');
}
}
Http request for validation when wants to store data
public function rules()
{
$cid = \Route::input('id');
$isEdit = isset($cid) ? true : false;
$rules = array(
'name' => 'required|string|Max:50',
'father_name' => 'required|string|Max:50',
'class' => 'required|string|Max:50',
'date' => 'required|date|Max:50',
'teacher_name' => 'required|string|Max:50',
'remarks' => 'required',
'roll_no' => 'required',
);
return $rules;
}
You can performe as many forms on one page as you wish. As far as I see you should have proper validtion in your controller - different validation for each form.
You need to use proper Laravel Validation. Reading this will make your coding process easier and faster: https://laravel.com/docs/5.0/validation
Example of validation in controller in Laravel 5.0:
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required|unique|max:255',
'body' => 'required',
]);
}
Laravel Valiation also let you perform validations with query to database qithout writing whole SQL query: https://laravel.com/docs/5.0/validation#available-validation-rules
To display errors in view use:
#if($errors->has())
#foreach ($errors->all() as $error)
<div>{{ $error }}</div>
#endforeach
#endif
Good luck!
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() !!}
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.