Can't get id from Auth::check - laravel

I try to get user id in the Laravel app. In my User.php I added this code
if (Auth::check())
{
$id = Auth::id();
echo '<script>console.log($id)</script>';
}
I get below given error:-
Uncaught ReferenceError: $id is not defined
What am I doing wrong?

You are mingling the logic of JS and PHP :)))
if (Auth::check())
{
$id = Auth::id();
echo '<script>console.log('.$id.')</script>';
}
OR simply echo it?
if (Auth::check())
{
$id = Auth::id();
echo $id;
}
OR you could use a double quote if you really wanna print it under script tags with your current format:
if (Auth::check())
{
$id = Auth::id();
echo "<script>console.log($id)</script>";
}

Access Id like this:
$id = Auth::user()->id;

Related

Undefined index: email in codeigniter

im trying to log in but i keep encountering the error below. attached is my controller and model
controller
public function login(){
//load session library
$this->load->library('session');
$this->load->model('Model_students');
$email = $_POST['email'];
$password = $_POST['password'];
$data = $this->Model_students->login($email, $password);
if($data)
{
$id=$data[0]->id;
$first_name=$data[0]->firstname;
$last_name=$data[0]->lastname;
$this->session->set_userdata('user_id',$id);
$this->session->set_userdata('lname',$last_name);
$this->session->set_userdata('user', $email);
$this->session->set_userdata('fname',$first_name);
$this->getImg();
redirect('Students/homepage');
}
else{
$this->session->set_flashdata('error','Invalid login. User not found');
}
}
model
public function login($email,$password)
{
// $query = $this->db->get_where('users', array('email'=>$email, 'password'=>$password));
$query = $this->db->get_where('users', array('email'=>$email));
if($query->num_rows() == 1 )
{
return $query->result();
}
wheni i run this i get an error:
Message: Undefined index: email
Filename: controllers/Students.php
Line Number: 145
please don't use $_POST parameter without validation. use input library
public function login(){
//load session library
$this->load->library('session');
$this->load->model('Model_students');
$post = $this->input->post(NULL, TRUE);
$email = isset($post['email']) ? $post['email'] : '';
$password = isset($post['password']) ? $post['password'] : '';
$data = $this->Model_students->login($email, $password);
if($data && !empty($data)){
$id = $data[0]->id;
$first_name = $data[0]->firstname;
$last_name = $data[0]->lastname;
$this->session->set_userdata('user_id', $id);
$this->session->set_userdata('lname', $last_name);
$this->session->set_userdata('user', $email);
$this->session->set_userdata('fname', $first_name);
$this->getImg();
redirect('Students/homepage');
}else{
$this->session->set_flashdata('error', 'Invalid login. User not found');
}
}

My page could not be found, can someone help me?

Here is my function in the controller
public function list()
{
$customers = Customer::all();
foreach ($customers as $customer) {
$list = [
'Customer ID' => $customer->id,
'Customer Name' => $customer->full_name,
];
$lists[] = $list;
}
$collection = $this->paginate($lists, $perPage = 5, $page = null, $options = []);
return new mainCollection($collection);
}
and here is my route
Route::get('customers/list', 'customerController#list')
When I access http://127.0.0.1:8000/api/customers/list in PostMan I have error 404 Not Found
Another Route works well.
can someone help me?
Check php artisan route:list / double check your route info.
Also, check your controller class name and namespace.

joomla - router change url when getting the name of product

I have build my own component in joomla and client wants now a friendly urls f.e
website.com/someplace/{product-id}-{product-name}. So i Build my own router like this.
function componentBuildRoute(&$query)
{
$segments = [];
if (isset($query['view'])) {
$segments[] = "szkolenie";
unset($query['view']);
}
if (isset($query['product_id'])) {
$productName = JFilterOutput::stringURLSafe(strtolower(getProductName($query['product_id'])));
$newName = $query['product_id'] . '-' . $productName;
$segments[] = $newName;
unset($query['product_id']);
}
return $segments;
}
and parse route function
function componentParseRoute($segments)
{
$app = JFactory::getApplication();
$menu = $app->getMenu();
$item =& $menu->getActive();
$count = count($segments);
switch ($item->query['view']) {
case 'catalogue' : {
$view = 'training';
$id = $segments[1];
}
break;
}
$data = [
'view' => $view,
'product_id' => $id
];
return $data;
}
While on the end of buildroute function segments are ok I have exactly what I want that on the beginning of parse route I have something like
website.com/szkolenie/1-krakow <-- I dont know wtf is this krakow( I know it is city i Poland) but still where is it get from ? The getProductName function implementation is
function getProductName($productId)
{
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('#__component_training.id as id, #__component_product' . name)
->from($db->quoteName('#__component_training'))
->where('#__s4edu_product.product_id = ' . $productId)
->leftJoin('#__component_product ON
#__component_training.product_id=#__component_product.product_id');
$training = $db->loadObject();
return trim($training->name);
}
So taking all this into consideration I think that something is happening between the buildRoute and parseRoute, something what filters the $segment[1] variable, but how to disable that and why is it happening ?
P.S
Please do not send me to https://docs.joomla.org/Joomla_Routes_%26_SEF
I already know all the tutorials on joomla website which contains anything with sef.
P.S.S
It is built on joomla 3.7.0
You do not have a product named "krakow" ?
If not you can try to remove the $productName from the build function, just to check if this "krakow" is added automaticaly or it's from the getProductName() function.
Also i noticed that you have an error i guess in the function getProductName()
->where('#__s4edu_product.product_id = ' . $productId)
It's should be
->where('#__component_product.product_id = ' . $productId)

Codeigniter route page using value fetch from database

currently I have a page like the following -
abc.com/controller/action/23
Here 23 is Item id,which is dynamic. I have to fetch the name of the item using the id from database and route to a new url like following -
abc.com/controller/p/23-itemname.
How can I do that?
what you can do is when the user visits this page:
abc.com/controller/action/23
You put this code into the controller
CONTROLLER
function action()
{
$id = $this->uri->segment(3); //id is now 23, this is the third uri
$data['details'] = $this->YOUR_MODEL->get_details($id);
foreach($data['details'] as $d)
{
$itemname = $d->YOUR_TABLE_COLUMN_NAME;
}
redirect(base_url(). 'controller/p/' . $id . '-' . $itemname);
//line produces abc.com/controller/p/23-itemname
}
MODEL
function get_details($id)
{
$this->db->select('*');
$this->db->from('YOUR_TABLE_NAME');
$this->db->where('YOUR_ID_COLUMN_NAME',$id);
$query = $this->db->get();
return $query->result();
}
Just comment if you need more clarifications.
Goodluck meyt
EDIT
Since you have action and you redirect to function p, here is what will happen.
function p()
{
$explodethis = $this->uri->segment(3); //Contains "23-itemname"
$pieces = explode("-", $explodethis); //Separate by the dash
$id = $pieces[0]; //Contains "23"
$itemname = $pieces[1]; //Contains "itemname"
$data['details'] = $this->YOUR_MODEL->YOUR_FUNCTION(YOUR_PARAMETERS)
$this->load->view('YOUR_VIEW',$data);
}
Routing not required to do this. You may generate link by item name
<a href="<?php echo site_url('controller/action').'/'.$val['item_id'].'-'.$val['item_name']; ?>"
and now in controller explode this title to get ID of the item and then execute your query
$title = explode('-','23-item-name');
$id = trim($title[0]);//this is your item ID

accessing result array in codeigniter

I have the following happening in my model:
public function load_user_menu($username)
{
$this->db->select('*');
$this->db->from('menu');
$this->db->where('username', $username);
$query = $this->db->get();
return $query->result();
}
and the following in my controller:
public function index()
{
/*If user previously logged in and not logged out, username remains in session.
load username and load profile data.
*/
//check if user logged in or not
if(($this->session->userdata('username')!=""))
{
//load data from model
$profile = array();
$username = $this->session->userdata('username');
$result = $this->profileModel->user_profile($username);
foreach($result as &$value)
{
$profile['userdetails'] = $value;
}
$this->load->view('profile', $profile);
}else{
//redirect to login function
$this->login();
}
}
but I am getting errors on profile view. I am sure I am accessing them wrong because I am doing this on view:
<? echo $userdetails['profilepic']; ?>
and nothing is showing but this error:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: controllers/profile.php
Line Number: 60
which am certain because of the wrong accessing. How can I access the details based on the above?
assumming you have profilepic inside userdetails and seeing the error you are trying to get he property of nonobject that means
<? echo $userdetails['profilepic']; ?>
should be
<? echo $userdetails->profilepic; ?>
since you are taking you result as object and not array
return $query->result();
or you change this to
return $query->result_array();
$query->result(); produces an object; $query->result_array(); gives you an array
either is fine, depending on your needs
-There is no need of foreach loop in your controller as
$result = $this->profileModel->user_profile($username);
foreach($result as &$value)
{
$profile['userdetails'] = $value;
}
-Try this instead.
$profile['userdetails'] = $this->profileModel->user_profile($username);
-To Access in View,use this as
echo $userdetails->profilepic;
just try this on your view page
<? echo $profilepic; ?>

Resources