Php L5如何使用散列id但保留pivot功能的特性
我使用trait向ID添加了哈希。但是,通过这样做,现在我不能再使用attach()或关系 例如,在我看来,这种关系不再有效:Php L5如何使用散列id但保留pivot功能的特性,php,laravel,laravel-5,laravel-5.3,Php,Laravel,Laravel 5,Laravel 5.3,我使用trait向ID添加了哈希。但是,通过这样做,现在我不能再使用attach()或关系 例如,在我看来,这种关系不再有效: @foreach ($invoice->items as $item) {{ $item->item }} @endforeach <?php namespace App; use App\Traits\HashedId; use Illuminate\Database\Eloquent\Model; use HipsterJazzbo\L
@foreach ($invoice->items as $item)
{{ $item->item }}
@endforeach
<?php
namespace App;
use App\Traits\HashedId;
use Illuminate\Database\Eloquent\Model;
use HipsterJazzbo\Landlord\BelongsToTenants;
class Invoice extends Model
{
use BelongsToTenants;
use HashedId;
//
protected $fillable = [
'client_id',
'invoice_number',
'purchase_order',
'invoice_note',
'invoice_status',
'invoice_total',
'invoice_type',
'sub_total',
'balance_due',
'due_date',
'invoice_type',
'user_id',
];
protected $hidden = [
'user_id'
];
public function items()
{
return $this->belongsToMany('App\LineItem', 'invoice_items', 'invoice_id', 'item_id');
}
public function client()
{
return $this->belongsTo('App\Client');
}
}
这是一个对我的id进行哈希运算的特征
<?php
namespace App\Traits;
use Hashids\Hashids;
use Illuminate\Database\Eloquent\Builder;
trait HashedId
{
/**
* Get the user's id as hashids.
*
* @param $value
* @return string
*/
public function getIdAttribute($value)
{
$hashids = new \Hashids\Hashids(env('APP_KEY'),10);
return $hashids->encode($value);
}
public function scopeHashId(Builder $query, $id)
{
$hashIds = new Hashids(env('APP_KEY'), 10);
$id = $hashIds->decode($id)[0];
return $query->where('id', $id);
}
}
如何继续使用控制器中的
$invoice->attach($lineItem)
或$invoice->items
,同时仍使用哈希ID的特性 正如您所评论的,您不能使用attach,因为id是getIdAttribute的哈希值。我建议您使用getOriginal()
比如说,
$invoice->attach($lineItem->getOriginal()['id']);
我认为这可能是附加该属性的唯一方法。正如您所评论的,您不能使用附加,因为id是getIdAttribute的哈希cos。我建议您使用getOriginal() 比如说,
$invoice->attach($lineItem->getOriginal()['id']);
我认为这可能是附加该特性的唯一方法。我将该特性重新编写如下(假设您使用的是PHP5.6或更高版本):
我将这个特性重新编写如下(假设您使用的是PHP5.6或更高版本):
我的目标是将id散列在URL中,使其不易识别,但我这样做的代价可能是无法使用许多关系,因为id总是被散列?我的目标是将id散列在URL中,使其不易识别,但我这样做的方式可能是同时出现的由于id总是被散列而无法使用很多关系的成本?Rob,你帮了很大的忙,所以感谢你的回复。我仍然有一个地狱般的时间使用关系,我可能已经超过了复杂的方式。你能给我一个完整的例子,说明我如何在URL中继续使用哈希而不是ID,以及如何使用$model->relationship->attach()
,等等吗?我仍在学习laravel和Hash以及traits对我来说都是新概念。这个粘贴应该显示我所做的一切:只是重申一下,url中的Hash ID工作得很好,但当我使用attach()时
在controller中,我在数据库上收到一个完整性约束冲突异常,因为它试图使用散列ID而不是真实ID来存储关系。HashedId.php第44行中的FatalErrorException:无法重新声明App\Traits\HashedId::scopeHashIds()
Oops,我的错。我已经将第一个作用域函数重命名为scopeHashId
,第二个是scopeHashIds
。没有错误,但是现在(假设我理解正确)如果我在我的视图中使用类似的内容:
那么url指向/invoices
,没有ID或hashRob,你帮了我大忙,谢谢你的回复。我仍然有一个地狱般的时间使用关系,我可能已经超过了复杂的方式。你能给我一个完整的例子,说明我如何在URL中继续使用哈希而不是ID,以及如何使用$model->relationship->attach()
,等等吗?我仍在学习laravel和Hash以及traits对我来说都是新概念。这个粘贴应该显示我所做的一切:只是重申一下,url中的Hash ID工作得很好,但当我使用attach()时
在controller中,我在数据库上收到一个完整性约束冲突异常,因为它试图使用散列ID而不是真实ID来存储关系。HashedId.php第44行中的FatalErrorException:无法重新声明App\Traits\HashedId::scopeHashIds()
Oops,我的错。我已经将第一个作用域函数重命名为scopeHashId
,第二个是scopeHashIds
。没有错误,但现在(假设我理解正确),如果我在视图中使用类似的内容:
,则url指向/invoices
,没有ID或哈希
<?php
namespace App\Traits;
use Hashids\Hashids;
use Illuminate\Database\Eloquent\Builder;
trait HashedId
{
/**
* Get model ID attribute encoded to hash ID.
*
* @return string
*/
public function getHashIdAttribute()
{
$hashIds = new Hashids(env('APP_KEY'), 10);
return $hashIds->encode($this->getKey());
}
/**
* Restrict query scope to find model by encoded hash ID.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param integer $id
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeHashId(Builder $query, $id)
{
$hashIds = new Hashids(env('APP_KEY'), 10);
$id = $hashIds->decode($id)[0];
return $query->where('id', $id);
}
/**
* Restrict query scope to find models by encoded hash IDs.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param array $ids
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeHashIds(Builder $query, ...$ids)
{
$hashIds = new Hashids(env('APP_KEY'), 10);
$ids = array_map(function ($id) use ($hashIds) {
return $hashIds->decode($id)[0];
}, $ids);
return $query->whereIn('id', $ids);
}
}