I am working with Laravel 5.8 and I would like if it is possible that someone could help me with the following topic. I would like to store the contents of a collection in text format in a variable.
The query is as follows:
$dependentjobs = DB::table('users')->join('jobs', 'users.job_id', '=', 'jobs.id')
->select('jobs.jobname')
->where('users.topjob_id', '=', $userselected->job_id)
->get();
Once I have obtained the query data, I convert it into an array
if ($dependentjobs->count() > 0) {
$jobs = $dependentjobs->pluck('jobname');
}
So far everything is correct. What I would like now is to be able to save the array values as a string in a variable to pass to the view.
I've tried looping through $jobs with a foreach, but it gives me an error.
All the best.
Thanks everyone for your help.
The problem I was having is that I hadn't declared the variable before starting the foreach.
In this way if I store the content of the collection.
Greetings and thank you.
Alternatively, although you already found a solution, you can simplify things quite a bit the Laravel way.
You can use implode() which will return a text string.
For example, if this query returns a collection where items have an attribute
jobname
:You can use
implode()
in the following ways: