$task = Task::all();
$sort = $task->sortByDesc('id')->toArray();
return view('welcome')->with('sort',$sort);
I want to pass Eloquent collection object to an array and then sort to iterate through but I get this error:
htmlspecialchars() expects parameter 1 to be string, array given
I don't know what the error means?
Try this
// controller
$task = Task::orderBy('id','DESC')->get();
return view('welcome')->with('task',$task);
// view
#foreach($task as $t)
{{$t->colunm_name}}
#endforeach
Laravel get() returns an array of object
Related
I want to combine two data search results into one array, I use array_merge but there is an array_merge() error:
Argument # 1 is not an array
How to turn $vendor's eloquent results into an array and combine it with $plucked?
$vendor = Vendor::find($id);
$vendor_detail = VendorDetail::where('vendor_id',$id)->get();
$plucked = $vendor_detail->pluck('vendor_profile_value','vendor_profile_name');
$coba = array_merge($vendor,$plucked);
$plucked already an array
I think the problem here is that $vendor is not yet an array
You could do it like this:
$vendor = Vendor::find($id);
$vendor_details = VendorDetail
::select('vendor_profile_value', 'vendor_profile_name')
->where('vendor_id', $id)
->get()
->toArray();
$coba = array_merge($vendor,$vendor_details);
The get() method execute the query returning a Collection instance, in which you can call the toArray() method.
Side note
As far as I can see, you could make use of relationships and eager loading.
If you have a one-to-many relationship defined like this in your Vendor model:
public function details()
{
return $this->hasMany(VendorDetails::class);
}
Then, you could eager load the relationship like this:
$vendor = Vendor::with('details')->find($id);
// ^^^^^^^^^^^^^^
You could even just load the wanted fields:
$vendor = Vendor::with('details:vendor_profile_value,vendor_profile_name')
->find($id);
Then, your object will have a new attribute called "details" containing the related objects (or a collection of the limited selected fields).
You can convert the $vendor to an Array like below.
$vendor = Vendor::find($id)->toArray();
I'm trying to loop through an array for a query written in the blade, as follows :
#foreach({{\App\Post::where('qid',$updt_18_q->qid)->get()->body}} as $updt_18_a)
I'm receiving the following error :
syntax error, unexpected '<'
The query is working fine by using first() outside the #foreach loop.
What you are doing is so bad practice, write all your logic in Controller
{{}}
The above syntax is for echoing something so you are not looping an array but echoing it do it like this
In Laraval if you echo an array or object it automatically convert's it into json Object so you won't get array to string conversion error
#php $data = \App\Post::where('qid',$updt_18_q->qid)->get() #endphp
#foreach($data as $updt_18_a)
//code
#endforeach
get() method in Laravel returns an array of objects and first() method returns single object. That's why your query is working with first().
So you need to do this :
// This is your array of objects.
#php($posts = \App\Post::where('qid',$updt_18_q->qid)->get())
// Iterate through array
// $updt_18_a is your Post object
/* $updt_18_a \App\Post */
#foreach($posts as $updt_18_a)
$body = $updt_18_a->body; // Access like this.
// Code here
#endforeach
#foreach(\App\Post::where('qid',$updt_18_q->qid)->get()->body ?? [] as $updt_18_a)
//code
#endforeach
I have a method in my controller as follows:
function surcharge_load_data(){
$data = $this->Surcharge_model->surcharge_load_data();
foreach($data->result_array() as $r) {
echo $r->surcharge_id;
}
}
The Surcharge_model method returns a result array. This is my method in the model:
function surcharge_load_data(){
$this->db->order_by('surcharge_id', 'DESC');
$query = $this->db->get('surcharges');
return $query->result_array();
}
The data returned from the above method looks like this:
[{"surcharge_id":"37","surcharge_curr":"EUR","surcharge_name":"MNM","surcharge_unit_id":"3","surcharge_value":"43","surcharge_category_id":"3","created_timestamp":"2019-06-03 06:18:18","updated_timestamp":"2019-09-01 08:19:18"},
{"surcharge_id":"36","surcharge_curr":"EUR","surcharge_name":"TOY","surcharge_unit_id":"3","surcharge_value":"433","surcharge_category_id":"3","created_timestamp":"2019-07-09 09:21:21","updated_timestamp":"2019-09-10 09:21:21"}]
A few questions as follows:
(1) The entire data is encapsulated in []. Does that mean it has returned one large object with array of arrays?
(2) Why do I get an error, Call to a member function result_array() on array? I thought $data is a result array. How can I access it please?
Try this, no need to use result_array() here
function surcharge_load_data(){
$data = $this->Surcharge_model->surcharge_load_data();
foreach($data as $r) {//changes
echo $r['surcharge_id'];
}
}
HERE, you are using result_array() so you need to use each object like $r['surcharge_id'] if you use result() then you need to use $r->surcharge_id
From your controller you can't retrieve the result_array method because this only exists within your Model.
So, in your controller you can read the answer from your model like:
function surcharge_load_data(){
$data = $this->Surcharge_model->surcharge_load_data();
foreach($data as $r) {
//just here you can retrieve your data
...
}
}
Where $data is the result from the model (before returned as $query->result_array()) which your are recieving in your controller
I am trying to loop through an array of ids to get data from another table, I mean I want to get latest queues of every schedule id we are looping in it.
So first i have $schedules:
$schedules = [{"id":19},{"id":18},{"id":15}]
And the foreach loop is like this:
$queues= [];
foreach ($schedules as $schedule) {
$queues[] = Queue::withTrashed()
->latest()
->where('schedule_id', $schedule);
}
return $queues;
when i return queues it's showing :
Object of class Illuminate\Database\Eloquent\Builder could not be converted to string
The error that shows is related to you are not running the query, you need a ->get() at the end of Queue::...->where(...)->get() to do it.
if you dont do it, as Dhananjay Kyada say in his answer:
it will try to return a query object
But to return a response:
The Response content must be a string or object implementing __toString()
Next, we need to tackle one more thing.
If you are defining the variable $schedules and assigning it the value as it is shown in the question:
$schedules = [{"id":19},{"id":18},{"id":15}]
try to do it taking the JSON as string and converting it into a PHP variable with json_decode:
$schedules = json_decode('[{"id":19},{"id":18},{"id":15}]');
Then you can use the id values in you where clause:
->where('schedule_id', $schedule->id)->get()
Maybe because you are not getting the result from your query. Because it is just a query it will return a query object. You need to add ->get() in order to get the result. You can try the following code:
$queues = [];
foreach ($schedules as $schedule) {
$queues[] = Queue::withTrashed()
->latest()
->where('schedule_id', $schedule)->get()->toArray();
}
return $queues;
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 }}