Laravel Function Not Updating Database - laravel-5

I am calling the function below
function makeanote($type,$id){
$note = Notification::where("user_id",Auth::user()->id)->get();
if ($type == "user"){
if($id > $note->pluck("users") ){
$note->users = $id;
$note->save();
return;
}
}
return;
}
Like so: makeanote($type,$id). The calling $type is "user" and the calling $id is "31".
In the database for my current user, the $note value at the users column is currently zero (0).
Therefore I would expect it to update to 31, but it is staying at zero. Am I using pluck() incorrectly?
Thank you.

Give this a shot:
function makeanote($type,$id){
$note = Notification::where("user_id",Auth::user()->id)->first();
if ($type == "user"){
if($id > $note->users){
$note->users = $id;
$note->save();
return;
}
}
return;
}
My changes are: ->first() (this will return a single object) instead of ->get() (this returns a object collection) and once you have the object you can access the users attribute directly.

Related

Make authorization Laravel

Im got error in section elseif, i want make HOD looking only at its own department data
public function index(User $user)
{
if(auth()->user()->role == 'Admin')
{
$form = Form::all();
}
elseif(auth()->user()->positions == 'HOD')
{
$form = Form::all()->department('user_id', \Auth::user()->id)->get();
}
else
{
$form = Form::where('user_id', \Auth::user()->id)->get();
}
return view('form.index', ['list_form' => $form]);
}
what should i change in elseif code ?
Try to do a dd() on auth()->user()->positions if it returns nothing, the relation between User model and Positions doesnt exist, or is set up wrong.

How to check if user email is null?

I'm trying to authenticate 2 users by inheritance in a Laravel project.
In my migration I have only 1 column that can be null, that column is email.
With that column I'm expecting to double authenticate professors and alumns, I have also 2 types of registers, one has the input email and the other not.
In my database I have 2 users, one is professor, and the other alumn, professor has email, and the other has email also, because they belong to the same table but that email is NULL in alumn row.
I'm trying to check when I login if that user with email column is null, my view returns alumn.
If it's not null my view returns professor.
I tried to check if email is null in my Laravel controller.
This is my code
public function index()
{
$user = User::where('email', '=', Input::get('email'))->first();
if ($user == 'NULL'){
return ('alumn');
}
if ($user != 'null'){
return ('professor');
}
}
And my Laravel router looks like this.
Route::get('/home', 'HomeController#index')->name('home');
I also tried this function in my controller instead the other one.
if (User::where('email', '=', Input::get('email'))->count() > 0) {
// user found
}
And with exists() instead of count().
If you are wondering, I'm returning just a string right now for testing purposes.
The issue you are having is within your conditionals. ($user == 'NULL') and ($user != 'null'). What you are checking for currently if the User object is the follow string: "NULL".
These are not how you check for null. There are many options that will work.
if (empty($user)){
return view('alumn');
}
// OR
if (!$user) {
return view('alumn');
}
// OR
if (is_null($user)) {
return view('alumn');
}
Would work. You could also use is_null. If you wanted to check that user equals null you cannot put quotation marks around null.
The first() method will return null if there's no data so use is_null() instead like :
if ( is_null($user) ) {
return view('alumn');
}else{
return view('professor');
}
FYI, first() will return you null when there is no data in the database, so I hope this will help you
public function index()
{
$user = User::where('email', '=', Input::get('email'))->first();
if ( is_null($user) ) {
return view('alumn');
} else {
return view('professor');
}
}
All the answers above didn't work in my case.
So i did this to make it worth.
public function index()
{
$user = Auth::user();
if ($user->email == ''){
return ('alumne');
}
else{
return ('professor');
}
print_r(Auth::user());
}
First i printed my Auth::user to check if all was working right, then i tried to save the authentification in a variable called user.
Then i checked with a conditional and all worked fine.
public function index() {
$user = User::where('email', '=', Input::get('email'))->first();
if (empty($user)) {
return view('alumn');
} else {
return view('professor');
}
}

Laravel 5.5 using Scope in model returns error for undefined method

When I try to use scope in this situation, returns me this error:
Call to undefined method Illuminate\Database\Query\Builder::isPromotionTypeIdScope() (View: C:\MAMP\htdocs\mysite\resources\views\site\home.blade.php)
Logic is:
If I replace isPromotionTypeIdScope() with all of the clauses (from the scope), works, but if I use scope gives me error, any suggestions?
Something about the structure is not working. I'm using scopes in another models and have no issues with them. Cannot find what's wrong.
is it possible to be, because I'm trying to add scope (Example: ->promotion()->isPromotionTypeIdScope($promotion_type_id))?
public function product()
{
return $this->belongsTo('App\Models\Product', 'product_id');
}
public function promotion(){
return $this->belongsToMany('App\Models\Promotion', 'promotion_product_prices', 'product_price_id', 'promotion_id');
}
public function single_promotion($promotion_type_id = 0){
return $this->promotion()->isPromotionTypeIdScope($promotion_type_id)->first() ?? false;
}
public function category_promotion($promotion_type_id = 0){
return $this->product()->first()
->category()
->first()
->promotion()
->isPromotionTypeIdScope($promotion_type_id)
->first() ?? false;
}
public function full_promotion($promotion_type_id = 0)
{
return Promotion::where('full', 1)->isPromotionTypeIdScope($promotion_type_id)->first() ?? false;
}
public function hasPromotion($promotion_type_id = 0){
if($this->full_promotion($promotion_type_id) !== false){
return $this->full_promotion($promotion_type_id);
}elseif($this->category_promotion($promotion_type_id) !== false){
return $this->category_promotion($promotion_type_id);
}elseif($this->single_promotion($promotion_type_id) !== false){
return $this->single_promotion($promotion_type_id);
}else{
return false;
}
}
public function scopeIsPromotionTypeIdScope($query, $promotion_type_id=0){
if($promotion_type_id != 0){
return $query->where('promotion_type_id', $promotion_type_id)
->where('validity_from', '<=', date('Y-m-d H:i:s'))
->where('validity_to', '>=', date('Y-m-d H:i:s'))
->where('active', 1)
->orderBy('updated_at', 'DESC')->limit(1);
}else{
return $query;
}
}
Everywhere you call your isPromotionTypeIdScope method on Promotion model so you should define scopeIsPromotionTypeIdScope in Promotion model instead of this one.

Laravel 4 hasMany with WHERE clause

Not sure if this is the correct way to add an additional query to the hasMany argument but was unsuccessful. Is this possible?
public function menuItems($parent=false){
if($parent){
$menuItems = $this->hasMany('MenuItem')->where('parent',$parent);
}else{
$menuItems = $this->hasMany('MenuItem');
}
return $menuItems;
}
When called using
$menu_items = $menu->menuItems(0);
This just seems to return an empty array when passed a parent. Even though data with MenuItem->parent = 0 exists
Do I need to some way distinguish I'm asking for my linked items "parent" and not the main models "parent"
public function menuItems(){
return $this->hasMany('MenuItem');
}
Called with
$menu_items = $menu->menuItems()->where('parent', 0)->get();
I am not sure about the query part but at first wouldn't passing a 0 to the function still register the $parent variable as false? So maybe just check if the $parent is not null.
public function menuItems($parent = null){
if(!$parent == null)){
$menuItems = $this->hasMany('MenuItem')->where('parent',$parent);
}else{
$menuItems = $this->hasMany('MenuItem');
}
return $menuItems;
}
In PHP 0 = FALSE, change this
if( $parent ){
for this
if( $parent !== false ){

codeigniter call to a member function row() on a non-object

I've searched the ends of the earth and back for this solution, and for everyone else, the fix was to autoload the database class in autoload.php. However, I am already doing this, so I don't know where else to go with it.
Controller:
class Login extends CI_Controller{
function validate_credentials(){
$this->load->model('membership_model');
// call the member function from membership_model.php
$q = $this->membership_model->validate();
if($q){
// user info is valid
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('site/members_area');
}else{
$this->index();
}
}
}
Model:
class Membership_model extends CI_Model{
function validate(){
// this method is called from the login.php controller
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', md5($this->input->post('password')));
$q = $this->db->get('members');
if($q->num_rows == 1){
return true;
}
return false;
}
}
Any help on this please, I'm going round in circles with this
My next thought is that php.ini needs configuring on my host server, to load the mysql modules - but I would very surprised by this?
Just to clarify - the line:
if($q->num_rows)
Is the problem, saying that I am trying to reference a property of a non object
Actually
if($q->num_rows)
should be
if($q->num_rows()) // It returns the number of rows returned by the query.
So you can use
if($q->num_rows()) // In this case it'll be evaluated true if rows grater than 0
{
// code when rows returned
}
num_rows is a function.
if ($query->num_rows() > 0) {
}
So you need
if($q->num_rows() > 0) {
return true;
}
Solved - I hadn't assigned a User to the database, therefore couldn't access it.
Relief

Resources