【问题标题】:Non-static method App\User::addresses() should not be called statically不应静态调用非静态方法 App\User::addresses()
【发布时间】:2020-03-08 16:45:00
【问题描述】:

我正在尝试获取已登录的用户的所有地址,我已在模型地址和用户中创建了关系。任何登录的客户/用户都可以保存多个地址,并将一个地址作为该部分工作的主要地址,但保存后我想获取该特定客户的所有地址,所以我创建了关系。我试图使用

获取所有地址
$userAddress=User::addresses();

但我得到了

非静态方法 App\User::addresses() 不应被称为静态地址

这是我的用户模型:

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Hash;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'first_name', 'last_name', 'email', 'password','address_id'
    ];

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

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    /**
    * @return string
    */
    public function getFullNameAttribute()
    {
    return $this->first_name. ' '. $this->last_name;
    }

    public function setPasswordAttribute($pass){

        $this->attributes['password'] = Hash::make($pass);

    }

    public function wishlist(){
        return $this->hasMany(Wishlist::class);
    }

    public function addresses(){
        return $this->hasMany(Addresses::class);
    }
}

地址模型:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Addresses extends Model
{
     /**
     * @var string
     */
    protected $table = 'addresses';

     /**
     * @var array
     */
    protected $fillable = [
        'name', 'address', 'country', 'state', 'city','address_type','user_id','updated_at'
    ];

    public function user(){
        return $this->belongsTo(User::class);
     }
}

产品控制器:

<?php

namespace App\Http\Controllers\Site;
use App\Contracts\AttributeContract;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Contracts\ProductContract;
use App\Contracts\AddressContract;
use Cart;
use Validator;
use App\User;

class ProductController extends Controller
{ 
    protected $productRepository;
    protected $attributeRepository;
    protected $addressRepository;
    public function __construct(ProductContract $productRepository, AttributeContract $attributeRepository, AddressContract $addressRepository)
{  
    $this->productRepository = $productRepository;
    $this->attributeRepository = $attributeRepository;
    $this->addressRepository = $addressRepository;
}

    public function index()
    {
        $products = $this->productRepository->listFeaturedProduct();
        return view('site.pages.homepage', compact('products'));
    }

public function checkout(Request $request)
{
    $userAddress=User::addresses();
    return view('site.pages.checkout');
}

public function addUserAddress(Request $request)
{
    $customer_name=$request->customer_name;
    $customer_address=$request->customer_address; 
    $country=$request->country; 
    $city=$request->city;
    $zip_code=$request->zip_code;  
    $state=$request->state; 
    $address_type=$request->address_type; 
    $is_primary_address=$request->primary_address; 
    $userID=auth()->user()->id;
    $data=array('name'=>$customer_name,'address'=>$customer_address,'country'=>$country,'state'=>$state,'city'=>$city,'address_type'=>$address_type,'user_id'=>$userID,'is_primary_address'=>$is_primary_address);
    $userAddress  = $this->addressRepository->addAddress($data);
    return redirect()->back()->with('message', 'Address Added');
}

}

【问题讨论】:

  • 非静态使用,auth()-&gt;user()-&gt;addresses,因为你需要一个用户的地址,你首先需要用户从认证或查询它User::find(1)-&gt;addresses
  • 找不到“App\Addresses”类。
  • 您有命名空间问题。我会在答案中写给你,评论不够大

标签: laravel laravel-5 eloquent


【解决方案1】:

您的用户命名空间是App\User,您的地址命名空间是App\Models\Addresses

您需要编辑您的两个模型。

用户模型

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Hash;

use App\Models\Addresses; // You are missing this line !!!!

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'first_name', 'last_name', 'email', 'password','address_id'
    ];

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

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    /**
    * @return string
    */
    public function getFullNameAttribute()
    {
    return $this->first_name. ' '. $this->last_name;
    }

    public function setPasswordAttribute($pass){

        $this->attributes['password'] = Hash::make($pass);

    }

    public function wishlist(){
        return $this->hasMany(Wishlist::class);
    }

    public function addresses(){
        return $this->hasMany(Addresses::class);
    }
}

地址模型

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\User; // and this line here

class Addresses extends Model
{
     /**
     * @var string
     */
    protected $table = 'addresses';

     /**
     * @var array
     */
    protected $fillable = [
        'name', 'address', 'country', 'state', 'city','address_type','user_id','updated_at'
    ];

    public function user(){
        return $this->belongsTo(User::class);
     }
}

现在您可以使用关系来获取一个用户的地址

$id = 1;
$adresses = User::find($id)->addresses;
\\ or
$adresses = auth()->user()->addresses;

我还有两句话:

  • 模型不应该像 Addresses 那样是复数形式
  • 你有一个错字,英文里只有一个 D in adress 和法文不同。
  • 您应该注意到了,我们没有直接使用该方法,我们将其用作属性(-&gt;addresses 而不是-&gt;addresses())如果您使用该方法,那么您就是在使用查询生成器。李>
$user->addresses()->get();
\\or
$user->addresses()->latest()->first();

【讨论】:

    猜你喜欢
    • 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
    相关资源
    最近更新 更多