public function user_delete($email){
//echo $email;
$this->load->model('hradvalid');
$user_id = $this->hradvalid->user_id($email);
$user_email=$user_id[0]->email;
$this->hradvalid->user_delete($email,$user_email);
if($this->hradvalid->user_delete()){
$this->session->set_flashdata("alert","your data deleted successfully");
}else{
$this->session->set_flashdata("alert","error comming sorry!");
}
return redirect('admin/dashboard');
}
model code
public function user_id($value)
{
$q = $this->db->select('email')
->from('user')
->where('id',$value)
->get();
return $q->result();
}
public function user_delete($value,$email)
{
$user_delete=$this->db->delete('user',array('id'=>$value));
$user_delete=$this->db->delete('acuser',array('username'=>$email));
return $user_delete;
}
above code is my controller I receive a value from hradvaild model and method user_id getting one value this is working now I want to send this value to hradvaild model in user_delete method here I am getting an error offset[0] error please help me and also $user_email undefined
Try this
In Controller
public function user_delete($email)
{
$this->load->model('hradvalid');
$user_id = $this->hradvalid->user_id($email);
$user_email = $user_id[0]['email'];
if(!$this->hradvalid->user_delete($email,$user_email)){ # Changed
$this->session->set_flashdata("alert","error comming sorry!"); # Changed
}else{
$this->session->set_flashdata("alert","your data deleted successfully"); # Changed
}
return redirect('admin/dashboard');
}
In Model
public function user_id($value)
{
$query = $this->db->select('email')
->from('user')
->where('id',$value)
->get();
return $query->result_array(); # Change this
}
public function user_delete($value,$email)
{
$user_delete=$this->db->delete('user',array('id'=>$value));
$user_delete=$this->db->delete('acuser',array('username'=>$email));
}
Related
Seeking for your help again. Just wondering for any idea's to return something if the column is NULL. Please see below codes: Thank you!
Application Controller
public function index()
{
$user_id = Auth::user()->id;
$applications = application::where('user_id', '=', $user_id)->first()->get();
if (empty($applications)) {
return redirect(route('user.application.index'))->with('message','Application is Succesfully');;
}
else{
return view('user.application.index',compact('applications'));
}
}
If user is not authenticated then you will get unexpected error. So first check the auth as Auth::check()
Try this :
use Illuminate\Support\Facades\Auth;
public function index()
{
if(!Auth::check()) {
return view('login');
}
$applications = Application::where('user_id', $Auth::user()->id)->first();
if (empty($applications)) {
return redirect(route('user.application.index'))->with('message','Application is Succesfully');;
}
else{
return view('user.application.index',compact('applications'));
}
}
I'm having a controller with some functions. In every function I get user data by sharing it from the Contoller.php
In controller.php
public function share_user_data() {
$user = Auth::user();
$this->checkValidation($user);
$this->user = $user;
View::share('user', $user);
}
public function checkValidation($user){
if($user->email_activated == 0){
var_dump($user->email_activated); // I get: int(0)
return redirect('/verifyEmail');
}
}
In the other controller
public function viewCategory(Category $category){
$this->share_user_data(); // here's the check
$this->get_site_lang();
$products = $category->products;
return view('category', compact('category','products'));
}
But I get the category view and not redirected to the verifyEmail route. How to fix this and why it's happening?
The controller function called by the route is the one responsible for the response. I guess it is viewCategory() in your example?
Your viewCategory() function is always returning view(). It must return redirect() instead. I think the main function should be responsible for picking the output of the request.
private function checkValidation($user) {
return $user->email_activated == 0;
}
public function viewCategory(Category $category) {
$user = Auth::user();
/* ... call to share_user_data() or whatever ... */
if ($this->checkValidation($user)) {
return redirect('/verifyEmail');
}
return view('category', compact('category','products'));
}
Im making a login page , so i need to compare the passoword form DB and the on form form , whats wrong with my code , it says Trying to get property of non-object
Using laravel
public function index()
{
// $mahasiswa = DB::table('mahasiswa')-> get();
return view('index');
}
public function cek(Request $request)
{
$user = DB::table('mahasiswa')->where('npm', $request->npm)->first();
if ($user->Password == $request->password) {
redirect('welcome');
} else {
return ('eror');
}
}
I expect its all right to write like that
you can easily use
Auth::attempt
to check user credentials and this is the correct way to do what you want
or
use this code
if (Hash::check($request->password, $user->password)) {
// true...
}
and the error Trying to get property of non-object
as you write in your code
if($user->Password == $request->password){
redirect('welcome');
}
the problem with Password you write p as a capital not small
I hope my answer help you!
try this, if the error persist, check your database password field if its getting the field. $user->password;
public function index()
{
// $mahasiswa = DB::table('mahasiswa')-> get();
return view('index');
}
public function cek(Request $request)
{
$user = DB::table('mahasiswa')->where('npm', $request->input('npm'))->first();
if ($user->Password == $request->input('password')) {
redirect('welcome');
} else {
return ('erorr');
}
}
I am new to codeigniter I Have create a login form and i am able to login succesfully fine and after login i am getting the login username also fine.but when i try to get the userid it is showing empty.I am displaying userid just like username displaying .But it is displaying empty why.Please any help would be appreciated thanks in Advance.
This is my model
class Login_Model extends CI_Model{
public function checklogin($uname,$pass){
$this->db->select('id','username','password');
$this->db->get('login');
$this->db->where('username',$uname);
$this->db->where('password',$pass);
$query=$this->db->get('login');
if($query->num_rows()==1){
return true;
}else{
return false;
}
}
This is mycontroller
public function verifyuser(){
$name=$this->input->post('username');
$password=$this->input->post('password');
$this->load->model('Login_Model');
if($this->Login_Model->checklogin($name,$password)){
$this->Login_Model->checklogin($name,$password);
$this->session->set_userdata('username', $name);
$this->session->set_userdata('id', $id);
return true;
}else{
$this->form_validation->set_message('verifyuser','username or password is invalid');
return false;
}
}
This view
$id= $this->session->userdata('id');
echo "login id is".$id;
in View page i am accessing data like this but it giving empty for id but it is username fine.
It looks like you are just checking if the username and password match in the database in the checklogin function.
You are not returning the id and storing it in $id variable.
Hence since $id is undefined, no value is printed.
Changes required
Model
Fetch the id from the database and return it instead of a boolean.
class Login_Model extends CI_Model{
public function checklogin($uname,$pass){
$this->db->select('id','username','password');
$this->db->get('login');
$this->db->where('username',$uname);
$this->db->where('password',$pass);
$query=$this->db->get('login');
if($query->num_rows()==1){
$res = $query->row_array();
return $res['id'];
} else {
return false;
}
}
}
Controller
Fetch the id and use it to set the userdata.
public function verifyuser(){
$name=$this->input->post('username');
$password=$this->input->post('password');
$this->load->model('Login_Model');
$id = $this->Login_Model->checklogin($name,$password);
if($id){
$this->session->set_userdata('username', $name);
$this->session->set_userdata('id', $id);
return true;
} else {
$this->form_validation->set_message('verifyuser','username or password is invalid');
return false;
}
}
Your are getting userid but not storing it in session so you will not get this id
make model code like this
public function checklogin($uname,$password)
{
$this->db->select('user_id','user_email','user_password');
$this->db->from('users');
$this->db->where('user_email',$uname);
$this->db->where('user_password',$password);
$res = $this->db->get()->result_array();
if(!empty($res))
{
return $res[0]['user_id'];
}
else
{
return 0;
}
}
and controller code like this so you will get id
$name=$this->input->post('username');
$password=$this->input->post('password');
$user_id=$this->Login_model->checklogin($name,$password);
if($user_id > 0)
{
$this->session->set_userdata('username', $name);
$this->session->set_userdata('id', $user_id);
return true;
}
else
{
$this->form_validation->set_message('verifyuser','username or password is invalid');
return false;
}
You can make the following Changes in your Model.
class Login_Model extends CI_Model{
public function checklogin($uname,$pass){
$query = $this->db->select('id','username','password');
->where(['username'=>$uname,'password'=>$pass]);
->get('login');
return $query->row()->id;
}
I am struggling with flash data in codeigniter. My edit function is working properly. But problem is with delete function.My controller is as follows
public function rmRole()
{
$data = array();
$roleId = $this->uri->segment(4,0);
if ($this->_delete($roleId)) {
$this->session->set_flashdata('flash_message' ,'data_deleted');
redirect('adminroles');
}
}
My model includes
public function _delete($id) {
$table = $this->get_table();
$this->db->where('id', $id);
$this->db->delete($table);
}
My data is got deleted. But it is redirecting to a blank page with the url. What shall i do to display a flash data like data deleted successfully and redirect to adminroles module?
try this in model
function delete($id) {
if ($id)
{
if ($this->db->where("id", $id)->delete("table name")){
return TRUE;
}
else{
return FALSE;
}
}else{
return FALSE;
}
}