【发布时间】:2020-11-15 18:02:26
【问题描述】:
我不知道为什么会出现此错误:
Non-static method App\User::products() should not be called statically
这是我的控制器方法:
public function create()
{
$users = User::products('name', 'id');
return view('products.create')->with('users', $users);
}
这是我的模特
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
use Kyslik\ColumnSortable\Sortable;
use Illuminate\Database\Eloquent\Model;
class User extends Authenticatable
{
use Notifiable;
use HasRoles;
use Sortable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'surname', 'showname', 'business', 'NIP', 'PESEL', 'address', 'city', 'postalcode', 'phone', 'comments',
];
public $primaryKey = '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',
];
public $sortable = ['name',
'email',
'surname',
'showname',
'business',
'address',
'city',
'phone',
'role',
];
public function products()
{
return $this->hasMany('App\Product');
}
public function invoices()
{
return $this->hasMany('App\Invoice');
}
}
你能帮帮我吗?我正在尝试对此进行动态依赖下拉菜单。想要从用户模型中获取用户名和 id 以在视图中进行下拉,然后将产品与用户连接并将数据保存到带有用户 id 的产品表中。
【问题讨论】:
-
products是用户模型的关系,它不是你可以使用User模型调用的静态方法。
标签: php sql laravel orm frameworks