如何从数据库到控制器检索项目?

时间:2019-01-13 08:24:14

标签: javascript php sql laravel

我想从数据库中检索数据到控制器并使用它作为视图。我已经尝试过但出现错误。

控制器

public function index()
{
    $trans = Uwdlog::where('user_id', Auth::user()->id)->orderBy('id', 'desc')->paginate(10);
    $rlink = url('/') . '/refer/' . Auth::User()->username;
    $package = Package::first();
    $user = User::find(Auth::User()->id);
    $avatar = Avatar::where('user_id', $user['id'])->pluck('photo')->first();
    $refers = User::where('refid', $user['id'])->orderBy('id', 'desc')->get();
    $balance = DB::select('select * from student');

    return view('home', compact('trans', 'rlink', 'refers', 'package', 'avatar', 'balance'));
}

home.blade.php

@extends('front.layouts.master')

@section('content')
<!--Start Admin Section-->
<section class="admin-two dashboard">
<!--Start Container-->
<div class="container">
<!--Start Admin Wrap-->
<div class="admin-two-wrap">
<!--Start Admin Wrap Row-->
<div class="row">
<!--Start Admin Menu Col-->
@if ($balance == null)
do this

@else
@include('front.layouts.sidebar')
<!--End Admin Menu Col-->
@endif
<!--Start Admin Content Col-->
<div class="col-md-9">
<!--Start Admin Content-->
<div classs="admin-two-cont">
<!--Start Dashboard-->
<div class="admin-dashboard">
<!--Start Row-->
<div class="row">
<!--Start Overview-->
<div class="db-overview">
<!--Start Overview Item Col-->
<div class="col-sm-3">
  <!--Start Overview Item-->
  <div class="overview-item {{Auth::User()->package_id == '1' ? 'two': 'one'}}">
      <h3 class="text-center">{{Auth::User()->firstname}} {{Auth::User()->lastname}}</h3>
      <p class="text-center"><i class="fa fa-user"></i></p>
      <p class="text-center">{{Auth::User()->package_id == '2' ? 'Premium': 'Free'}}</p>
  </div>
  <!--End Overview Item-->
</div>
<!--End Overview Item Col-->

<!--Start Overview Item Col-->
<div class="col-sm-3">
  <!--Start Overview Item-->
  <div class="overview-item three">
      <h3 class="text-center">Balance</h3>
      <p class="text-center"><i class="fa fa-shopping-bag"></i></p>
      <p class="text-center">{{ number_format(floatval(Auth::user()->balance) , $gset->decimalPoint, '.', '') }} 
{{$gset->curSymbol }}</p>
  </div>
  <!--End Overview Item-->
</div>
<!--End Overview Item Col-->

<!--Start Overview Item Col-->
<div class="col-sm-3">
  <!--Start Overview Item-->
  <div class="overview-item three">
      <h3 class="text-center">Refered User</h3>
      <p class="text-center"><i class="fa fa-share"></i></p>
      <p class="text-center">{{count($refers)}} Person</p>
  </div>
  <!--End Overview Item-->
</div>
<!--End Overview Item Col-->

<!--Start Overview Item Col-->
<div class="col-sm-3">
  <!--Start Overview Item-->
  <div class="overview-item {{Auth::User()->package_id == '1' ? 'four': 'one'}}">
      <h3 class="text-center">Status</h3>
      <p class="text-center"><i class="fa fa-joomla"></i></p>
      <p class="text-center">Active</p>
  </div>
  <!--End Overview Item-->
</div>
<!--End Overview Item Col-->
</div>
<!--End Overview-->
</div>
<!--End Row-->
<div class="row">
<!--Refer -->
<div class="db-overview">
  <div class="col-md-6">
    <div class="panel panel-success">
<div class="panel-heading">My Referal Link:</div>
<div class="panel-body">

<h3><code>{{ $rlink }}</code></h3>
</div>
</div> 
  </div>
  <div class="col-md-6">
    <div class="panel panel-info ">
<div class="panel-heading">Package Informaton</div>
<div class="panel-body">
  <table class="table-striped col-md-12">
    <thead>
      <tr>
        <th>Package</th>
        <th>Commision</th>
        <th>Daily Limit</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <th>Free</th>
        <td>{{$package->frcomm}} {{$gset->curSymbol}}</td>
        <td>{{$package->frlimit}} Person</td>
      </tr>
      <tr>
        <th>Premium</th>
        <td>{{$package->prcomm}} {{$gset->curSymbol}}</td>
        <td>{{$package->prlimit}} Person</td>
      </tr>
    </tbody>
  </table>
</div>
</div> 
  </div>

</div>
</div>



<!--Start Row-->
<div class="row">
<!--Start Visitors Country-->
<div class="admin-vistitor-country">
<div class="col-md-12">
  <div class="visitor-country">
      <h3 class="text-bold text-center">Transactions</h3>
      <div class="table-responsive">
          <table class="table table-responsive">
              <thead>
                  <tr>

              <th>
                Transaction ID
              </th>
              <th>
                Amount
              </th>
              <th>
                Operation
              </th>
              <th>
                Balance
              </th>
              <th>
                Description
              </th>
              <th>
                Processed at
              </th>


                  </tr>
              </thead>
              <tbody>
   @foreach($trans as $tran)
<tr class="{{ $tran->flag == "1" ? 'success' : 'danger' }}">
<td>
{{$tran-> trxid }}
</td>
<td>
{{number_format(floatval($tran-> amount), $gset->decimalPoint, '.', '')}} {{$gset-> curSymbol}}
</td>
<td>
{{ $tran->flag == "1" ? 'Credited' : 'Debited' }}
</td>
<td>
{{number_format(floatval($tran->balance), $gset->decimalPoint, '.', '')}}      
</td> 
<td>
{{$tran-> desc}}
</td>
<td>
{{$tran-> created_at}}
</td>
</tr>
@endforeach    
              </tbody>
              <tfoot>
                  <tr>
                      <th>
                Transaction ID
              </th>
              <th>
                Amount
              </th>
              <th>
                Operation
              </th>
              <th>
                Balance
              </th>
              <th>
                Description
              </th>
              <th>
                Processed at
              </th>

                  </tr>
              </tfoot>
          </table>
          <?php echo $trans->render(); ?>
      </div>
  </div>
</div>
</div>
<!--End Visitors Country-->
</div>
<!--End Row-->
</div>
<!--End Dashboard-->
</div>
<!--End Admin Content-->
</div>
<!--End Admin Content Col-->
</div>
<!--End Admin Wrap-->
</div>
</div>
<!--End Container-->

</section>
<!--End Admin Section-->


@endsection

我希望能够显示“执行此操作” if balance == null。我收到错误消息:

  

找不到类'App \ Http \ Controllers \ DB'。

请帮助检查我是否正确加载了视图。

2 个答案:

答案 0 :(得分:0)

在课程文件上方添加此行

use Illuminate\Support\Facades\DB;

答案 1 :(得分:0)

您已在控制器中使用$balance = DB::select('select * from student');。而且您可能忘记了导入 DB

在控制器头中添加use Illuminate\Support\Facades\DB;

您可以在https://laravel.com/docs/5.7/database#running-queries中查看更多信息。