1

I am working on a Laravel project in which I need to write a custom function, but when I call this function Laravel says:

Laravel : htmlentities() expects parameter 1 to be string, array given

Here is my function:

public static function get_checkup_time(){
    $user_logged_in = Auth::user()->foreignkey_id; 
    $result = DB::table('doctors')
               ->select('checkuptime')
               ->where(['id'=>$user_logged_in])
               ->get();
    return $result;
}

And this is my view in which I am trying to invoke this function .

@if(Auth::user()->userrolebit ==2)
    {{
        DateTimeFormat::get_checkup_time()
    }}
 @endif

i don't know what I am doing wrong here.

1
  • You cannot just "print" arrays. Well, you can, but the result would certainly be meaningless. I'm pretty sure you want to extract checkuptime fromt he array. Commented Nov 10, 2016 at 13:16

3 Answers 3

1

where() expects string as first parameter, so change this:

->where(['id'=>$user_logged_in])

to this:

->where('id', $user_logged_in)
Sign up to request clarification or add additional context in comments.

Comments

0

If you are trying to return just checkup time you should do this in your method

public static function get_checkup_time()
{
    $user_logged_in = Auth::user()->foreignkey_id; 

    $result = DB::table('doctors')
               ->select('checkuptime')
               ->where('id', $user_logged_in)
               ->first();

    return $result->checkuptime;
}

Comments

-1

Edit:

Following a downvote, I've seen that mine wouldn't work as well because the ->get() call should be replaced with ->first() as in @Paul's answer. See my comment below.


The $result you are returning from the method is not a string.

Therefore {{ DateTimeFormat::get_checkup_time() }} is the one returning the error.

Returning something like $result->checkuptime should do it.

1 Comment

@Paul's answer is correct, mine would not work. Calling ->first() instead of ->get() is the key before returning a proper attribute. Good thing he beat me to posting an answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.