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'));
}
}
Related
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'));
}
I have a function in my User model which is called isAdmin, if "Admin" is set to 1 in the database, it returns true, if not it returns false.
How is this gonna work with Auth::user()?
When I do Auth::user()->isAdmin(), it returns "Property [admin] does not exist on this collection instance."
Thats why I came to the conclusion it may not use the User model?
User model
public function isAdmin() {
if($this->admin == 1) {
return true;
} else {
return false;
}
}
public function view ()
{
if(Auth::check() && Auth::user()->isAdmin()) {
$user = User::all();
$post = Post::all();
$visit = Visits::all();
return view('admin')->with('post', $post)->with('user', $user)->with('visit', $visit);
} else {
return redirect()->to('/');
}
}
If I may suggest, for this use case, you can actually make do without an extra function. You could just say auth()->user()->admin, specially if the 'admin' column in the database is boolean type.
Otherwise (even admin coloumn is not boolean type) you can set up a mutator method in the model, like so:
public function getIsAdminAttribute()
{
return (bool) $this->admin;
}
Then to check you can access it like so: Auth::user()->isAdmin or auth()->user()->isAdmin
And better yet, you might want to read about Gate and Policies to achieve more robust access controlling. https://laravel.com/docs/5.7/authorization
Suggestion, change the code to just this:
public function isAdmin() {
return $this->admin;
}
This code does exactly the same as you've got above..
Now in your admin.blade.php you are using:
$user->isAdmin();
But in the controller you have:
$user = User::all();
which returns collection.
You should iterate over it, and check on each user instance if it is an admin:
$users = User::all();
In the view:
#foreach($users as $user)
#if($user->isAdmin())
{{ $user->name }} // some code here..
#endif
#endforeach
No Need To do anything just check if login then auth()->check() is return true then auth()->user() return the user
public function view ()
{
if(auth()->check() && auth()->user()->isAdmin()) {
$user = User::all();
$post = Post::all();
$visit = Visits::all();
return view('admin')->with('post', $post)->with('user', $user)->with('visit', $visit);
} else {
return redirect()->to('/');
}
}
public function isAdmin()
{
return $this->admin;
}
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'm trying to create a callback to return my views based on data from my current logged-in user. If I do something basic like echoing 'hi' it works, is there any way accomplish this?
function checkUser($type,$callback){
if( is_callable($callback) ){
call_user_func($callback);
}
}
class FichaController extends Controller
{
public function contarFichas()
{
checkUser('particular',function(){
$currentUser = Auth::user();
$countFichas = Ficha::where('user_id',$currentUser->id)->count();
return view('particular.index', array('countFichas' => $countFichas));
});
}
}
Return the result from checkUser
if( is_callable($callback) ){
return $callback();
}
public function contarFichas()
{
return checkUser('particular',function(){
$currentUser = Auth::user();
$countFichas = Ficha::where('user_id',$currentUser->id)->count();
return view('particular.index', array('countFichas' => $countFichas));
});
}
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));
}