在我现在使用Laravel 9和Laravel 5的Backpack开发的应用程序中,我有用户模型和公司模型,用户有许多公司,公司属于一个用户。
用户可以将公司添加到数据库中,然后创建发票,费用,报价,员工等,但基本上它们属于公司,而不是用户。
我在 setup() 方法中的每个 controller 中添加了以下行:
$this->crud->addClause('WHERE', 'company_id', backpack_user()->company_id);
一切看起来都很好,但在客户端页面的标题让我们说,这是属于用户的公司的客户端它指出公司显示1到1的1条目(从6总条目过滤).重置这让我相信我做错了什么.
用户名.php
<?php
namespace App\Models;
use Backpack\CRUD\app\Models\Traits\CrudTrait;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable
{
use CrudTrait;
use HasApiTokens, HasFactory, Notifiable;
use HasRoles;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
'company_id'
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function company() {
return $this->hasOne(\App\Models\Company::class, 'company_id', 'company_id');
}
}
公司.php
<?php
namespace App\Models;
use Backpack\CRUD\app\Models\Traits\CrudTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Company extends Model
{
use CrudTrait;
use HasFactory, SoftDeletes;
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = false;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'user_id',
'company_name',
'company_registration',
'company_vat',
'company_address1',
'company_address2',
'company_city',
'company_postcode',
'company_created',
'company_updated',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'company_id' => 'integer',
'user_id' => 'integer',
'company_created' => 'datetime',
'company_updated' => 'datetime',
];
public function user()
{
return $this->belongsTo(User::class);
}
public function invoice() {
return $this->hasMany(Invoice::class, 'company_id');
}
protected $primaryKey = 'company_id';
}
客户端.php
<?php
namespace App\Models;
use Backpack\CRUD\app\Models\Traits\CrudTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Client extends Model
{
use CrudTrait;
use HasFactory, SoftDeletes;
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = false;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'company_id',
'client_name',
'client_registration',
'client_vat',
'client_utr',
'client_cis',
'client_address1',
'client_address2',
'client_address2',
'client_city',
'client_postcode',
'client_country',
'client_payments_method',
'client_payments_term',
'client_country',
'client_notes',
'client_created',
'client_updated',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'client_id' => 'integer',
'company_id' => 'integer',
'client_vat' => 'string',
'client_utr' => 'string',
'client_cis' => 'boolean',
'client_country' => 'integer',
'client_payments_method' => 'integer',
'client_payments_term' => 'integer',
'client_created' => 'datetime',
'client_updated' => 'datetime',
];
public function company()
{
return $this->belongsTo(Company::class, 'company_id', 'company_id');
}
public function invoices() {
return $this->hasMany(\App\Models\Invoice::class, 'client_id');
}
public function country() {
return $this->belongsTo(\App\Models\Country::class, 'client_country', 'country_id');
}
public function paymentsMethod() {
return $this->belongsTo(\App\Models\PaymentsMethod::class, 'client_payments_method', 'payments_method_id');
}
public function paymentsTerm() {
return $this->belongsTo(\App\Models\PaymentsTerm::class, 'client_payments_term', 'payments_term_id');
}
protected $primaryKey = 'client_id';
}
ClientCrudController.php
public function setup()
{
CRUD::setModel(\App\Models\Client::class);
CRUD::setRoute(config('backpack.base.route_prefix') . '/client');
CRUD::setEntityNameStrings('client', 'clients');
CRUD::enablePersistentTable();
$this->crud->addClause('WHERE', 'company_id', backpack_user()->company_id);
}
你对这件事有什么建议吗?
谢谢你乔治
2条答案
按热度按时间qkf9rpyu1#
您可以通过使用
CRUD::addBaseClause()
而不是CRUD::addClause()
来避免在ListOperation的顶部显示“filtered from 6 total entries”。这将添加您的子句baseQuery
。在您的情况下,它将是:这个函数以前没有文档记录,所以我在这里添加了它-https://backpackforlaravel.com/docs/5.x/crud-operation-list-entries#custom-query-1
pepwfjgg2#
用户有许多公司,它应该在用户模型:
我很困惑为什么你需要在用户表中的company_id列。如果用户只有一个公司或有很多公司,你不需要该列。只需删除它