Didik Ariyana
Didik Ariyana

Reputation: 39

How Can Declare Query in laravel blade?

Can I declare a query in the laravel blade? cause in CI you can use like this code

$cek_tiket= $this->db->query("SELECT * from tiket WHERE tanggal='$tgl' AND id_jadwal='$jadwal->id_jadwal'");

Upvotes: 0

Views: 2129

Answers (3)

Eka
Eka

Reputation: 91

Not good idea but maybe using controller to get table and in the view you can filter by where clause. example : controller.php

$ticket = DB::table('tiket');

and in the view you can using where clause. view.blade.php

@php
$ticket->where('tanggal', $tgl)->where('id_jadwal', $jadwal)->first();
@endphp

Upvotes: 0

Prashant Deshmukh.....
Prashant Deshmukh.....

Reputation: 2292

Yes you can, but not recommended.

   @php
       $cek_tiket = DB::select(DB::raw("SELECT * from tiket WHERE tanggal='$tgl' AND id_jadwal='$jadwal->id_jadwal'"));
   @endphp

Then use it liek this {{$cek_tiket}}

Upvotes: 0

Jeremy Harris
Jeremy Harris

Reputation: 24579

You can query in the view, but it is a BAD PRACTICE.

You would do something like this (untested):

<div class="ticket-number">
    {{
    DB::table('tiket')
        ->where('tangal', $tgl)
        ->where('id_jadwal', $jadwal->id_jadwal)
        ->first()
        ->id;
    }}
</div>

Like I said though, this is NOT a good way to do it.

Instead, in your controller get the data from your model and pass it to the view.

Upvotes: 0

Related Questions