Issue updating user account with avatar laravel - laravel

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;

Related

Too few arguments to function App\Http\Controllers\Update::update(), 0 passed nd exactly 7 expected

I want to update datas in database in Laravel, but I get error.
Here is my Route:
Route::post('/resetPOST', [App\Http\Controllers\Update::class, 'Update'])->name('id');
Here is my page:
<form method="POST" action="/resetPOST">
#csrf
<input type="hidden" name="id" value="{{ Auth::user()->id }}">
<input type="text" name="name" class="form-control" required value="{{ Auth::user()->name }}">
<input type="text" name="surname" class="form-control" required value="{{ Auth::user()->surname }}">
<input type="text" name="middlename" class="form-control" required value="{{ Auth::user()->middlename }}">
<input type="email" name="email" class="form-control" autocomplete="off" required value="{{ Auth::user()->email }}">
<input type="date" name="dateofbirth" class="form-control" required value="{{ Auth::user()->dateofbirth }}">
<input type="text" name="idnumber" class="form-control" required value="{{ Auth::user()->idnumber }}">
<button type="submit" class="btn btn-outline-primary" >Изменить</button>
</form>
Here is my App\Http\Controllers\Update code:
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
class Update extends Controller
{
function update($id, $name, $surname, $middlename, $email, $datebirth, $idnumber){
echo $id;
// DB::table('users')-->where('id', '=', $id ->update(['name' => $name,'surname' => $surname,'middlename' => $middlename,'email' => $email,'datebirth' => $datebirth, 'idnumber' => $idnumber]));
}
}
This is my error:
ArgumentCountError
Too few arguments to function App\Http\Controllers\Update::update(), 0 passed in D:\openserver\OpenServer\domains\localhost\laravel\right-univercity\vendor\laravel\framework\src\Illuminate\Routing\Controller.php on line 54 and exactly 7 expected
You definitely should not have id in your form. A user would be able to manipulate that and update anyones profile. id should just be taken from Auth::user()->id (within your update method) without ever appearing in your form.
Try changing to this:
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
class Update extends Controller
{
public function update(Request $request)
{
$name = $request->input('name');
// do the same for everything else..
// echo $name;
DB::table('users')
->where('id', '=', Auth::user()->id)
->update([
'name' => $name
]);
redirect()->back();
}
}

Undefined property: Illuminate\Support\Facades\Request::$email

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();
}

Facing Error in updating value in two table in laravel

public function update(Request $request, $user)
{
$user= User::find($user);
$user->name = $request->input('name');
$mobile= Mobile::find($user);
$mobile->id = $request->input('id');
$mobile->country_code = $request->input('country_code');
$mobile->phone_number = $request->input('phone_number');
$user->save();
return redirect('/dashboard')->with('alert-success', 'updated');
}
this code is not working for me ... i want to update the value in two table one table is (user) and second table is (mobile)..
You are not committing the changes to the Mobile model to your database.
Calling $model->save() commits any changes to the that instance of that model object, to the database.
However, it looks like you're using the wrong value to 'find' the mobile model. If you're using Laravel relationships correctly, my edit to that part should help.
I tend to handle changes to my models individually in my code, so it is more readable. Here's how I would do it:
$user= User::find($user);
$user->name = $request->input('name');
$user->save();
$mobile = $user->mobile //this part relies on you having set up the relationship correctly.
$mobile->id = $request->input('id');
$mobile->country_code = $request->input('country_code');
$mobile->phone_number = $request->input('phone_number');
$mobile->save();
Edit A Form
<form class="" action="{{ route('user.update',$user->id) }}" method="POST" name="">
<input type="hidden" name="_method" value="PATCH">
<input type="text" name="id" value="{{$user->id}}">
{{ csrf_field() }}
<Input type="text" name="name" value="{{$user->name}}" >
<Input type="text" name="email" value="{{$user->email}}" >
#if($user->mobile)
<Input type="text" name="country_code" value="{{$user->mobile->country_code}}">
<Input type="text" name="phone_number" value=" {{$user->mobile->phone_number}}">
#else
<Input type="text" name="country_code" value="{{$user->mobile->country_code}}">
<Input type="text" name="phone_number" value=" {{$user->mobile->phone_number}}">
#endif
<button type="submit" class="btn btn-danger btn-sm" value="" >update</span></button>
<span class="pull-right">
</div>
its my edit.blade.php
controller for edit
$editinfo = Mobile::find($user);
return view('edit',compact('user'));
You are not performsave() on $mobile instance
are you forgot add $mobile->save()
you could use tap()function
$user= User::findOrFail($id);
$user_update = tap($user)->update([
'country_code' => $request->input('country_code'),
'phone_number' => $request->input('phone_number'),
]);
further read: https://laravel.com/docs/5.6/helpers#method-tap

"No message" error laravel - trying to update user account information

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);.

Laravel routes, form won't submit data into the database

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');

Resources