无法从Laravel中的数据库检索数据

时间:2019-10-31 23:34:47

标签: php database laravel

我的控制器具有功能

public function caritiket(Request $request){
    $tgl=($request->tgl);
    $kota1 = ($request->kota1);
    $kota2 = ($request->kota2);
    $jumlah = (int)$request->jml_p;

    // $cek = DB::table('tb_bis_wp')->where('tanggal',$request->tgl)->where('kota1',$request->kota1)->where('kota2',$request->kota2)->count();
    // if ($cek > 0) {
        $cek1 = DB::table('tb_bis_wp')->where('tanggal',$request->tgl)->where('kota1',$request->kota1)->where('kota2',$request->kota2)->get();
        foreach ($cek1 as $cek1) {
            $a=$cek1->tanggal;
            $b=$cek1->kota1;
            $c=$cek1->kota2;
        }
        return redirect('/tampil/daftartiket/{{$a}}/{{$b}}/{{$c}}/{{$jumlah}}');}

从视图输入的名称已经与函数caritiket(Request $ request)中的名称相同,此后,指向函数daftartiket()的路由链接 这是daftartiket函数

public function daftartiket($a,$b,$c,$jumlah){
        $jml= $jumlah;
$data = DB::table('tb_bis_wp')->where('tanggal',$a)->where('kota1',$b)->where('kota2',$c)->get();


                return view('member/daftar_tiket',['data'=>$data]);
    }

在视图中,我有这样的脚本

 <table class="table table-bordered tabel table-striped">
                <thead class="thead-dark text-center">
                    <tr>
                        <th scope="col">Kode Rute</th>
                        <th scope="col">Waktu</th>
                        <th scope="col">Rute</th>
                        <th scope="col">Kelas</th>
                        <th scope="col">Harga</th>
                        <th scope="col">Sisa Kursi</th>
                        <th scope="col">Option</th>
                    </tr>
                </thead>
                <tbody class="text-center">
                @foreach($data as $z)
                        <tr>
                            <td>{{$z->kode_rute}}</td>
                            <td>{{$z->waktu}}</td>
                            <td>{{$z->rute}}</td>
                            <td>{{$z->kelas}}</td>
                            <td>{{$z->harga}}</td>
                            <td>{{$z->sisa_kursi}}</td>
                        <td>
                            <a href="/tampil/#/{{$z->kode_rute}}" class="btn btn-primary ">Pesan</a>
                        </td>
                        </tr>
                    @endforeach
                </tbody>
              </table>

2 个答案:

答案 0 :(得分:0)

快速查看代码后,可能与以下行有关:foreach ($cek1 as $cek1) {确保名称不同,然后看是否有效。例如

<?php
foreach ($cek1 as $cek) {
    // ...
}

更新: 这是该功能的完整版本。

<?php

namespace App\Http\Controllers;

class MyController extends Controller
{
    public function caritiket(Request $request)
    {
        $tgl = $request->tgl;
        $kota1 = $request->kota1;
        $kota2 = $request->kota2;
        $jumlah = (int) $request->jml_p;

        // You are only grabbing the last value anyway.
        $cek1 = DB::table('tb_bis_wp')
            ->where('tanggal', $tgl)
            ->where('kota1', $kota1)
            ->where('kota2', $kota2)
            ->orderBy('id', 'DESC')
            ->first();

        if ($cek1) {
            $a = $cek1->tanggal;
            $b = $cek1->kota1;
            $c = $cek1->kota2;

            // You cannot use blade syntax in the controller.
            return redirect("/tampil/daftartiket/$a/$b/$c/$jumlah");
        }
    }
}

答案 1 :(得分:0)

首先,您必须了解如何编写代码。请参阅存在一些错误    您的代码,如果您将request-> kota1存储在$ kota1中,则在有条件的地方使用    $ kota1而不是$ request-> kota1,因为没有必要使用它。    foreach循环不要使用相同的变量来避免冲突。

public function caritiket(Request $request)
{
  $tgl = $request->tgl;
  $kota1 = $request->kota1;
  $kota2 = $request->kota2;
  $jumlah = (int)$request->jml_p;

    $cek1 = DB::table('tb_bis_wp')
            ->where('tanggal',$tgl) 
            ->where('kota1',$kota1)
            ->where('kota2',$kota2)
            ->get();

          foreach ($cek1 as $cek) 
          {
            $a=$cek1->tanggal;
            $b=$cek1->kota1;
            $c=$cek1->kota2;
          }
     return redirect('/tampil/daftartiket/{{$a}}/{{$b}}/{{$c}}/{{$jumlah}}');
    }
相关问题