PHP Laravel Select Query
foreach ($task as $task1) {
$parent=\DB::select('select parent_wbs_id from projwbs where wbs_id= "$task1->wbs_id"');
foreach ($parent as $parent1){
\DB::table('tasks')->insert([
['id'=>$task1->task_id, 'text'=>$task1->task_name, 'start_date'=>$task1->act_start_date,
'duration'=>round($days), 'parent'=>$parent1->parent_wbs_id,'progress'=>1.0]
]);
}
}
My parent select query has where clause in which it is checking wbs_id from for each loop of task and getting parent_wbs_id accordingly.
Now, the question is-- It is not going inside my inside parent loop and I am stuck If It is returning an empty row to $parent.
And I tried displaying $parent. It prompted me with an error of invalid array to string conversion
Sorry, I have just started PHP laravel project and I am a beginner in this.
Answer
Solution:
Change your code to this:
foreach ($task as $task1) {
// $parent=\DB::select('select parent_wbs_id from projwbs where wbs_id= "$task1->wbs_id"');
$parent = \DB::table('projwbs')
->where('wbs_id', $task1->wbs_id)
->select('parent_wbs_id')
->get();
// $parent should contains all parent_wbs_id from table projwbs where wbs_id is equal to $task1->wbs_id
}
The main error in your first query was you didn't use get
at the end, so you were iterating through a query builder.
Also, you can't use select
the way you did, I rewrote it with the way Laravel expects.