I'm receiving the error "MethodNotAllowedHttpException
No message" on submit of my user's form, which is meant to update the user's table. I have two post forms on the same page and two post routes, would that have something to do with it?
I will include all the routes and another form that might be conflicting with it.
web.php
Route::get('profile','userController#profile');
Route::post('profile', 'userController#update_avatar');
Route::post('profile-update', 'userController#update_account'); //this ones not working
userController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use Auth;
use Image;
class UserController extends Controller
{
//
public function profile()
{
return view('profile', array('user' => Auth::user()) );
}
public function update_avatar(Request $request)
{
if($request->hasFile('avatar')){
$avatar = $request->file('avatar');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
Image::make($avatar)->resize(300,300)->save( public_path('/uploads/avatars/' . $filename) );
$user = Auth::user();
$user->avatar = $filename;
$user->save();
}
return view('profile', array('user' => Auth::user()) );
}
public function update_account(Request $request, $id) //the function with the error
{
User::update([
'id' => Auth::user()->id,
'name' => $request->name,
'email' => $request->email
]);
return redirect('/profile');
}
}
profile.blade.php
<img src="/uploads/avatars/{{ $user->avatar }}" style="width:150px;height:150px;float:left;border-radius:50%;margin-right:25px">
<h2>{{ $user->name }}'s Profile</h2>
<form enctype="multipart/form-data" action="/profile" method="post">
<label>Update Profile Image</label>
<input type="file" name="avatar">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" class=" btn btn-sm btn-light" style="color:#2b2b2b;" value="Update Image">
</form>
<form method="post" action="/profile-update"> <!-- The form with the error -->
{{ method_field('put') }}
{{ csrf_field() }}
<input type="hidden" name="_method" value="PUT" />
<label>Username</label>
<input type="text" name="name" class="form-control" value="{{ $user->name }}">
<label>Email</label>
<input type="email" name="email" class="form-control" value="{{ $user->email }}">
<input type="submit" id="update-account" class="btn btn-success" value="Update">
</form>
try this method:
public function update_account(Request $request, $id)
{
$user = User::find($id)
$user->name = $request->name;
$user->email = $request->email;
$user->update();
return redirect('/profile');
}
You don't have any route which can handle the PUT request for "profile-update". In your form you have defined the following function.
{{ method_field('put') }}
This helper function generate an hidden input field which will be used by Laravel to process the current request only as PUT.
To make this work, you either have to make your make your request POST by removing the above helper function or change your route method to PUT.
Route::put('profile-update', 'userController#update_account');
For those that might need the same answer, to fix this I had to play about with it for quite some time and used bits from the suggested answers to solve the issue completely.
I changed the route method to put in web.php.
Replaced my update_account function with #TonzFale answer but replaced $user = User::find($id)with $user = User::find(Auth::user()->id);.
Related
I would like to make a form.
After submitting the data,Redirect to the same page and reflect a change.
I thought return redirect should be a good way.
but it seems need to fetching the DB.
because 'ErrorException
Trying to get property 'id' of non-object (View:'error happens.
writing $user = \DB::table('users')->where('id', $request->id)... twice
is redundancy and cheesy.
Is there any good way to implement this.
class CertainController extends Controller
{
public function index(Request $request)
{
$user = \DB::table('users')->where('id', $request->id)->first();
$data = ['user' => $user];
return view('user.detail',$data);
}
public function update(Request $request)
{
\DB::table('users')
->where('id', $request->id)
->update([
$request->name => $request->value
]);
return redirect(route('user.detail', [
'user_id' => $request->id,
]));
}
}
web.php
Route::get('/user_detail', 'CertainController#index')->name('user.detail');
Route::get('/user_detail/update', 'CertainController#update')->name('user.detail.update');
blade
<form method ="GET" action={{ route('user.detail.update')}}>
<div class="form-group row">
<label>name</label>
<div class="col-md-6">
<input type = "hidden" name ="id" value="{{ $user->id }}"/>
<input type = "hidden" name = "column" value="name">
<input id="name" type="text" class="form-control #error('name') is-invalid #enderror" name="name" value="{{ $user->name }}" required autocomplete="name">
#error('')
{{ $message }}
#enderror
<button type = "submit" class ="button">submit</button>
</div>
</div>
</form>
You are looking for id in the index method,
$request->id
^^
But you are sending user_id from update method.
'user_id' => $request->id,
^^^^^
In a simple way, you can just do
return back();
here, back() is a helper function, which redirect back to where it came from.
I am new to laravel here i am trying to make a simple registration and login form,Registration form register the user in the database and login form login the user,But here i am getting this error been trying to solve this issue for many hours looked into many resources but can't figure it out,Any help would be appreciated..Thanks
Error:Undefined property: Illuminate\Support\Facades\Request::$email
Blade
#extends("layouts.master")
#section('title')
My page
#endsection
#section('content')
<div class="row">
<div class="col-md-6">
<h3>Sign-Up</h3>
<form action="{{ route('signup') }}" method="post">
<div class="form-group">
<label for="email">Your email</label>
<input class="form-control" type="text" name="email" id="email">
</div>
<div class="form-group">
<label for="first_name">First Name</label>
<input class="form-control" type="text" name="first_name" id="first_name">
</div>
<div class="form-group">
<label for="password">Password</label>
<input class="form-control" type="password" name="password" id="password">
</div>
<button type="sumbit" class="btn btn-primary">sumbit</button>
<input type="hidden" name="_token" value="{{ Session::token() }}">
</form>
</div>
<div class="col-md-6">
<h3>Login </h3>
<form action="{{ route('signin') }}" method="post">
<div class="form-group">
<label for="email">Your email</label>
<input class="form-control" type="text" name="email" id="email">
</div>
<div class="form-group">
<label for="password">Password</label>
<input class="form-control" type="password" name="password" id="password">
</div>
<button type="sumbit" class="btn btn-primary">sumbit</button>
<input type="hidden" name="_token" value="{{ Session::token() }}">
</form>
</div>
</div>
#endsection
use Illuminate\Http\Request;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::post('/signup', [
'uses' => 'UserController#postSignUp',
'as' => 'signup'
]);
Route::post('/signin', [
'uses' => 'UserController#postSignIn',
'as' => 'signin'
]);
Route::get('/dashboard', [
'uses' => 'UserController#getdashboard',
'as' => 'dashboard'
]);
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Controller
namespace App\Http\Controllers;
use App\Http\Requests;
use App\User;
use App\UserTypes;
use Auth;
use Hashids;
use Redirect;
use Request;
use Hash;
class UserController extends controller
{
public function getdashboard()
{
return view('dashboard');
}
public function postSignUp(Request $request)
{
$email = $request->email;
$first_name = $request->first_name;
$password = bcrypt($request->password);
$user = new User();
$user->email = $email;
$user->first_name = $first_name;
$user->password = $password;
$user->save();
return redirect()->route('dashboard');
}
public function postSignIn(Request $request)
{
if (Auth::attempt(['email' => $request->email, 'password' => $request->password])) {
return redirect()->route('dashboard');
}
return redirect()->back();
}
}
This helps importing use Illuminate\Http\Request; instead of use Request;
use Illuminate\Http\Request;
class UserController extends controller{
}
Change your Controller Function
use Illuminate\Http\Request;
public function postSignUp(Request $request)
{
$email = $request->input('email');
$first_name = $request->input('first_name');
$password = bcrypt($request->input('password'));
$user = new User();
$user->email = $email;
$user->first_name = $first_name;
$user->password = $password;
$user->save();
return redirect()->route('dashboard');
}
public function postSignIn(Request $request)
{
if (Auth::attempt(['email' => $request->input('email'), 'password' => $request->input('password')])) {
return redirect()->route('dashboard');
}
return redirect()->back();
}
I'm trying to create a search function in Laravel and its returning me with "undefined variable: posts" when I do foreach on my view.
My code:
Post Model
class Post extends Model {
protected $fillable = [
'creator',
'post_url',
'books',
'likes',
'created_at'
];
public function user() { return $this->belongsTo(User::class); }
}
Homeview:
<form action="{{ url('/search') }}" method="get">
<input type="text" class="search-text form-control form-control-lg" name="q" placeholder="Search" required>
</form>
Controller:
public function search($keyword)
{
$result = Post::where('books', 'LIKE', "'%' . $keyword . '%'")->get();
return view('/search', ['posts' => $result]);
}
Route:
Route::get('/search/{keyword}', 'SearchController#search');
Searchview:
#foreach($posts as $post)
<div class="post">{{ $post->id }}</div>
#endforeach
What am I doing wrong here?
This might help you out.
Homeview.blade.php
<form action="/search" method="POST">
#csrf // include your csrf token
<input type="text" class="search-text form-control form-control-lg" id="q" name="q" placeholder="Search" required>
</form>
Searchview.blade.php
<!-- or did you return a collection? -->
#if( $posts->count() > 1 )
<!-- then loop through the posts -->
#foreach( $posts as $post )
<div class="post"> {{ $post->id }} </div>
#endforeach
#else
#if( !empty($posts) )
<div class="post"> {{ $post->id }} </div>
#endif
#endif
Routes/web.php
Route::post('/search', 'PostsController#show')->name('posts.show');
PostsController
use App\Post;
public function show( Request $request )
{
$result = Post::where("books", "LIKE", "%{$request->input('q')}%")->get();
// Uncomment the following line to see if you are returning any data
// dd($result);
// Did you return any results?
return view('searchview', ['posts' => $result]);
}
The reason it wasn't working,
Route::get('/search/{keyword}', 'SearchController#search');
In your route file you were looking for a {keyword} that was never passed by the form. Your form action is action="{{ url('/search') }}". A get variable will not be picked up by a route and if it was you called the input 'q' anyway.
So then in your controller you were looking for the keyword being passed that is never passed in.
public function search($keyword)
Instead the correct thing to do is pass in the Request object like so
public function search(Request $request)
Then use $request->input('q') to retrieve the passed value through your form.
In your example $keyword would always have been blank.
Corrected code
Homeview:
<form action="{{ url('/search') }}" method="get">
<input type="text" class="search-text form-control form-control-lg" name="q" placeholder="Search" required>
</form>
Controller:
public function search(Request $request)
{
$result = Post::where('books', 'LIKE', "%{$request->input('q')}%")->get();
return view('/search', ['posts' => $result]);
}
Route:
Route::get('/search', 'SearchController#search');
Searchview:
#foreach($posts as $post)
<div class="post">{{ $post->id }}</div>
#endforeach
try:
return view('/search')->with('posts', $result);
Or even better with dinamic vars.
return view('/search')->withPosts($result);
So I was able to submit the form to allow an avatar to be uploaded and changed which worked. Now I am trying to allow all the user details to be updated too.
If I just try to change the username or email and submit, the page is reloaded with the original content. When I upload a new avatar and then try to submit I get the error "Non-static method Illuminate\Http\Request::input() should not be called statically".
*edited*** I've just realised Im saying if the user uploads an image then update the form. Whereas I want the form to submit whether or not an image has been uploaded. How could I change my update_avatar function for this to work?
web.php
Route::get('profile','userController#profile');
Route::post('profile', 'userController#update_avatar');
userController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
use Image;
class UserController extends Controller
{
//
public function profile()
{
return view('profile', array('user' => Auth::user()) );
}
public function update_avatar(Request $request)
{
if($request->hasFile('avatar')){
$avatar = $request->file('avatar');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
Image::make($avatar)->resize(300,300)->save( public_path('/uploads/avatars/' . $filename) );
$user = Auth::user();
$user->avatar = $filename;
$user->name = Request::input('username');
$user->email = Request::input('email');
$user->save();
}
return view('profile', array('user' => Auth::user()) );
}
}
profile.blade.php
<img src="/uploads/avatars/{{ $user->avatar }}" style="width:150px;height:150px;float:left;border-radius:50%;margin-right:25px">
<h2>{{ $user->name }}'s Profile</h2>
<form enctype="multipart/form-data" action="/profile" method="post">
<label>Update Profile Image</label>
<input type="file" name="avatar">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<label>Username</label>
<input type="text" name="username" class="form-control" value="{{ $user->name }}">
<label>Email</label>
<input type="email" name="email" class="form-control" value="{{ $user->email }}">
<input type="submit" class=" btn btn-sm btn-light" style="color:#2b2b2b;">
</form>
change the code
$user->name = $request->input('username');
$user->email = $request->input('email');
Please see the docs for more information:
http://laravel.com/docs/5.0/controllers#dependency-injection-and-controllers
in top of the namespace declaration in your controller ,
replace the following
use Illuminate\Http\Request;
to
use Request;
beginner trying to submit a basic form into the database using laravel. I know I should be using 'get' to display the registerform but when I do that I get the 'methodnotallowedhttpexception error.
Currently when I enter data into the form and press submit it just refreshes the page. Any help would greatly be appreciated, thanks
web.php
Route::post('registerForm', 'AuthController#viewregisterForm');
Route::post('registerUser', 'AuthController#registerUser');
AuthController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
//use Illuminate\Support\Facades\Auth;
//use App\Http\Requests;
use App\User;
class AuthController extends Controller
{
function viewregisterForm()
{
return view('register/registerForm');
}
function registerUser(Request $request)
{
$this->validate($request, [
'name' => 'required',
'email' => 'required',
'password' => 'required',
'dateofbirth' =>'required',
]);
//create a Film object
$user = new $User();
$user->name = $request->name;
$user->email = $request->email;
$user->password = $request->password;
$user->dateofbirth = $request->dateofbirth;
$user->role = 1;
$user->save();
return redirect('all');
}
}
?>
registerForm.blade
#extends('layouts.master')
#section('title', 'Register user')
#section('content')
<form action="{{url('registerForm')}}" method="POST">
{{ csrf_field() }}
<h1>Register user</h1>
<div>
<label for="title">Enter name</label>
<input type="text" name="name" id="name">
</div>
<div>
`enter code here`<label for="title">Enter email</label>
<input type="text" name="email" id="email">
</div>
<div>
<label for="title">Enter password</label>
<input type="text" name="password" id="password">
<label for="title">Enter date of birth</label>
<input type="text" name="dateofbirth" id="dateofbirth">
</div>
<input type="submit" name="submitBtn" value="Add User">
</form>
#endsection
You need to change the action of your form submit
from
<form action="{{ url('registerForm') }}" method="POST">
to
<form action="{{ route('register-user') }}" method="POST">
and change the route from
Route::post('registerUser', 'AuthController#registerUser');
to
Route::post('registerUser', ['as' => 'register-user', 'uses' => 'AuthController#registerUser']);
Try to change your routes to this:
Route::get('registerForm', 'AuthController#viewregisterForm');
Route::post('registerForm', 'AuthController#registerUser');