daugaard47
daugaard47

Reputation: 1868

Use multiple queries in blade view laravel

Pretty new to Laravel and trying to figure out the ins and outs. If I write one main query that selects everything in my table, can I write individual queries when selecting columns and rows from the table.

Example:

Main Query:

public function allTickets(){
$tickets = DB::table('tickets')->get();
return view('admin.index',compact('tickets'));

In my view:

//THIS WORKS. It gives me a count of all open and closed tickets
{{$tickets->where('status', '=', 'OPEN')->Count()}}
{{$tickets->where('status', '=', 'CLOSED')->Count()}}

However this does not work...

Error: Call to a member function where() on string (View: ...

@foreach ($tickets as $ts)
{{$ts->subject->where('status', '=', 'OPEN')}}
@endforeach

@foreach ($tickets as $ts)
{{$ts->subject->where('status', '=', 'CLOSED')}}
@endforeach

Is there a way to use that ONE main query and show all the subjects of the tickets that are open and closed, or will I need to write multiple queries in my controller to achieve this?

Upvotes: 0

Views: 794

Answers (1)

Ozal  Zarbaliyev
Ozal Zarbaliyev

Reputation: 638

You can do like this with simple solution. first write one and then another one with if statement

@foreach ($tickets as $ts)
  @if($ts->status == 'OPEN')
     {{$ts->subject}}
  @endif
@endforeach

@foreach ($tickets as $ts)
  @if($ts->status == 'CLOSED')
    {{ $ts->subject }}
  @endif
@endforeach

Upvotes: 4

Related Questions