getting data from database in laravel using the username - php

I have a table in database called 'User'. All I want is to get the basic information of the user. I've tried to get data from database but the thing that keep me bugging is that I get all the data inside the table.
UserTable
|id|username|firstname|lasttname|middlename|gender|age|birthdate|address|contact_number|designation|
1 admin lee chiu kim male 47 07-22-87 cebu 0977452445 admin
2 lrose rose loo mar female 27 04-02-88 cebu 0977452445 manager
3 mgray gray men try male 37 01-22-89 cebu 0977452445 employee
UserProfile.php --> Model
<?php
class UserProfile extends Eloquent {
public $timestamps=false;
protected $table = 'users';
protected $fillable=array('firstname','lastname', 'middlename', 'gender', 'age', 'birthdate', 'address', 'contact_number', 'designation');
}
This is the model of my site.
UserProfileContoller.php --> Controller
<?php
class UserProfileController extends \BaseController {
public function index($id)
{
$userprofile = User::find($id);
return View::make('userprofile.index', array('userprofile' => $userprofile));
//return View::make('userprofile.index')->with('UserProfiles',UserProfile::get());
}
For example I am currently log in to username admin and I want to edit and view my profile. How can I get the data just for admin only. Please Help.
The situation hers is that. let say they have 3 employees. So in the table 3 username. lets assume that I'm not the admin. I will log in using lrose. and the other one log in as admin and so.on. If the three of us will go to our profiles the only thing to display there is their own profile and can edit it.

Assuming you property set-up your database why not just use the DB object?
// In the User table find the item with the username 'admin'
DB::table('User')->where('username', '=', 'admin')->get();
To get the information of some user that is logged in (given you have their username), called something like $userprofile. Then all we need to do is find by id then get the username:
// $id is the ID of the logged in user
$userprofile = User::find($id)->username; // get username of logged in user

$userprofile::find($id) will get you the information for admin only, if $id = 1.
find() only returns 1 tuple from the database.
After you call find(), you can access the data as such:
$userprofile->firstname

you mean when you pass $id to index method of User Controller all the data of other users are returned?
That's kinda weird.
$userprofile = User::find($id); change that line to $userprofile = User::find(1); and see if you can only get the data of admin.
If not, probably you are calling other method. Probably you iterate over the index method somewhere else for multiple times so that you get all the data in the database. To confirm both possibilities, just add echo 'I am in' at the entry point of your index method. If no echo message is shown, the first possibility is true. If multiple echo messages occur, the second possibility is true.
Hope this helps.

first of all, you need to use Session after login and store the id of the username in the Session.
e.g.
Step 1:
if($login) //successfull login
{
Session::put('userid', $userid);
//this id, you have to retreive it from the database after successful login
}
Step 2:
Setup the route
Route::get('profile',['uses' => 'UserProfileContoller#index']);
Step 3:
Controller
public function index()
{
if(is_null(Session::get('userid'))) return Redirect::to('login');
return View::make('userprofile.index', ['userprofile' => $this->model->index()]);
// Above code uses dependency injection to retrieve the information from db.
}
Step 4:
UserProfileModel
public function index()
{
return DB::table('user')->where('id','=', Session::get('userid'))->first();
}
p.s. i find it easier to use query builder than eloquent orm. if you want to use orm, change the code where needed.

Related

Laravel many to many retrieve data across multi table

Database Table Relationship
What I did:
public function index(Request $request)
{
$company_feedback = $request->user()->company()
->get()->pluck('id')- >toArray();
$company = Company::whereIn('id',$company_feedback);
$feedback = $company->feedback;
return view('feedback.index', ['feedbacks' => $feedback]);
}
By using eloquent relationship, how to retrieve feedback data from a specific user id ? I want show feedback data which belong to current login user login id.
anyone could help? show me how to write the code in Index method in Feedback class.
Assuming you have two models User.php and Feedback.php
If you want to retrieve all feedback given by a current user
In your User.php
public function feedback()
{
//assuming you have user_id column in feedback table
return $this->hasMany("App\Feedback",'user_id');
}
In your controller
//all feedback given by the current user
$feedbacks = Auth::user()->feedback;

store variables on user model laravel 4

my user model has 3 fields: username, password and category_id
so when i auth the auth object will have all of the 3 variables, but i want to add the name of the category that is on other model
and i get like:
$category = Category::find(Auth::user()->category_id);
$category_name = $category->name;
so the question is who can i add $category_name to the Auth::user() object, in order to retrive it every time he is logged like this:
Auth::user()->category_name
i try Session::put("category_name","Category 1") when you loggin, but when i close the windows and open it by the last closed windows, it delete that variable.
i want to store the variable since the person login, untill the person logout, but if the person has logged in and close the window and then the person re open the page the variable must be filled
You can use Eloquent Accessors
In User model
public function setCategoryNameAttribute()
{
$categoryID = $this->category_id;
$category = Category::find($categoryID);
return $category->name;
}
You can access category name like
Auth::user()->category_name
try this
in your User model add the following function
EDIT try this
public function category_name()
{
return \Cache::remember('category_' . $this->id , 60, function()
{
return $this->belongsTo('App\Category')->get();
});
}
now everytime you use
Auth::user()->category_name
you will have the name
this will eco the name in your View
{{ Auth::user()->category_name }}
NOTE : this code is not the best solution even if it worked for you, this is open for changes, i just want you to get the point that if you want to access something via Auth::user()->getname you have to add the getname function in the User model
and by the way can you show us the relation you set in you model? because that would make it easier
an exemple
if you have something like this in ur model(hasone)
public function category() {
return $this->hasOne('category');
}
you can do this
Auth::user()->category->name

Getting Model from within Collection

Something very basic but I'm having a hard time solving this.
I have a list of users in the database that show as online users. I am fetching these users by their user_id
Model
public function scopeloggedInUser($query){
return $query->select('user_id')->get();
}
when I var_dump or dd it shows that its a collection of a list of currently logged in users. (Said it was super simple).
I need to fetch those individual users. How do I dilute this to the individual user within the Online Model.
Within the Controller
public function index(Online $online)
{
$activeuser = $online->loggedInUser();
return view('user.user', compact('activeuser'));
}
In your online-model specify a relationship to the real user like this:
public function user()
{
return $this->hasOne('App\User');
}
In your view you can now access each user in your foreach-loop like this:
foreach ($activeusers as $user)
{
echo $user->user->username; // or whatever fields you need
}
But to be honest: in your case I wouldn't set up a new database table and new model if you need this functionality.
Move your logic to your User model and add a boolean field to your user table and change your query-scope to this (again: in your user model)
public function scopeOnline($query){
return $query->where('online', 1);
}
You also shouldn't do a get() within a scope because then you have no more access to the query builder. For example: you want all logged in users that are female.
With get: not pretty.
Without get:
User::online()->where('gender', '=', 'female')->get();

CodeIgniter get userinfo by username if username exists, if not get by userID

What I'd like to create is basically something similar like Facebook does. If user have posted his/her's username, the link would be like: www.mysite.com/user/{username}, if not link would be like: www.mysite.com/user/{userid}. With the codes below I'm getting undefined indexes for all of the fields I'm trying to retrieve from the dataabase. It does work only with user_id but not with username. Any help would be much appreciated.
Link to profiles (Views):
<a href="<?=base_url()?>user/<?=isset($event['username']) ? $event['username'] : $event['creatoruserid']?>">
<img src="<?=base_url()?>public/images/uploads/profiles/<?=$event['profilepic']?>" class="event-profile-pic">
</a>
Route to profiles:
$route['user/(:any)'] = "user/profile/$1";
User Controller with user method:
<?php
class User Extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('event_model');
$this->load->model('user_model');
}
public function profile($user_id)
{
// All the data we need in profile page
$data['user'] = $this->user_model->getUserInfo($user_id);
$data['events'] = $this->event_model->getEventsById($user_id);
$data['total_events_created'] = $this->user_model->TotalEventsCreated($user_id);
$data['total_comments'] = $this->user_model->TotalComments($user_id);
$data['total_attending_events'] = $this->user_model->TotalAttendingEvents($user_id);
$this->load->view('/app/header');
$this->load->view('/app/profile', $data);
$this->load->view('/app/footer');
}
}
Model for retrieving the userinfo from database:
<?php
class User_model extends CI_Model
{
public function getUserInfo($user_id)
{
$this->db->select('*')->from('users')->where(['user_id' => $user_id]);
$query = $this->db->get();
return $query->first_row('array');
}
}
You can probably handle this in your SQL layer. The proposition is to find the user record either by their username or by their user ID. So, in SQL (CodeIgniter Active Record) you can phrase this as:
$this->db->select('*')
->from('users')
->where(['user_id' => $user_id])
->or_where(['user_name'] => $user_id])
->limit(1);
In this case we are assuming the $user_id can be a string (user_name) or an integer (user_id). We are also assuming that you have no user_names that are numeric and may by chance match a user_id. To prevent multiple rows being returned, you could add a limit to the query.
Once you have a user record, then you will need to pass in the found user record's user_id in the other queries instead of the user_id passed into the function.
Please be sure your 'username' is match column name in DB, and can you add echo statements such as echo 'A'; echo 'B'; to determine exactly where is undefined indexes error is?

Dynamically populating the dropdown menus in PHP Cake. Access Model from another Model?

I am building a Cake PHP application. Different users have different properties so I use two objects to store a user for example
User hasOne Student / Student belongs to User
User hasOne Lecturer / Lecturer belongs to User
The profile edit page will allow the User to edit all their details for both objects. I've set up the form and used saveAll so save both objects. My problem is dynamically populating the dropdown menus depending on which role the user has.
For example the counties field. Admin does not have an address whereas Student and Lecturer do. I have setup my Country model to find all my counties and put them into opt-groups in the select box (sorting them by country as shown here Dropdown list with dyanmic optgroup)
I can do this fine inside the Students/LecturersController as they allow me to access the Country model as I set $uses variable. I do not want to do this inside the UsersController as not all user roles have an address and an address is never stored inside the User object. I tried putting the code in the model but then I don't know how to access other Models inside a Model. Up to now I've had no problem building the app and I feel that I may have made a bad design decision somewhere or there's something I'm not understanding properly.
Essentially I'm asking how do I implement the setForForm() function below.
public function edit() {
//get user
$user = $this->Auth->user();
//get role
$role = $user['User']['role'];
if ($this->request->is('post') || $this->request->is('put')) {
//get IDs
$userID = $user['User']['id'];
$roleID = $user[$role]['id'];
//set IDs
$this->request->data[$role]['user_id'] = $userID;
$this->request->data[$role]['id'] = $roleID;
$this->request->data['User']['id'] = $userID;
//delete data for role that is not theirs
foreach ($this->request->data as $key => $value) {
if($key !== 'User' && $key !== $role) {
unset($this->request->data[$key]);
}
}
if ($this->User->saveAll($this->request->data)) {
//update logged in user
$this->Auth->login($this->User->$role->findByUserId($userID));
$this->Session->setFlash('Changes saved successfully.');
} else {
$this->Session->setFlash(__('Please try again.'));
}
}
//set role for easy access
$this->set(compact('role'));
//sets required variables for role form
$this->User->$role->setForForm();
//fills in form on first request
if (!$this->request->data) {
$this->request->data = $user;
}
//render form depending on role
$this->render(strtolower('edit_' . $role));
}
//this is the method I would like to implement in the Student/Lecturer model somehow
public function setForForm() {
$counties = $this->Country->getCountiesByCountry();
$homeCounties = $counties;
$termCounties = $counties;
$this->set(compact('homeCounties', 'termCounties'));
}
Not sure if I get your question correct, but I think what you want is the following:
User hasOne Student / Student belongs to User
and
Student hasOne Country / Country belongsTo Student
(and the same for Lectureres)
then from your UsersController you can do:
$this->User->Student->Country->findCountiesByCountry();
hope that helps
--
EDIT:
if you want to want to use $role instead of Student/Lecturer you would have to do it like this:
$this->User->{$role}->Country->findCountiesByCountry();
In the end I decided to just load counties in the user controller regardless of whether the form will need them or not since Admin is the only user that doesn't need them.
Try something like this:
public function setForForm() {
App::import('model', 'Country');
$Country = New Country();
$counties = $Country->getCountiesByCountry();
$homeCounties = $counties;
$termCounties = $counties;
$this->set(compact('homeCounties', 'termCounties'));
}
I don't know is this the best solution or not but it is working at least :)
Edited
Here is another mistake. Its not recommended to set variable from model to view , you can return data that will be set then in edit function normally set it to the view , but anyways if you need to set from model to the view you can load controller class to your model using App::import(); and use set function.

Categories