【问题标题】:Non-static method App\User::getallreferrals() should not be called statically不应静态调用非静态方法 App\User::getallreferrals()
【发布时间】:2017-08-23 00:30:58
【问题描述】:

我试图在我的控制器中调用我的模型中的函数,但我收到此错误。

(1/1) ErrorException 非静态方法 App\User::getallreferrals() 不应被静态调用。在 HomeController.php(第 37 行)

这是我的模型。

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Auth;

class User extends Authenticatable
{
use Notifiable;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'first_name','last_name','other_name','user_name','number2','email', 'password', 'number','bank_name','acct_number','acct_name', 'next_kin', 'next_kin_no', 'transaction_details', 'entry_matrix','referral_id', 'referred_by','first_downline_id', 'second_downline_id'
];

/**
 * The attributes that should be hidden for arrays. `1
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];


public function scopeGetallreferrals($query)
{
    return $query->where('referred_by', Auth::user()->referral_id)->pluck('referral_id');
}

public function scopeGetallreferralsobject($query)
{
    return $query->where('referred_by', Auth::user()->referral_id)->get();
}

public function scopeGetallreferrals2genobject($query)
{
    $referrals = $this->getallreferrals();
    return $query->where('referred_by', $referrals)->get();
}

public function scopeGetallreferrals2gen($query)
{
    $referrals = $this->getallreferrals();
    return $query->where('referred_by', $referrals)->pluck('referral_id');

}

public function scopeGetallreferrals3gen($query){
    $referrals2gen = $this->getallreferrals3gen();
    return $query->where('referred_by', $referrals2gen)->pluck('referral_id');

}

public function scopeGetcaptain($query)
{
    return $query->where('referral_id', Auth::user()->referred_by)->pluck('referred_by');
}

public function scopeGetallcaptain2gen($query)
{
    $captain = $this->getcaptain();
    return $query->where('referral_id', $captain)->pluck('referral_id');

}

public function  scopeGetallcaptain3gen($query){
    $captain2gen = $this->getcaptain2gen();
    return $query->where('referred_by', $captain2gen)->pluck('referral_id');

}

public function  scopeGetallreferralsbyID($query, $id){
    return $this->getallreferrals()->where('id', $id);
}

public function  scopeGetallreferrals2genbyID($query, $id){
    return $this->getallreferrals2gen()->where('id', $id);
}

public function  scopeGetallreferrals3genbyID($query, $id){
    return $this->getallreferrals3gen()->where('id', $id);
}

}

然后是我的控制器。

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Http\Controllers\DateTime;
use App\User;
use Auth;
use Session;


class HomeController extends Controller
{
/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('auth');
}

/**
 * Show the application dashboard.
 *
 * @return \Illuminate\Http\Response
 */

public function index()
{
    $captain = User::getcaptain();
    $referrals = User::getallreferrals();


    return view('home', ['captain' => $captain, 'referrals' => $referrals]);
}

请问我做错了什么?我在这个项目上已经有一段时间了。

我的迁移

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('first_name');
        $table->string('last_name');
        $table->string('other_name');
        $table->string('user_name');
        $table->string('number');
        $table->string('number2');
        $table->string('bank_name');
        $table->string('acct_number');
        $table->string('acct_name');
        $table->string('email')->unique();
        $table->string('password');
        $table->string('next_kin');
        $table->string('next_kin_no');
        $table->string('entry_matrix');
        $table->string('transaction_details')->nullable();
        $table->integer('isconfirmed')->default(0);
        $table->string('total_earned')->nullable();
        $table->string('total_paid')->nullable();
        $table->string('type_account')->nullable();
        $table->integer('stage')->default(0);
        $table->string('referral_id')->unique();
        $table->string('referred_by')->nullable();
        $table->rememberToken();
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('users');
}

}

【问题讨论】:

标签: php laravel laravel-5.4


【解决方案1】:

正如它所说的 getallreferrals 方法不是静态的,但你试图用 : 来调用它,这意味着静态。

应该是这样的

在用户模型中为此使用Scope

public function scopeGetallreferrals($query, $user)
{
    return $query->where('referred_by', $user->referral_id);
}

在控制器中

public function __construct(User $user)
{ 
    $this->middleware('auth');
    $this->user = Auth:user();
}

public function index()
{
    $captain = $this->user->getcaptain();
    $referrals = $this->user->getallreferrals($this->user)->pluck('referral_id');

    return view('home', ['captain' => $captain, 'referrals' => $referrals]);
}

【讨论】:

  • 谢谢,我认为这有效。但随后又出现了新的错误。 FatalThrowableError 调用 User.php 中 null 上的成员函数 where()(第 52 行)
  • 它是因为可能有一段时间用户没有被授权。需要检查空值
  • public function scopeGetcaptain($query) { return $this->model->where('referral_id', Auth::user()->referred_by)->pluck('referred_by'); } 那是行。返回值似乎为空。我不希望它显示错误。我该怎么做
  • @PatrickObafemi 看到我更新了答案。检查它现在是否正常工作。
  • 不,不是。还是同样的错误。当用户没有队长时,它会显示空错误,但当他有时,它不会显示错误。如果用户没有推荐,它会显示 null 错误,但他没有。
猜你喜欢
  • 2020-11-15
  • 1970-01-01
  • 2014-09-08
  • 1970-01-01
  • 2013-11-10
  • 1970-01-01
  • 1970-01-01
  • 2012-08-16
  • 2015-01-13
相关资源
最近更新 更多