Vue.js - Laravel User Id - laravel

Im working on a project to learn Vue.js with Laravel.
I created a simple tasks application, where its possible to create, modify, mark as done, and delete tasks.
I wish to make it user individual, so i ran artisan make:auth to create the auth template.
Added column : user_id to tasks table.
In the Index method of TaskController instead of -
return Task::all();
I tried to do -
return Task::where('user_id', Auth::id() );
but its not working as I wish...
If instead of Auth::id() I put 1 for example, it does return the tasks for user id 1.
Can anyone help me out?

Check in your code that Auth::id() is actually returning the user's ID - it's likely that your user isn't authenticated and is therefore returning null, so the query doesn't work as you expect.
Also, if you set up your associations correctly (that is, a user hasMany tasks) then you could simplify the code by using the association instead.
return Auth::user()->tasks;
// Achieves same result as...
return Task::where('user_id', Auth::id())->get();

Related

How can i use Laravel relationships to get the value i want?

As you see on the UML diagram, one or more users can be in charge of one project thats why i had to create user_projects table.
my situation i need create a controller that get a request that contain the project_id and return all the users that are in charge of that project.
My controller:
public function getUsers(Request $request){
$users_project = UserProject::where('project_id', $request->project_id)->get();
dd($users_project);
}
i managed to send the request with the project_id and get the rows from user_projects table that has the same project_id on the variant $users_poject, i am stuck now xD...
is it possible to do the task using laravel relationships?
hope someone can help finish this controller.

Get user id from session - laravel 5.3 authentication

I have fresh Laravel 5.3 installation. Just used the php artisan make:auth
to get started with built in auth functionalities. All works fine, and after login the Auth::check() returns true, Auth::user() return the user's data and Auth::id() gives the logged in user's id.
What bothering me is that when I have only this line
$userId = Auth:id();
in my controller's action (and no other code elsewhere associated with Auth) in the Debugger panel's "Queries" tab I see the following query
select * from `users` where `users`.`id` = '34' limit 1
This extra query is highly undesirable to me (especially the select all part), as I need the user's id to make a query, selecting specific fields from users table, as well as to join it with another tables. For performance reasons I do not want to do double query.
So, is there a way to tell Laravel to save the logged in user's id in session(if it is not being saved) and get the id of the logged in user directly from session - without making any query to the database.
EDIT: I understand that after first Auth::user() call, the subsequent calls will not make requests to the database, however it is pointless, why make select * query, if I will need only some columns from the database. At least is there a way to tell Laravel to select only specific columns, instead of select * ?
Thanks
You don't need to execute another query. Use this object to get other properties:
auth()->user()->name

how to set up multi level authentication in laravel 5.3

How to define multilevel Authentication in laravel 5.3
In this project many role
Owner
Admin
employe
end user
I need Full project of laravel 5.3 authentication any one send me code
email id : tukadiyarameshr#gmail.com
Please refer to the documentation for Polymorphic relations given in the documentation and the answer given here. The answer will help you understand how to make the relations & create the users.
Once, you have understood how Polymorphic relations work... Understand this that Authentication will happen using the User table only (by email and password), but you can verify if the user is Admin, Owner or Employee in the following manner
public function checkUserRole() {
if(auth()->user()->profile_type == 'App\Models\Admin') {
return 'admin';
} else if(auth()->user()->profile_type == 'App\Models\Owner') {
return 'owner';
} else {
return 'employee';
};
}
Also, to handle user's page flow, you will have to create Middlewares and assign them to their respective routes, so no one can access the views which are not meant for them.
Have u tried like in version 5.2? it will help:
How to use multi Auth in laravel 5.2
i'm not sure if it works in 5.3 but it doesn't looks different

Retrieve selected keys from database from relations

I am using laravel 4 and I have to retrieve registrations and all users associated with that registrations but user table has lot more keys but I only need 3 keys so how can I get selected keys. I know laravel provide select method but its not working on relations.
Registration::where('section_id', '=', $key->id)
->with('user')
->select('users.id', 'users.name')
->get();
I tried users and user but nothing works. Could someone please post correct way to load this query.
Edit:
- Registration has one user
- Registrations has one section
I have registration table that contains section id and user id. I want to get all users, signed up for particular section. So this is very simple query and its working fine only problem is retrieving selected keys from database.
In your Registration model change your relation to this
public function user()
{
return $this->hasOne('User')->select('id','name');
}
And in your controller write this
Registration::where('section_id', '=', $key->id)->with('user')->get();
Hope this work for you.

Laravel passing variable from multiple models

I have 2 models
User
Customer
Each user hasMany customers (aka Accounts)
the user model has
public function customer() {
return $this->hasMany('App\Customer', 'id','customer_id');
}
but when i try to output this:
$user->customer->name
i get an error saying:
Undefined property: Illuminate\Database\Eloquent\Collection::$name
When i use this one, i get the entire customers record output in JSON.
$user->customer
So the relationship is working, but obviously i am missing something. I was sure this was the right way to do it. I swear this worked in Laravel4.2 but now that I'm in Laravel 5 it doesn't.
As the error and the below link says, It is returning a collection (hasMany) not a single object, you need to either loop over the collection or do direct access in the collection based on the index.
that is to say... does doing this work?..
$user->customer[0]->name
http://laravel.io/forum/04-10-2014-undefined-property-illuminatedatabaseeloquentcollectionitem
You should loop over customers, if you want display customers in laravel view you can do the following
#foreach
($user->customer as $cust)
{{$cust->name}}
#endforeach

Resources