I want to change my gettitle method into a scope but receive the error "object could not be converted to string".
Do query scopes have a speed advantage over methods? What is the advantage of creating a scope?
I have a table with products. Then there is a table with product_titles which also have a language_id.
Each product can have several titles since they can be in different languages.
public function scopeGettitle($language_id="1")
{
return self::product_titles()->select('title','information')
->where("language_id","=",$language_id);
}
public function product_titles ()
{
return $this->hasMany(ProductTitle::class);
}
In the blade file I use this code:
$title=$book->gettitle()->first();
Thank you
Edit: This seems the right way:
public function scopeGettitle($query, $language_id="1")
{
return $query
->select('title','information')
->where("language_id","=",$language_id);
}
$producttitle=$product->product_titles()->gettitle()->first();
My question is if there is any reason why this is better than to simply create a method which I can call in this way: $producttitle=$product->gettitle();
And to create a "bylanguage" scope in the Title model.
Scope parameters should be defined after the $query
parameter
$query
\\Illuminate\\Database\\Eloquent\\Builder
public function scopeGettitle($query, $language_id="1")
{
return $query
->product_titles()
->select('title','information')
->where("language_id","=",$language_id);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.