Error trying to edit a user on Laravel 5.4 - php

I'm starting a project with laravel, and I'm having a problem when I try to edit a user.
It always return the "QueryException" as if I didn't pass the user ID, but I used the default layout for my Model, so my URL is something like /users/1/edit.
This is how my route is defined:
Route::get('/', function(){
return view('welcome');
});
Route::resource('users', 'CtrUsers');
This is the way I get the edit URL:
href="{{route('users.edit', $user)}}"
(the $user is set inside a foreach loop)
And this is my edit function:
public function edit(User $user)
{
return view('users.edit', compact('user'));
}
And something strange is that when I enter the URL /users/edit/1 (with de ID in the end), IT stops returning the QueryException, but returns "NotFoundHttpException".
Anyone had this problem?

change your controller
public function edit($id)
{
$user = User::findOrFail($id);
return view('users.edit', compact('user'));
}
Edited again
public function edit($id)
{
$user = User::where('usr_id',$id)->findOrFail();
return view('users.edit', compact('user'));
}

The issue is with the variable name which you have used for route model binding. Both variable names: the one which is on the route and another from the action parameter must match.
You can check the route info using php artisan route:list command.
So you have two options, either change the users variable name to user in the route like:
Route::resource('user', 'CtrUsers');
OR
You can change the variable name from user to users in the action parameter like:
public function edit(User $users)
{
return view('users.edit', compact('user'));
}
For more info:
https://laravel.com/docs/master/routing#route-model-binding

The best solution I found was rebuild my migrations changing the names of my tables ID's to just "ID", becouse I noticed that the errors were allways because the system doesn't find any column named usr_id.

Related

Impossible to get data from controller in Laravel 7

I want to send my data from controller to xedit.blade.php, but I get the same error
in controller:
public function index5()
{
$users=User::all();
return view('xedit')->with('users',$users);//xedit is from xedit.blade.php
}
my route:
Route::get('admin/edit', function () {
return view('xedit');
})->name('edit');
Route::get('edit', 'Admin\UserController#index5');
I get the error:
Undefined variable: users
Remove this route
Route::get('admin/edit', function () {
return view('xedit');
})->name('edit');
Because when you go to this route there is no users variable. You can pass here also if you want.
If you want named route then you can also named 2nd one like -
Route::get('edit', 'Admin\UserController#index5')->name('edit');
Also you can send user variable in first one like this-
Route::get('admin/edit', function () {
$users = App\User::all();
return view('xedit', compact('users'));
})->name('edit');

Call to a member function update() on null

I'm building my first Laravel app. That's my first problem that I can't overcome.
I tried to google something about it, but I couldn't find something that could help me.
class ProfilesController extends Controller
{
public function index(User $user)
{
return view('profiles.index', compact('user'));
}
public function edit(User $user){
return view('profiles.edit', compact('user'));
}
public function update(User $user){
$data = request()->validate([
'description' => 'required',
]);
$user->profile->update($data);
return redirect("{$user->id}/edit");
}
}
I want to get through that and update $data.
Edit
public function profile() {
return $this->hasOne(Profile::class);
}
public function posts(){
return $this->hasMany(Post::class)->orderBy('created_at', 'DESC');
}
Try this:
optional($user->profile)->update($data);
You can see the official documentation of optional() helper here.
I think you cannot update directly on an instance, you'd have to do : User::where('user_id', $user->id);
If you want to "update" an instance, you would have to do : $user->description = $data['description']; $user->save();
Do this:
if($user->profile) {
$user->profile()->update($data);
}
Hope this will help you.
I would imagine this is because the user doesn't have a profile by default.
One way you can get around this is by using withDefault() on your profile relationship in your User model e.g.
public function profile()
{
return $this->hasOne(Profile::class)->withDefault();
}
You would then need to change your controller code slightly since the profile might not exist:
Change:
$user->profile->update($data);
To:
$user->profile->fill($data)->save();
I had the same issue. As it turned out I forgot to create a new empty profile while creating a new user. So i was calling an update on the user's profile in this case null.
Try adding this to the User model and later migrate:fresh your data.
protected static function boot()
{
parent::boot();
static::created(function ($user) {
$user->profile()->create();
});
}
This will create a new profile for user automatically.

Laravel 5.2 trying to get user_id post

I am following a Laravel course on Udemy and while I followed everything the instructor did, for some weird reason I am not getting the expected result.
This is the Relationship One to One lesson and I added a function in User Model to check if it has any posts.
Then added the route to display post if user_id equals.
app\User.php
public function post() {
return $this->hasOne('App\Post');
}
app\Http\routes.php
Route::get('/user/{id}/post', function($id) {
return User::find($id)->post;
});
Below is the screenshot from the database showing that I have a post with user_id = 1 in the posts table. I also have a user with id=1 in the user's table.
MySQL data
Why do I get a blank page when visiting domain/user/1/post?
Sohel, i got a result from your function, but had to use
var_dump(User::with('post')->where('id',1)->first());
Then tried something else:
return User::with('post')->where('id',$id)->first();
And this is the result:
{"id":1,"name":"Nick","email":"nick#kriogen.name","created_at":"2018-03-15 09:49:51","updated_at":"2018-03-15 09:49:51","post":null}
Your one to one relationship should go as:
app\User.php
public function post() {
return $this->hasOne('App\Post');
}
app\Post.php
public function user() {
return $this->hasOne('App\User');
}
you can try doing this function in controller:
public function getPost {
$user= User::find($id);
return $user->post();
}
The issue was not with the functions, the issue was in the database.
Column deleted_at was not NULL and it was marking the post as being soft deleted, therefore not being displayed.
Since the "user_id" field is in the "posts" table, the relation in the App\User model need to be:
public function post() {
return $this->hasMany('App\Post');
}
then, call it without the parenthesis to get the result:
public function getPost {
$user= User::find($id);
return $user->post;
}
When you use the parenthesis, you get the builder and not the result. example:
public function getPost {
$user= User::find($id);
return $user->post()->get();
}
OR
public function getPost {
$user= User::find($id);
return $user->post()->where('name', 'like', '%hello%')->get();
}
I think you need ->hasMany() relation if you want to check if user has any posts because the user can has many posts... and the code would be:
public function posts() {
return $this->hasMany('App\Post');
}
The call:
User::find($id)->posts;

Registered user data is not saved to database. Laravel 5.2

I tried to implement role base authentication. Authentication is done correctly. If the user is admin only then he can register a new employee. But the matter is the data put into registration form is not saved to database. Route redirections are correct but data is not saved. I am using builtin auth system of laravel 5.2.
Route:: get('/register',['middleware'=> 'roles', function(){
return view('auth.register');
}]);
middleware:
public function handle($request, Closure $next)
{
if ($request->user()==null)
{
return redirect('/login');
}
if (!$request->user()->isAdmin()){
return redirect('books');
}
return $next($request);
}
}
User Model:
public function roles(){
return $this->belongsToMany('App\Role','roles_users', 'user_id', 'role_id');
}
public function isAdmin() {
return in_array(2, $this->roles()->pluck('role_id')->all());
}
if I remove the middleware then the registered data is saved properly.
I solved my problem. In built in AuthController there is a constructor with predefined middleware. Deleted it. My code works as I wanted.

Laravel 5.2 - access authenticated user in route binding

Is it possible to access the authenticated user in a route binding.
Route::bind('account', function($account_id)
{
dd(auth()->user()); // it's null :(
$account = App\Models\Account::where('business_id', auth()->user()->business_id)
->where('account_id', $account_id)
->first()
return !is_null($account) ? $account : App::abort(404);
});
I've tried grouping the route binding within some auth middleware, no dice - is this a thing? It would be really useful to pull off, to avoid extra validation in the controller.
Help appreciated.
As long as the bind is inside the Auth middleware you should be able to access it using Auth::user()
Route::bind('account', function($account_id)
{
dd(Auth::user()); // Here is the change
$account = App\Models\Account::where('business_id', Auth::user()->business_id)
->where('account_id', $account_id)
->first()
return !is_null($account) ? $account : App::abort(404);
});
You can use \Auth. This works for me:
RouteServiceProvider:
public function boot(Router $router) {
parent::boot($router);
$router->bind('account', function () {
dd(\Auth::user());
});
}
routes.php
Route::get('account/{account}', function () {
//test
});
prints user object

Categories