user7362769
user7362769

Reputation:

Laravel foreach in one item?

i have a relationship like

+------------+---------+
|    id      | problem |
+------------+---------+
| 1          | problem |    
+------------+---------+


+------------+----------+---------+
|    id      | problemid|solution |
+------------+----------+---------+
| 1          |  1       |solution1|    
+------------+----------+---------+
| 2          |  1       |solution2| 
+------------+----------+---------+

I have done a join and the problem is when i do a foreach in the view i get 2 items with the same problem and 2 different solutions but i need to get the same problem (one problem) and 2 solutions. How can i do it?

Here is the code in Controller:

$problem=Problem::select()
            ->join('solution','problem.id','=','solution.problemid')
            ->where('problem.id',$id)
            ->get();

Upvotes: 0

Views: 881

Answers (2)

user7362769
user7362769

Reputation:

I find the Solution and i am using in View:

@foreach ($problem as $probl)
    @if ($loop->first)
        Here i put the code for fist item
    @endif

@endforeach

For another persons that are searching for same question here is documentation :

https://laravel.com/docs/5.3/blade#the-loop-variable

Upvotes: 0

Adlan Arif Zakaria
Adlan Arif Zakaria

Reputation: 1745

$problem=Problem::select()
            ->leftjoin('solution','problem.id','=','solution.problemid')
            ->where('solution.id',$id)
            ->get();

Upvotes: 1

Related Questions