There is a discussion about composite keys, which it sounds like your wanting, over here - http://laravel.io/forum/09-25-2014-models-for-tables-with-multiple-columns-for-primary-key
How do I override the find method in the show function?
static function show($supplierid)
{
// get the nerd
$nerd = User::find($supplierid);
// show the view and pass the nerd to it
return View::make('show')
->with('nerd', $nerd);
}
I have tried this in the User model. Which results in an exception.
public static function find($orderid) {
return User::where('orderid', '=', $orderid)->first();
}
ErrorException Declaration of User::find() should be compatible with Illuminate\Database\Eloquent\Model::find($id, $columns = Array)
You could try making your own function in the model.
in the Testmodel class
// not needed but to show the primary key is 'id'
protected $primaryKey = 'id';
public static function GetByTestID($id)
{
return SELF::where('testOtherKey', '=', $id);
}
(there could be some unknowns as I'm not running advanced querys with it, but it does return a eloquent object so it should work)
then query like this
// get only using the second primary key
$Testmodel = Testmodel::GetByTestID(3)->get();
// get a combo using the secondary and primary
$Testmodel = Testmodel::GetByTestID(3)->where('id', '=', 2)->get();
// could be done without the custom function with stacking where
$Testmodel = Testmodel::where('id', '=', 2)->where('testOtherKey', '=', 3)->get();
Hope that helps
Thank you ever so much TerrePorter!!!
It works!!!
$nerd = User::GetByTestID($supplierid)->first();
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community