【问题标题】:I get an error when want to display specific data using "where"想要使用“where”显示特定数据时出现错误
【发布时间】:2017-01-31 17:24:06
【问题描述】:

我的表用户 ->

Schema::create('mstanggota', function (Blueprint $table) {
            $table->increments('id');
            $table->string('no_anggota',10);
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });

表 Mstbeasiswas ->

 Schema::create('mstbeasiswas', function (Blueprint $table) {
            $table->increments('id');
            $table->string('no_anggota',10)->unique();
            $table->string('nm_anak',25);
            $table->string('kt_lahir',25);
            $table->date('ttl');
            $table->String('nm_skl',50);
            $table->String('st_pend',6);
            $table->String('lbg_pend',6);
            $table->String('prov_skl');
            $table->String('jenkel',9);
            $table->integer('k_umum');
            $table->integer('k_khusus');
            $table->integer('score')->nullable();
            $table->boolean('status')->default(0);
            $table->string('ket',250)->nullable();
            $table->string('img1',50)->nullable();
            $table->string('img2',50)->nullable();
            $table->string('img3',50)->nullable();
            $table->timestamps();
        });

我想尝试用控制器显示数据,如下代码:

    <?php

namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use App\Mstbeasiswa;
use App\User;
use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Database\Query\Builder;

class BeasiswaController extends Controller
{


   public function index(){
    /*$datas = Mstbeasiswa::orderBy('id','DESC')->paginate(1);
    return view('home')->with('datas', $datas);

    */
    $user = Auth::user()->no_anggota;
    $user = Mstbeasiswa::table('mstbeasiswas')->where('no_anggota', $user)->first();
    echo $user->no_anggota;
   }


}

我的错误->

Builder.php 第 2405 行中的 BadMethodCallException: 调用未定义的方法 Illuminate\Database\Query\Builder::table()

帮帮我:(

【问题讨论】:

  • 原因是Mstbeasiswa::table('mstbeasiswas')——如果你的Mstbeasiswa扩展Model,你不应该在那里定义表名..但是在模型中..你能发布这个Mstbeasiswa ?
  • 删除table()-调用并尝试Mstbeasiswa::where('no_anggota', $user)-&gt;first();

标签: php sql laravel laravel-5.2


【解决方案1】:

假设您创建了一个扩展 Model 类的 Eloquent 模型,您可以删除 ::table(...) 部分并调用

Mstbeasiswa::where('no_anggota', $user)->first();

但这只会给你一个模型。如果您需要集合,请使用 -&gt;get() 而不是 -&gt;first(),如下所示:

Mstbeasiswa::where('no_anggota', $user)->get(); 

【讨论】:

  • 非常感谢,这很有帮助。
  • 没问题@SitiRahmah :) 请接受这个答案。
猜你喜欢
  • 2015-01-10
  • 1970-01-01
  • 2017-04-04
  • 1970-01-01
  • 2020-06-19
  • 1970-01-01
  • 2014-10-09
  • 2012-11-20
  • 2019-06-03
相关资源
最近更新 更多