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

Hi ya ottz0, welcome to the family!

To be honest, you really don't need to do things the way you are at the moment. You are kinda over complicating things.

Laravel is really smart and you don't need to create the connections on the model. It's done for you.

Also, when you query your model from your controller, Eloquent is smart enough to create your object for you. For example, if you query for an item by the ID, it will return an instance of that model. If you get mulitple results, then you will get a Collection.

Try this and see how you get on...

Model

class Chainlist extends Eloquent{

   // this is all you need for this basic query... awesome!
}

In your controller

$myvar = Chainlist::find(14); // single record based on the ID
dd($myvar);

$myvar = Chainlist::where('colmnNameOfYourChoice', 'valueToSearchForMatch');  // in this example you'll get all results that match grouped in a collection

Hopefully that will make sense. Feel free to post back if you need anymore help.

I'd also suggest taking the time to read the Eloquent docs. You learn loads there. Also, there are heaps of tutorials out that that might of of use. If you prefer watching videos, Laracasts.com is a great resource.

Hope it helps.

Last updated 2 years ago.
0

Thanks for that, Wow that's pretty easy.

I obviously need to call my second DB in Eloquent? DB::connection('ean') for it to know not to use default one?

Also what I wrote is valid though? My main aim was just to practice to instantiate and get, set and return data from inside the class so I can do it with other stuff.

Last updated 2 years ago.
0

Hi T2theC

I am trying your code and I am think i'm getting the eloquent model rather than the data. Please remember i'm trying to access a second database

Last updated 2 years ago.
0

If I understand correctly you have two tables with the same name on different databases.

Model 1

class Chainlist extends Eloquent{
	// this will use the default database conneciton
}

Model 2

class Chainlist2 extends Eloquent{
	protected $table = "chaninlist"; // set table name 
	protected $connection = 'ean'; // this will use the specified database conneciton
}

The query

$query = DB::connection($this->ean)
                ->table($this->table)
                ->where($this->row, '=', $this->record)
                ->first();

can be either

$query = Chainlist::where($this->row, '=', $this->record)->first();
or 
$query = Chainlist2::where($this->row, '=', $this->record)->first();

Since your pulling one record from the alternative database you might be able to set up a relationship.

It would depend on the db fields on how to setup the elequent relationship.

It would allow querys like,

$data = Chainlist::where($this->row, '=', $this->record)->with('altdbdata')->first();

This would create a collection with a item named altdbdata conatining the data from that table/db.

The 'altdbdata' is whatever you named the relationship function in the model file.

More info, check out http://laravel.com/docs/4.2/eloquent#relationships

Hope that helps

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

ottz0 ottz0 Joined 15 Nov 2014

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.