How to use Get function as object - php

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');
}
}

Related

Call to a member function get() on null - LARAVEL

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'));
}
}

How to pass variable from one method to another in Laravel

Please, I need help, I know that this is basic oop but I'm still learning...
public function confirm()
{
$q = Input::get('track');
$confirm = Tracking::where(function ($query) use ($q) {
$query->where('tracking_no', 'like', $q );
})->first();
if ($confirm) {
return Redirect::to('progress');
} else if(!$confirm){
return Redirect::to('invalid-tracking-number');
}
}
public function invalid($q)
{
$data = $this->confirm($q);
return view('frontend.invalid');
}
I'm to pass the input field from confirm() to invalid() but is not working... I need the $q inside my invalid()
so that I pass it to view...
I have fixed it....I reference another method using
if($confirm->isNotEmpty())
{
return (a valid page)
} else {
return $this->invalid();
}
with is, I'm able to return all the input data to the view of invalid

how to send a receive array value to another model in codeigniter

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));
}

Laravel - Trying to get property of non-object when logging in twice

Not sure what's happening here.
If have this as my login function in one of my controllers
protected $user;
public function __construct()
{
$this->user = Auth::user();
}
public function postLogin()
{
if( Auth::attempt([
'username'=>Input::get('username'),
'password'=>Input::get('password')
]))
{
return $this->user->username;
}
else
{
return Redirect::to('login');
}
}
and this as my logout
public function getLogout()
{
Auth::logout();
return Redirect::to('login');
}
If I sign in once then it displays the username , just like I want.
If I logout and log in again, the I get a
ErrorException (E_UNKNOWN)
Trying to get property of non-object
Then if I logout and re-login it works once more until I logout again.
Any ideas as to what is going on here?
I think the problem is both in:
$this->user = Auth::user();
and
return $this->user->username;
In constructor you should rather do something like this:
if (Auth::check()) {
$this->user = Auth::user();
}
but when logging in you shouldn't use:
return $this->user->username;
but
return Auth::user()->username;

A user is able to login into my site despite not having existed as a user in my membership table - why is this?

If a user does not exist its supposed to throw back an error on the login screen but the user is then brought to the home page as if they have previously registered in my sign controller. The login controller - validate credentials function - and my model - function can_log_in() - will illustrate the error its supposed to bounce back
model:
class Membership extends CI_Model
{
function Membership()
{
parent::__construct();
}
public function can_log_in()
{
$this->db->select('*')->from('membership');
$this->db->where('username', $this->input->post ('username'));
$this->db->where('password', md5($this->input->post ('password')));
$query = $this->db->get('membership');
if ($query->num_rows() == 1)
{
return true;
}
else{
return false;
}
}
login controller:
class Login extends CI_Controller
{
function Login()
{
parent::__construct();
$this->load->model('membership');
}
function loguserin()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]|trim');
$this->form_validation->set_rules('password', 'Password', 'required|md5|trim');
$username = $this->input->post('username');
$password = $this->input->post('password');
if ($this->form_validation->run()==TRUE)
{
$this->load->model('membership');
if($this->membership->can_log_in($this->input->post('username'),$this->input->post('password'))
{ // this is unepected despite a new if statement called
$this->session->set_userdata('status', 'OK');
$this->session->set_userdata('username', $username);
redirect('home');
} else //this else is unexpected
{
$this->session->set_userdata('status', 'NOT_OK');
$this->session->set_flashdata('Incorrect username/password');
$this->index();
}
} else {
$this->index;
}
}
function logout()
{
$this->session->sess_destroy();
redirect ('start');
}
function index()
{
$this->load->view('shared/header');
$this->load->view('account/logintitle');
$this->load->view('account/loginview');
$this->load->view('shared/footer');
}
}
Once again thanks :) its just that the user if not already in the table should not be able to sign into the home page
Your logic is all over the place and you're misunderstanding the concept of form validation. Form validation validates form input that's it, it does not validate credentials. The validate_credentials function you have is sort of pointless to be honest since you can't call the form validation error from outside the run function. So you may as well just make that go away entirely.
First, the initial call after form_validation should be like this:
if ($this->form_validation->run()==TRUE)
{
$this->load->model('membership');
if($this->membership->can_log_in($this->input->post('username'),$this->input->post('password')))
{
$this->session->set_userdata('status', 'OK');
$this->session->set_userdata('username', $username);
redirect('home');
} else {
$this->session->set_userdata('status', 'NOT_OK');// you'll need to do something
// here to tell them their credentials are wrong as I said this won't work through
// the form validation, perhaps look into using $this->session->set_flashdata()
$this->index();
}
} else {
$this->index; //reload the index to display the validation errors
Okay that takes care of sending the data to the login function, now the function actually needs to receive data, and since it is NOT the first page sent the data the post string will be empty, that is why I passed the data in the function above. The only part of the model that should need to change is the following:
public function can_log_in($username,$password)
{
$this->db->select('*')->from('membership');
$this->db->where('username', $username);
$this->db->where('password', md5($password));
try replacing
if ($this->form_validation->run())
with
if ($this->form_validation->run() && validate_credentials())

Categories