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

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.

where() expects string as first parameter, so change this:
->where(['id'=>$user_logged_in])
to this:
->where('id', $user_logged_in)

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

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.

Related

Laravel withQueryString does not exist after calling paginate method on query builder

Method Illuminate\Database\Eloquent\Collection::withQueryString does
not exist.
when i write this code it gives this error
blade;
<div class="float-right">{{ $modeller->withQueryString()->links()}}</div>
controller;
public function index(){
$modeller = Modeller::query();
$koleksiyonlar = Koleksiyon::all();
$modelistler = Modelist::all();
$uretim_sorumlulari = Uretim_sorumlusu::all();
if(request('model_kodu')){
$modeller = $modeller->where('ModelKodu', 'LIKE', "%".request('model_kodu')."%");
}
if(request('koleksiyon_id')){
$modeller = $modeller->where('koleksiyon_id', 'LIKE', "%".request('koleksiyon_id')."%");
}
if(request('uretim_sorumlusu_id')){
$modeller = $modeller->where('UretimSor', 'LIKE', "%".request('uretim_sorumlusu_id')."%");
}if(request('modelist_id')){
$modeller = $modeller->where('modelist_id', 'LIKE', "%".request('modelist_id')."%");
}
$modeller = $modeller->paginate(18);
return view('kumas.index',compact('modeller','koleksiyonlar','modelistler','uretim_sorumlulari'));
}
The paginate method, runs an implicit get on your query result.
try to use withQueryString instead of paginate.
example:
$modeller->withQueryString()->paginate(18);
but from your use case I suggest to use this in your view, instead of query string, this will add everything came from query string to your links:
{{ $users->appends($_GET)->links() }}
More Details on the pagination & Query String params
the page, offset, ... and everything paginate needs, would append automatically to paginate function without any effort.
you only need to explicitly ->appends($_GET) when you have some filtering parameters in your $_GET and want to preserve them in the following requests, when user clicks the next page or previous page
to expand on #Abilogos answer, I think it is better to use Request::except('page') method, which return an array of query parameter except the page parameter
in your blade view:
{{ $users->appends(\Request::except('page')->links() }}

I need a help to understand WHEN clause

i'm working on a small project using Laravel and i do my best to understand the Laravel documentation to create a nice solutions, but this time i found some difficult to understand the WHEN clause
can someone explain to me this code by giving me an example:
$role = $request->input('role');
$users = DB::table('users')
->when($role, function ($query, $role) {
return $query->where('role_id', $role);
})
->get();
Docs : https://laravel.com/docs/7.x/queries#conditional-clauses
Also i would like to know what stand for the second parameter [] in the input function
request()->input('field', []);
As long as the variable role equates to true, this query will execute:
return $query->where('role_id', $role);
In terms of your second question, if request parameter 'field' is not filled the default value set will be: [].
Another Example:
$foo = $request->input('test', 69);
If the request parameter test isn't filled, the parameter foo will be 69.

Laravel - htmlspecialchars() expects parameter 1 to be string, object given

I go this error:
htmlspecialchars() expects parameter 1 to be string, object given
I'm using in controller:
$data = '{"pr":{"code":"1"},"ac":[[{"icon":"web","action":"link","url":"asd"}]]}'
$newData = json_decode($data);
And i send it to the view as array: 'data' => $newData
And when i try to use $data into the view, it give me that error
Tried already to use $data->ac OR $data['ac'] but still the same...
Some help, please?
When you use a blade echo {{ $data }} it will automatically escape the output. It can only escape strings. In your data $data->ac is an array and $data is an object, neither of which can be echoed as is. You need to be more specific of how the data should be outputted. What exactly that looks like entirely depends on what you're trying to accomplish. For example to display the link you would need to do {{ $data->ac[0][0]['url'] }} (not sure why you have two nested arrays but I'm just following your data structure).
#foreach($data->ac['0'] as $link)
This is a link
#endforeach
if you real intention is to send the full array from the html to the controller, you can use the following code:
from the blade.php:
<input type="hidden" name="quotation" value="{{ json_encode($quotation,TRUE)}}">
in controller
public function Get(Request $req) {
$quotation = array('quotation' => json_decode($req->quotation));
//or
return view('quotation')->with('quotation',json_decode($req->quotation))
}
You could use serialize
<input type="hidden" name="quotation[]" value="{{serialize($quotation)}}">
But best way in this case use the json_encode method in your blade and json_decode in controller.
This is the proper way to access data in laravel :
#foreach($data-> ac as $link)
{{$link->url}}
#endforeach
Try use in your collection the json_encode().
https://www.php.net/manual/pt_BR/function.json-encode.php
public static function getTeamMemberInto($user_id){
$team = DB::table('members_team')
->join('teams', 'teams.id', '=', 'members_team.teams_id')
->join('employees', 'employees.id', '=', 'teams.leader_employees_id')
->where('members_team.employees_id', '=', $user_id)
->select('*')->first();
// dd($team);
if($team){
return json_encode($team, true);
}else{
return false;
}
}

Laravel htmlentities() expects parameter 1 to be string

I am trying to fetch data from a table by using a custom class .But it says htmlentities() expects parameter 1 to be string.
This is My DateTimeFormat Class .Here vitals is a table.which have vita_name attribute.
public static function get_vital_details($vital_id)
{
$result = DB::table('vitals')
->select('vita_name')
->where(['id' => $vital_id])
->get();
return $result;
}
This is the view i am trying to access the data .
<?php $vitalsinfo=DateTimeFormat::get_vital_details($vitaldetails->vital_id) ?>
#foreach($vitalsinfo as $vitalsinfo)
{{$vitalsinfo}}
#endforeach
i Am new to laravel .Any suggestions would be more than welcome. Thank You
You're trying to dipslay object as string, so try to use first() instead of get() to get just one object instead of collection:
$result = DB::table('vitals')
->select('vita_name')
->where(['id' => $vital_id])
->first();
And do just this (instead of #foreach construction) to display property of an object:
{{ $vitalsinfo->vita_name }}

Laravel fetch data from model where the condtions is from another model

I have a table called List which i planned to be displayed into view with this command : $lists= List::with('user', 'product.photodb', 'tagCloud.tagDetail')->get();. But, i want the data displayed is only those that has TagID equal to the one user inputted. Those data can be retrieved from TagCloud table.
What i am currently doing is :
$clouds = TagCloud::select('contentID')
->where('tagDetailID', '=', $tagID)
->get();
$lists = List::with('user', 'product.photodb', 'tagCloud.tagDetail')
->where('id', '=', $clouds->contentID)
->get();
But when i tried to run it, it only return a null value, even though when i am doing return $clouds, it does returned the desired ID.
Where did i do wrong ? Any help is appreciated !
A couple of gotchas with your current solution.
Using get() returns an Illuminate\Database\Eloquent\Collection object. Hence you can't use $clouds->contentID directly since $clouds is a collection (or array if you prefer). See Collection Documentation.
where(...) expects the third parameter to be a string or integer, aka single value. Instead, you are passing a collection, which won't work.
The correct way is to use whereHas() which allows you to filter through an eager loaded relationship.
Final Code:
$lists = List::with('user', 'product.photodb', 'tagCloud.tagDetail')
->whereHas('tagCloud',function($query) use ($tagID) {
return $query->where('contentID','=',$tagID);
})
->get();
See WhereHas Documentation.
What you want is whereHas()
$list = List::with(...)
->whereHas('relation', function($q) use($id) {
return $q->where('id', $id);
})->get();
Apply Where condition in you tagCloud model method tagDetail
public function tagDetail(){
return $q->where('id', $id);
}

Categories