How to post the contents of a form to db? I know the database works.
<form method="post" action="{{ action('UserController#registration') }}">
<h1>Please Name</h1>
<textarea name="name"></textarea>
<button type="submit">Access</button>
</form>
Route::post('/registration', 'UserController#registration');
public function registration(Request $request)
{
DB::table(‘user2s’)->insert([‘name’=> $request->name]);
return 'success';
}
Add this token in your form
{!! Form::token() !!}
or this
<input type="hidden" name="_token" id="csrf-token" value="{{ Session::token() }}" />
Related
I am using Laravel and I am trying to create an edit page and call my update method on submit, the problem is I am getting a 404 when updating. This is my blade file for editing like so:
#extends('adminlte::page')
#section('title', 'AdminLTE')
#section('content_header')
<h1>Professions</h1>
#stop
#section('content')
<form method="PUT" action="/admin/professions-update/{{ $data->pkprofession }}">
<div class="form-group">
<label for="profession_name">Profession Name</label>
<input type="text" name="profession_name" id="profession_name" class="form-control" value="{{$data->profession_name}}" />
</div>
<div class="form-group">
<button type="submit" class="btn btn-success">Update</button>
</div>
</form>
#stop
Here are my routes:
Route::get('/admin/professions-edit/{id}', 'v1\ProfessionsController#edit');
Route::put('/admin/professions-update/{id}', 'v1\ProfessionsController#update');
And Here are the methods being called:
public function edit($id)
{
$data = PdTprofession::find($id);
return view('professions-edit', compact('data'));
}
public function update(Request $request, $id)
{
$data = PdTprofession::find($id);
return view('professions-edit', compact('data'));
}
Why am I getting a 404 error and how do I fix it?
Thanks,
In laravel docs, HTML forms do not support PUT, PATCH or DELETE
actions. So, when defining PUT, PATCH or DELETE routes that are called
from an HTML form, you will need to add a hidden _method field to the
form. The value sent with the _method field will be used as the HTTP
request method:
<form action="/foo/bar" method="POST">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
You may use the #method Blade directive to generate the _method input:
<form action="/foo/bar" method="POST">
#method('PUT')
#csrf
</form>
There are so many issues in your code lets resolve one by one:
action="/admin/professions-update/{{ $data->pkprofession }}">
change it to:
action="{{ url('/admin/professions-update/' . $data->pkprofession) }}">
and then HTML forms do not support PUT, PATCH or DELETE actions, so chage it to:
<form action="{{ url('/admin/professions-update/' . $data->pkprofession) }}" method="POST">
#method('PUT')
#csrf // this is required when you are using the method other then 'get'
other elements
</form>
You're missing the csrf token and the method input. Try this:
#extends('adminlte::page')
#section('title', 'AdminLTE')
#section('content_header')
<h1>Professions</h1>
#stop
#section('content')
<form method="POST" action="/admin/professions-update/{{ $data->pkprofession }}">
#csrf
#method('PUT')
<div class="form-group">
<label for="profession_name">Profession Name</label>
<input type="text" name="profession_name" id="profession_name" class="form-control" value="{{$data->profession_name}}" />
</div>
<div class="form-group">
<button type="submit" class="btn btn-success">Update</button>
</div>
</form>
#stop
Also, in your update method you are forgeting to update the object, add this to your code:
$data->update($request->all());
For more info: DOCS
I am developing application using laravel 5.2 and I have form.blade.php file in my subtasks folder in view folder.
subtasks/form.blade.php
<form class="form-vertical" role="form" method="post" action="{{ route('projects/{projectId}/task/{taskId}/subtask')}}">
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<input type="text" name="task_name" class="form-control" id="name" value="{{ old('task_name') ?: '' }}">
#if ($errors->has('task_name'))
<span class="help-block">{{ $errors->first('task_name') }}</span>
#endif
</div>
<div class="form-group">
<button type="submit" class="btn btn-info">Create Task</button>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
My controller is this
public function store(Request $request,$projectId,$taskId)
{
$subtask = new Subtask;
$subtask->subtask_name = $request->input('task_name');
$subtask->task_id = $taskId;
$subtask->project_id = $projectId;
$subtask->save();
//
}
my routes are here
Route::get('projects/{projectId}/task/{taskId}/subtask', function ($projectId, $taskId) {
return view('subtasks/form',['projectId'=>$projectId,'taskId'=>$taskId]);
});
Route::post('projects/{projectId}/task/{taskId}/subtask','SubtasksController#store');
but I got this error messages
Route [projects/{projectId}/task/{taskId}/subtask] not defined.
how can fix this?
You have two options: name your route:
Route::post('projects/{projectId}/task/{taskId}/subtask','SubtasksController#store')
->name('subtask_route);
and your form would use this. Remember to pass in the correct projectId and taskId
<form class="form-vertical" role="form" method="post" action="{{ route('subtask_route', ['projectId'=> $projectId, 'taskId'=>$taskId])}}">
Or create the url yourself:
<form class="form-vertical" role="form" method="post" action="/projects/{{$projectId}}/task/{{$taskId}}/subtask">
I am using blade template but i was to know that is there any way to use form binding on html syntax based form?. if i would do it in blade's way it would be like
{{ Form::model( $user, array('route' => array('users.update', $user->id), 'method' => 'put' )) }}
But what if i want to use it like we add a hidden field for csrf_token() like
<input type="hidden" name="_token" value="{{ csrf_token() }}" />
Here is my HTML form code:
<form class="form-group" action="/update" method="post" id="EditCommunityForm">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="text" name="community_name" class="form-control">
</form>
Edit:
i would like to ask that is there a way to convert this syntax {{ Form::model( $user, array('route' => array('users.update', $user->id), 'method' => 'put' )) }} to plain HTML?
You can't do model binding directly into html. You'll have to fill your form "manually". And, in your case, we will have to do a trick to overwrite the browser default methods (post/get).
Heres an example:
<form action="{{ route('users.update', $user->id) }}" method="post">
<!-- Overwrite post method as 'Put' -->
<input type="hidden" name="_method" value="PUT"/>
<!-- CSRF token -->
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<!-- Fills an input with a model value -->
<input type="text" name="community_name" value="{{ $user->community_name }}"/>
</form>
I have a simple form for updating book info.
<form action="{{ action('BookController#update') }}" method="POST" class="form-horizontal">
<input type="text" id="title" class="form-control" name="title" placeholder="title" value="{{ $book[0]->title }}">
<input type="text" id="author" class="form-control" name="author" placeholder="author" value="{{ $book[0]->author }}">
......................
<button type="submit" class="btn btn-primary">Save</button>
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}" />
</form>
Controller:
public function update(Request $request, $id)
{
$book = new Book;
$title = $request->input('title');
$author = $request->input('author');
$category = $request->input('category');
$date = $request->input('date');
if ($book->updateBook($id, $title, $author, $category, $date)) {
return redirect('books')->with('status', 'Successfuly edited!');
}
else {
return dd($id);
}
}
The problem is, it doesn't pass the right $id. It pass a string {books}
Basically $id = "{books}"
It should be an integer (31) from url /books/31/edit
In routes is defined as a resource with all available default methods
What can i do?
You need to pass the book's ID in the form definition, as the second argument to action():
<form action="{{ action('BookController#update', ['id' => $book[0]->id]) }}" method="POST" class="form-horizontal">
See the definition of action() for more information.
The solution was very simple: i just needed to pass $id to the view and then pass it to form action as #EricMakesStuff suggested..
try this
<form action="{{ action('BookController#update', ['id'=> 1]) }}" method="POST" class="form-horizontal">
I'm having issues with getting a laravel app to update or delete a resource.
Here is my view.
#extends('admin.master')
#section('content')
<h1>Create an Article</h1>
<form action="/articles/{{ $article->id }}">
<input type="hidden" name="_method" value="PUT">
{!! csrf_field() !!}
#include('admin.partials.forms.article')
<div class="row">
<button type="submit" class="btn btn-success btn-lg">Update Article</button>
</div>
</form>
#endsection
Here is my controller
public function update($id, Request $request)
{
return "Update Article Code Here!";
}
All I get when I submit the form is a blank page with the url
app.dev/articles/1?_method=PUT&_token=LL6Z5zHNUG1dLjjH2TDpXXCWbGnfiCKTY4cuoVbm&title=Our+Upcoming+Event+Now+Updated&description=a+brief+event+description&body=Updated+Body&category=Events
The issues is that while you have to have the hidden method to allow laravel to see what you're doing, you also have to have the method="POST".
<form action="/articles/{{ $article->id }}" method="POST">
<input type="hidden" name="_method" value="PUT">