I get an exception when returning $request from my controller.
The form in creat.blade.php:
<form action="{{ route('admin.category.store') }}" method="POST" enctype="multipart/form-data">
#csrf
<div class="form-group form-float">
<div class="form-line">
<input type="text" id="name" class="form-control" name="name">
<label class="form-label">Category Name</label>
</div>
</div>
<div class="form-group">
<input type="file" name="image">
</div>
<button type="submit" class="btn btn-primary m-t-15 waves-effect">SUBMIT</button>
</form>
CategoryController
public function store(Request $request)
{
return $request;
}
Related
There is nothing changed when I'm updating my record. Before this I don't put $id in the function but after make some research, I need to put it and also put in the route. Still, I do not have any idea know where is my mistakes.
web.php
Route::get('/administrator','AdminController#index');
Route::post('/admin/create','AdminController#create');
Route::get('/admin/{id}/edit', 'AdminController#edit');
Route::post('/admin/{id}/update', 'AdminController#update');
Route::post('/admin/delete', 'AdminController#destroy');
AdminController
public function edit($id) {
$admin = Admin::find($id);
return view('admins.edit',['admins'=>$admin]);
}
public function update(Request $request, $id) {
$admins = Admin::find($id);
$admins->name = $request->name;
$admins->branch = $request->branch;
$admins->email = $request->email;
if ($request->hasFile('avatar')){
$filename = $request->avatar->getClientOriginalName();
$request->avatar->storeAs('images/'.$request->email,$filename,'public');
$admins->avatar = $request->email.'/'.$filename;
}
else
{
$admins->avatar='default.png';
}
$admins->save();
return redirect('/administrator')->with('success','Your details are updated!');
profile.blade.php
<h4 class="heading"><b><center>Admin's Details</center></b></h4>
<ul class="list-unstyled list-justify"><center>
<li><b>ID: </b>{{$admins->id}}</li>
<li><b>Name: </b>{{$admins->name}}</li>
<li><b>Branch: </b>{{$admins->branch}}</li>
<li><b>Email: </b>{{$admins->email}}</li></center>
</ul>
<div class="text-center">Edit Profile</div>
edit.blade.php
<div class="panel-body">
<form action="/admin/{id}/update" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<div>
<div class="form-group hidden">
<label for="id">id</label>
<input type="text" class="form-control" name="id" value="{{$admins->id}}"/>
</div>
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" value="{{$admins->name}}"/>
</div>
<div class="form-group">
<label for="address">Branch</label>
<input type="text" class="form-control" name="branch" value="{{$admins->branch}}" />
</div>
<div class="form-group">
<label for="email">Email Address</label>
<input type="email" class="form-control" name="email" value="{{$admins->email}}"/>
</div>
<div class="form-group">
<label for="avatar">Avatar</label>
<input type="file" name="avatar" class="form-control-file" value="{{$admins->avatar}}">
</div>
</div>
<button type="submit" class="btn btn-primary">Update</button>
</form>
</div>
I hope someone can help me solve this problem.
add name() to route then call in view
Route::post('/admin/{id}/update', 'AdminController#update')->name('admin.update');
in blade
<form action="{{route('admin.update',$admins->id)}}" method="post" enctype="multipart/form-data">
web.php
Route:: resources ('admin', 'AdminController');
edit.blade.php
<div class="panel-body">
<form action="{{route('admin.update',$id)}}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<div>
<div class="form-group hidden">
<label for="id">id</label>
<input type="text" class="form-control" name="id" value="{{$admins->id}}"/>
</div>
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" value="{{$admins->name}}"/>
</div>
<div class="form-group">
<label for="address">Branch</label>
<input type="text" class="form-control" name="branch" value="{{$admins->branch}}" />
</div>
<div class="form-group">
<label for="email">Email Address</label>
<input type="email" class="form-control" name="email" value="{{$admins->email}}"/>
</div>
<div class="form-group">
<label for="avatar">Avatar</label>
<input type="file" name="avatar" class="form-control-file" value="{{$admins->avatar}}">
</div>
</div>
<button type="submit" class="btn btn-primary">Update</button>
</form>
</div>
AdminController
public function update(Admin $admin, Request $request)
{
$admin->update([$this->requestValidate($request)]);
$this->storeImage($admin);
return redirect(route('admin.index'));
}
public function requestValidate($request)
{
return $request->validator([
'name' => 'string | required',
'branch' => 'string |required',
'email' => 'string | required | email'
]);
}
public function storeImage($request)
{
if ($request->hasFile('avatar')) {
$filename = $request->avatar-
>getClientOriginalName();
$request->avatar->storeAs('images/' . $request-
>email, $filename, 'public');
$request->avatar = $request->email . '/' . $filename;
} else {
$request->avatar = 'default.png';
}
}
Use this stardant with validate maybes it helps you...
i'm working on a crud app to learn laravel i'm doing good so far , other than when i want to update a post it gives me this
put method is not supported for this route
#extends ('layouts.app')
#section('content')
<form action="{{route('update_blog_path',['blog'=>$blog->id])}}" method="POST">
#method('PUT')
#csrf
<div class="form-group">
<label for="title">Title </label>
<input type="text" name="title" class="form-control" value={{$blog->title}}>
</div>
<div class="form-group">
<label for="title">Content </label>
<input type="text" name="content" class="form-control" value={{$blog->content}}>
</div>
<div class="form-group">
<button type="submit" class="btn btn-outline-primary">Edit</button>
</div>
</form>
#endsection
<?php
Route::get('/', function () {
return view('welcome');
});
Route::name('blogs_path')->get('/blogs','BlogController#index');
Route::name('create_blog_path')->get('/blogs/create','BlogController#create');
Route::name('store_blog_path')->post('/blogs','BlogController#store');
Route::name('blogs_path1')->get('/blogs/{id}','BlogController#show');
Route::name('edit_blog_path')->get('/blogs/{id}/edit','BlogController#edit');
Route::name('update_blog_path')->put('/blogs/{id}','BlogController#updtae');
Try please;
#extends ('layouts.app')
#section('content')
<form action="{{ route('update_blog_path', ['blog' => $blog->id]) }}" method="POST">
#csrf
{{ mehod_field("PUT") }}
<div class="form-group">
<label for="title">Title </label>
<input type="text" name="title" class="form-control" value={{$blog->title}}>
</div>
<div class="form-group">
<label for="title">Content </label>
<input type="text" name="content" class="form-control" value={{$blog->content}}>
</div>
<div class="form-group">
<button type="submit" class="btn btn-outline-primary">Edit</button>
</div>
</form>
#endsection
and
Route
<?php
Route::put('update_blog_path/{blog}', 'BlogController#update')->name("update_blog_path");
And your'e code wrong update term
Route::name('update_blog_path')->put('/blogs/{id}','BlogController#updtae');
change update
Looks like you have a typo in your routes file, change
Route::name('update_blog_path')->put('/blogs/{id}','BlogController#updtae');
to
Route::name('update_blog_path')->put('/blogs/{id}','BlogController#update');
You misspelled the method name update.
Hi i try to save fields in db ot 2 steps but i have MethodNotAllowedHttpException
on step 1 in my controller i save the 2 fields in db
public function storeNumber(Request $request){
$number = new Number;
$number->user_id = $user = \Auth::user()->id;
$number->number = $this->getGeneratedNumber();
$number = new Number($request->all());
$number->save();
return redirect('numbers/{id}/details');
}
view
<form class="form-horizontal" method="POST" action="{{action('NumberController#storeNumber')}}">
{{ csrf_field() }}
<div class="form-group">
<div class="col-md-12">
<button type="submit" class="btn btn-primary btn-block">
Generate Numbers
</button>
</div>
</div>
model
class Number extends Model
{
/**
* #var array
*
*/
protected $fillable = [
'number'
];
}
on step 2 i want to save another field in same db with same controller this is my another store function for store another fields in same db . but when i try to save laravel say MethodNotAllowedHttpException.
public function store(Request $request, $id){
$number = Number::find($id);
$number = new Number($request->all());
$number->save();
return redirect('numbers');
}
this is my view
<form method="post" action="{{action('NumberController#store', $id)}}">
{{csrf_field()}}
<input name="_method" type="hidden" value="PATCH">
<div class="form-group">
<label for="number" class="control-label">Number</label>
<input type="text" class="form-control form-control-lg disabled" placeholder="Number" name="number" value="{{$number->number}}">
</div>
<div class="form-group">
<label for="comment" class="control-label">Comment</label>
<textarea name="comment" class="form-control form-control-lg" cols="30" rows="10">{{$number->comment}}</textarea>
</div>
<div class="form-group">
<label for="accept" class="control-label">Accept</label>
<input type="radio" name="accept" value="1">Yes<br>
<input type="radio" name="accept" value="0">No<br>
</div>
#if($number->accept == 1)
<div class="form-group">
<label for="name" class="control-label">Number</label>
<input type="text" class="form-control form-control-lg disabled" placeholder="Name" name="name" value="{{$number->name}}">
</div>
<div class="form-group">
<label for="city" class="control-label">City</label>
<input type="text" class="form-control form-control-lg" placeholder="City" name="city" value="{{$number->city}}">
</div>
<div class="form-group">
<label for="postcode" class="control-label">Postcode</label>
<input type="number" class="form-control form-control-lg" placeholder="Postcode" name="postcode" value="{{$number->postcode}}">
</div>
<div class="form-group">
<label for="address">Address</label>
<textarea name="address" class="form-control form-control-lg" cols="30" rows="10">{{$number->address}}</textarea>
</div>
#else
<p>TODO status for NO</p>
#endif
<div class="form-group">
<div class="col-md-12">
<button type="submit" class="btn btn-default">Finish</button>
</div>
</div>
</form>
You have a "_method" filed with "PATCH" value, so you have to change the route to "patch" instead of "post".
Please change blade content of update as follow
<form method="post" action="{{action('NumberController#store', $id)}}">
to
<form method="post" action="{{action('NumberController#update', $id)}}">
return redirect('numbers/{id}/details');
Here is incorrect, what is id?
am try to send value form from by post method to controller
here is my view,and how can i use post method to send
<form class="form-horizontal" role="form">
<div class="form-group">
<label class="col-lg-3 control-label">Title:</label>
<div class="col-lg-8">
<input class="form-control" value='{{ $words->first()->title }}' type="text">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Meaning:</label>
<div class="col-lg-8">
<input class="form-control" value="{{ $words->first()->meaning }}" type="text">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<input class="btn btn-primary" value="Save Changes" type="button">
<span></span>
<input class="btn btn-default" value="Cancel" type="reset">
</div>
</div>
</form>
here is controller method like
public function postSaveedit($meaning){
}
using route by controller
You should read up on Requests in Laravel: https://laravel.com/docs/5.2/requests#accessing-the-request
You need to pass that to your controller
public function postSaveedit(Request $request) {
$input = $request->input();
$foo = $input['foo'];
$bar = $input['bar'];
$baz = $input['baz'];
}
In Laravel 5.2 you can use request() helper method to solve your problem...
This is how you can do it...
Routes file should look like this (be sure that this route should be of post type)
Route::post('/myurl', 'Controllername#postSaveEdit')->name('postSaveEdit');
Form file should look like this, also please specify the input field names in the form so that you can grab them in the the controller by their specified names (like - title, meaning - see below code)...
<form class="form-horizontal" action="{{ route('postSaveEdit') }}" method="POST" role="form">
<div class="form-group">
<label class="col-lg-3 control-label">Title:</label>
<div class="col-lg-8">
<input class="form-control" name="title" value='{{ $words->first()->title }}' type="text">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Meaning:</label>
<div class="col-lg-8">
<input class="form-control" name="meaning" value="{{ $words->first()->meaning }}" type="text">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<button class="btn btn-primary" type="submit">Save Changes</button>
<span></span>
<input class="btn btn-default" value="Cancel" type="reset">
</div>
</div>
</form>
and controller should be like this...
public function postSaveEdit() {
// The inputs variable contains all your form's inputs in the form of array...
$inputs = request()->all();
/*
$inputs = array(
'title' => 'title_value',
'meaning' => 'meaning_value'
)
*/
// Wheareas you can also get them by using 'get' method on request method like this
$title = request()->get('title');
$meaning = request()->get('meaning');
}
here u go
Form
you have to add method to your form + names to your inputs
<form class="form-horizontal" role="form" method="POST">
<!-- Add csrf token -->
{!! csrf_field() !!}
<div class="form-group">
<label class="col-lg-3 control-label">Title:</label>
<div class="col-lg-8">
<input class="form-control" value='{{ $words->first()->title }}' type="text" name="input1">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Meaning:</label>
<div class="col-lg-8">
<input class="form-control" value="{{ $words->first()->meaning }}" type="text" name="input2">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<input class="btn btn-primary" type="submit" value="Save Changes"/>
<span></span>
<input class="btn btn-default" value="Cancel" type="reset">
</div>
</div>
</form>
controller
Use Word; // at the top of the class
public function postSaveedit(Request $request) {
$word= new Word; // if you are creating a new record
$word= Word::find(1);// if you are updating a record
$word->title = $request->input('input1');
$word->meaning= $request->input('input2');
$word->save();
return view('home.blade.php');
}
Routes file
Route::get('/myurl', 'Controllername#postSaveedit');
:)
am try to send request by post form from but error TokenMismatchException
here is my controller code
public function postSaveedit(Request $request) {
$var1 = $request->input('title'); //name on the form
$var2 = $request->input('meaning'); //name on the form
$words = User::where("title", $var1)->update(array("meaning" => $var2));
return view('dict.watch', compact('words'));
}
here is view code.
<form class="form-horizontal" role="form" method="POST" action="{{ URL::to('index/saveedit') }}">
<div class="form-group">
<label class="col-lg-3 control-label">Title:</label>
<div class="col-lg-8">
<input class="form-control" value='{{ $words->first()->title }}' type="text" name="title">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Meaning:</label>
<div class="col-lg-8">
<input class="form-control" value="{{ $words->first()->meaning }}" type="text" name="meaning">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<input class="btn btn-primary" value="Save Changes" type="submit">
<span></span>
</div>
</div>
</form>
thnak you for your help
From https://laravel.com/docs/master/routing:
Laravel automatically generates a CSRF "token" for each active user
session managed by the application. This token is used to verify that
the authenticated user is the one actually making the requests to the
application.
Anytime you define a HTML form in your application, you should include
a hidden CSRF token field in the form so that the CSRF protection
middleware will be able to validate the request. To generate a hidden
input field _token containing the CSRF token, you may use the
csrf_field helper function
Just add this line inside your form
<form class="form-horizontal" role="form" method="POST" action="{{ URL::to('index/saveedit') }}">
<input type="hidden" name="_token" value="{{ csrf_token(); }}">
<div class="form-group">
<label class="col-lg-3 control-label">Title:</label>
<div class="col-lg-8">
<input class="form-control" value='{{ $words->first()->title }}' type="text" name="title">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Meaning:</label>
<div class="col-lg-8">
<input class="form-control" value="{{ $words->first()->meaning }}" type="text" name="meaning">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<input class="btn btn-primary" value="Save Changes" type="submit">
<span></span>
</div>
</div>
</form>