Reputation: 81
Good morning I am having a trouble while trying to create a simple query with Eloquent.
This is mi colonia model:
class Colonia extends Model
{
protected $table = 'catalogo_colonias';
public function city()
{
return $this->belongsTo('App\City', 'ciudades_id');
}
}
if I do.
$response = Colonia::find(1)->city;
I am getting a response but i do not want to find by id, I am trying to make something like this.
$response = Colonia::where('codigo_postal', $codigo_postal)->city;
but it throws me an error.
Undefined property: Illuminate\Database\Eloquent\Builder::$city
and it is declared above as you can see, I guess that is a syntax problem, hope you could help me.
Thank you, greetings.
Upvotes: 1
Views: 175
Reputation: 1114
Based on the answer of @HCK has explained how to execute the results of query. Here I just want to improve way to return the value of the column directly.
$response = Colonia::where('codigo_postal', $codigo_postal)
->value('city');
Upvotes: 0
Reputation: 14241
When you do:
$response = Colonia::find(1)->city;
The find()
method will return the result of the query, in this case the first object of the catalogo_colonias
table. A single object, this object has all your defined properties, like the city
relation.
Now, when you use the where()
method instead, this still hasn't retrieve the relationship objects yet because in some cases you would want to keep constraining the query. Instead, it returns an instance of the Query Builder class. That's why it throws an error:
$response = Colonia::where('codigo_postal', $codigo_postal) // return query builder
->city; // this property isn't defined in the builder, hence, the error.
To get the result you need to append the get()
(to get all the results that matches the query) or the first()
or etc.
$response = Colonia::where('codigo_postal', $codigo_postal) // query builder
->first() // returns an instance of Colonia
->city; // now it can access the model properties.
Upvotes: 5
Reputation: 1022
Don't worry. It's easy fix. You have specified the column you would like to search against. But Eloquent hasn't really executed your query.
If you use Model::where('col', $value')
you have to specify:
Would you like to return one Model row? If so you can use first()
p.s. it will return the first one it finds, so if you're trying to find a name with Bob, and you have records Bob1, Bob2, Bob3
it will return Bob1
first.
Would you like to return all rows thats within your query, you can use get()
. You will have to foreach
your collection to access the realtionship on each instance.
Then you can access your relationship property.
Goodluck!
Upvotes: 0
Reputation: 3383
The problem in your code is that the Colonia::where()
returns the query builder. You need to extract it, using the method first()
, like:
$response = Colonia::where('codigo_postal', $codigo_postal)->first()->city;
You can extract the info from the query with two methods: first()
and get()
. The first()
returns only the first element found, and the get()
returns an array with the whole results.
I recommend you to read the Laravel Query Builder - Where Clauses
Upvotes: 0