Get list of products from wordpress table using Laravel
Get list of products from wordpress table using Laravel
I'm working on a Laravel project where I need to retrieve products from a WordPress database based on a specific attribute value. The attribute in question is "brand," and I'm trying to fetch products where the brand is "7M." I'm using Laravel Eloquent and have set up models for Product and PostMeta to interact with the WordPress tables.
class Product extends Model{
public function meta()
{
return $this->hasMany('App\PostMeta', 'post_id');
}}
class PostMeta extends Model{
public function scopeMetaKey($query, $key)
{
return $query->where('meta_key', $key);
}
public function scopeMetaValue($query, $value)
{
return $query->where('meta_value', $value);
}
}
$brandName = '7M';
$metaKey = 'pa_brand'; // Replace with the actual meta key for the brand attribute
$product = Product::whereHas('meta', function ($query) use ($brandName, $metaKey) {
$query->metaKey($metaKey)->metaValue($brandName);
})->first();
this is the solution suggested by chatgpt, coz I am not an expert of WordPress tables.
when I execute this query it returns me null, but I have a product whose attribute has brand =7M.
Perhaps you can debug this by examining the actual SQL query sent to the database.
\DB::enableQueryLog(); // Enable the query log
// Your Eloquent queries
dd(\DB::getQueryLog()); // Display the query logs
Hope this helps.
December 30, 2023
Turbo Multi-language Translator
Make the better internet purchasing globaly
Turbosify SEO Speed Booster
5.0 (7)•Free plan available
Get better conversions by improving store loading speed
Installed
Turbo Multi-language Chat - AI Customer service in one hand
December 30, 2023