When using the Hashid without passing the arguments it returns array but when passing arguments returns an empty array below are the codes
This is what i have done.
I created a Trait which is modifying the RouteKey according to the model, here is the code
<?php
namespace App\Traits;
use Hashids\Hashids;
use Illuminate\Database\Eloquent\Model;
trait Hashidable
{
public function getRouteKey()
{
$hashids = new Hashids(Model::class,16); //Passing Arguments
//$hashids = new Hashids(); //Without Arguments
return $hashids->encode($this->getKey());
}
}
The use this trait to a model, where by all my Models extend this Base Model
<?php
namespace App\Models;
use App\Traits\Hashidable;
use Illuminate\Database\Eloquent\Model;
class BaseModel extends Model
{
use Hashidable;
}
This way all the Model IDs are encoded means works fine.
When trying to decode the ide especially on find or edit the model resource it returns empty array []. Here is the decoding way. I have created Base Repository which handles all the system eloquent functions as shown below
<?php
namespace App\Repositories;
use App\Contracts\BaseContract;
use Illuminate\Database\Eloquent\Model;
use Hashids\Hashids;
class BaseRepository implements BaseContract
{
/**
* Model Var
*/
protected $model;
protected $hashidable;
/**
* Base Repository constructor.
*/
public function __construct(Model $model)
{
$this->model = $model;
$this->hashidable = new Hashids();
}
public function find($id)
{
return $this->decodable($id);
}
/**
* Decode ID
*/
public function decodable($id)
{
//dd($id); //Returns the hash ID
dd($this->hashidable->decode($id)); //Here returns empty array []
}
}
I have injected the repository to Controller contrustor to use the repository methods Example code below
<?php
namespace App\Http\Controllers\Individual;
use App\Http\Controllers\AuthController;
use App\Http\Requests\ImportFileRequest;
use App\Http\Requests\IndividualRequest;
use App\Imports\IndividualImport;
use App\Repositories\IndividualRepository;
use App\Services\IndividualService;
use Maatwebsite\Excel\Facades\Excel;
use Exception;
class IndividualController extends AuthController
{
/**
* @var
*/
protected $individual;
protected $individualService;
/**
* Individual Controller constructor.
*/
public function __construct(IndividualRepository $individual, IndividualService $individualService)
{
parent::__construct();
$this->individual = $individual;
$this->individualService = $individualService;
}
//When trying to get resource for editing it returns null array
/**
* Show the form for editing the specified resource.
*/
public function edit($id)
{
$individual = $this->individual->find($id);
if (!is_null($individual)){
return view('individuals.edit',compact('individual'));
}
else {
return $this->error();
}
}
What I'm i doing wrong so getting an empty array instead of resource values
When using the Hashid without passing the arguments it returns array but when passing arguments returns an empty array below are the codes
This is what i have done. I created a Trait which is modifying the RouteKey according to the model, here is the code
The use this trait to a model, where by all my Models extend this Base Model
This way all the Model IDs are encoded means works fine.
When trying to decode the ide especially on find or edit the model resource it returns empty array []. Here is the decoding way. I have created Base Repository which handles all the system eloquent functions as shown below
I have injected the repository to Controller contrustor to use the repository methods Example code below
What I'm i doing wrong so getting an empty array instead of resource values