Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

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

Last updated 2 years ago.
0

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)

Last updated 2 years ago.
0

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

Last updated 2 years ago.
0

Thank you ever so much TerrePorter!!!

It works!!!


$nerd = User::GetByTestID($supplierid)->first();

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.