不应静态调用非静态方法App \ User :: getallreferrals()

时间:2017-08-22 14:49:17

标签: php laravel laravel-5.4

我试图在我的控制器中调用我的模型中的函数,但是我得到了这个错误。

  

(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');
}

}

1 个答案:

答案 0 :(得分: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]);
}