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 }}
Related
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;
}
}
Every time I try to setup my pagination:
Missing argument 1 for Illuminate\Support\Collection::get()
My controller:
public function index()
{
$products = DB::table('products')
->where('product_group_id', '=', 1)
->paginate(15)
->get();
return view('product.index', [
'products' => $products,
]);
}
My view:
{{ $products->links() }}
What goes wrong here?
You don't need ->get() here. ->paginate() gets your records from the database and returns a collection of those 15 items.
When you're running ->get() here you're attempting to run it on the collection that is returned which expects a $key and is used to get a specific item out of a collection.
You should do this:
$products = DB::table('products')
->where('product_group_id', '=', 1)
->paginate(15);
return view('product.index', [
'products' => $products,
]);
or with Eloquent
$product = Product::where('product_group_id', '=', 1)
->paginate(15);
return view('product.index', [
'products' => $products,
]);
Note: I have put the filters before the paginate() call to ensure that the where clause is part of the database query rather than trying to filter the resulting collection/paginator
When you call the paginate() method on a Illuminate\Database\Query\Builder object, You will get an instance of \Illuminate\Pagination\LengthAwarePaginator. That itself does not have a get()-Method, but the Illuminate\Pagination\AbstractPaginator it extends has a magic __call() function which forwards the call to the underlying Collection.
Now, Illuminate\Support\Collection does have a get() method, but it takes the key of the element you want to get out of the Collection as an argument, hence the error you get.
Now, I suppose what you actually want to achieve standard pagination with links for page numbers and "forward" and "back" buttons. If so, you should just stick to the documentation: Get the data just the way you did, just leave out the get(), then display it in a view the way its shown here.
EDIT: Just read about the Method links does not exist error you get. That is indeed strange. Builder::paginate() definitely returns an Instance of LengthAwarePaginator which itself definitely has a links() method.
Is there perhaps some more code that is relevant to this problem which you havent shown us yet? Maybe in your view?
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.
This is my Laravel controller code
public function switchInfo($prisw, $secsw){
$cur_sw_pair_id = DB::table('sw_pairs')
->select('sw_pair_id')
->where('pri_sw','=',$prisw)
->where('sec_sw','=',$secsw)
->get();
$infolist = DB::table('cust_sw_pair')
->select('interface','pri_sw_vlan','sec_sw_vlan','pri_sw_admin_status','sec_sw_admin_status','description')
->where('sw_pair_id', '=', $cur_sw_pair_id)
->get();
return view('switchinfo.switchinfoview',compact('infolist'));
}
when this code executing it gives Object of class stdClass could not be converted to string error. How can i solve this? How can i pass my infolist into switchinfoview view without getting this error?
You can use pluck() to get value of a column.
Your first query should like this:
$cur_sw_pair_id = DB::table('sw_pairs')
->where('pri_sw','=',$prisw)
->where('sec_sw','=',$secsw)
->pluck('sw_pairs');
Keep the second query as it is.
Hope it will help.
The issue is that ->get() returns a collection, which is a 0-indexed collection of results (rows) from the database. In your case, you want to be using the following query:
$cur_sw_pair_id = DB::table('sw_pairs')
->select('sw_pair_id')
->where('pri_sw','=',$prisw)
->where('sec_sw','=',$secsw)
->first();
And then accessing the property sw_pair_id of $cur_sw_pair_id, like so:
$infolist = DB::table('cust_sw_pair')
->select('interface','pri_sw_vlan','sec_sw_vlan','pri_sw_admin_status','sec_sw_admin_status','description')
->where('sw_pair_id', '=', $cur_sw_pair_id->sw_pair_id)
->first();
Then, in your view, you can access all the properties (anything in the ->select() statement) of $infolist using $infolist->PROPERTY_HERE
Note that using ->first() is essentially using the same as ->get();, but you have to access the results of ->get() using an index, so
$cur_sw_pair_id[0]->sw_pair_id
Hope that provides some insight for you.
i'm using laravel v 4.2..
i want to create update record.
can you help me.. what's wrong with this code...
this is my code :
MatakuliahsController.php
public function edit($id)
{
//$matakuliahs = $this->matakuliahs->find($id);
$matakuliahs = Matakuliah::where('id','=',$id)->get();
if(is_null($matakuliahs)){
return Redirect::route('matakuliahs.index');
}
return View::make('matakuliahs.edit',compact('matakuliahs'));
}
edit.blade.php
{{ Form::open(array('autocomplete' => 'off', 'method' => 'PATCH', 'route' => array('matakuliahs.update', $matakuliahs->id))) }}
...
{{ Form::close() }}
Error is :
Undefined property: Illuminate\Database\Eloquent\Collection::$id (View: C:\xampp\htdocs\Laravel 4\projectLaravel\app\views\matakuliahs\edit.blade.php)
thanks for your attention and your help..
What you are trying to get is a relationship on a collection of models, the relationship exists on the object in that collection. You can use first() to return the first one or you need to use loop for each one get their items
$matakuliahs = Matakuliah::where('id','=',$id)->get()->first();
$matakuliahs = Matakuliah::where('id','=',$id)->get();
return a colllection of object where the id is equal to $id. in this case will return a collection of 1 element and not the object itself of course if the id is unique, so when you do:
$matakuliahs->id
you are triying to acess the id properties of the $matakuliahs object but the $matakuliahs in this case is not a object is a collection.
to solve this problem you can do:
1.
$matakuliahs = Matakuliah::where('id','=',$id)->get()->first();
or
$matakuliahs = Matakuliah::where('id','=',$id)->first();
to get the object and acess the properties.
2. on you view:
#foreach( $matakuliahs as $matakuliah)
//your code here
#endforeach
hope this help. thanks
Try this in your controller method:
$matakuliahs = Matakuliah::find($id);
And just pass it to the view.
The method
MyModel::find($id);
Or relationship with Eloquent no works on Laravel 4.2 just next solution
$myModel= new MyModel;
$myModel->setConnection('mysql2');
$myModel= $myModel->where('id', $id)->first();