Casting json categories column to array, filtering by categories.

This commit is contained in:
Krzysztof Płaczek
2024-03-27 10:56:30 +01:00
parent b4536e4c6b
commit 3d49eb5c92
2 changed files with 50 additions and 8 deletions

View File

@@ -15,14 +15,23 @@ $capsule->addConnection([
$capsule->setAsGlobal(); $capsule->setAsGlobal();
$capsule->bootEloquent(); $capsule->bootEloquent();
echo '<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous"></head>'; echo '<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous"></head>';
echo "<table class='table table-hover'>";
if (isset($_GET['product_id'])) { if (isset($_GET['product_id'])) {
$product = Product::with('price')->find($_GET['product_id']); $product = Product::with('price')->find($_GET['product_id']);
echo "<a href='/browser.php'>back</a><table class='table table-hover'>";
echo "<tr> echo "<tr>
<td><img src='$product->image&width=70' class='img-fluid' alt='$product->name' /></td> <td><img src='$product->image&width=150' class='img-fluid' alt='$product->name'/></td>
<td><a href='?product_id=$product->id'>$product->name</a></td> <td><a href='?product_id=$product->id'>$product->name</a></td>
<td>$product->subTitle</td> <td>$product->subTitle</td>
<td><a href='https://pl.ryobitools.eu/{$product->url}'>link</a></td>"; <td><a href='https://pl.ryobitools.eu/{$product->url}'>link</a></td></tr>";
echo "</table>";
echo "<table class='table table-hover'>
<thead>
<tr>
<th>price</th>
<th>lowest product price in 30 days</th>
<th colspan='2'>standard price</th>
</tr>
</thead>";
/** @var Price $price */ /** @var Price $price */
foreach ($product->price as $price) { foreach ($product->price as $price) {
echo "<tr> echo "<tr>
@@ -32,16 +41,39 @@ if (isset($_GET['product_id'])) {
<td>$price->created_at</td> <td>$price->created_at</td>
</tr>"; </tr>";
} }
} else { echo "</table>";
$products = Product::with('price')->get(); }
if (isset($_GET['category'])) {
$products = Product::with('price')->where('categories', 'LIKE', '%'.$_GET['category'].'%')->get();
echo "<table class='table table-hover'>";
foreach ($products as $product) { foreach ($products as $product) {
echo "<tr> echo "<tr>
<td><img src='$product->image&width=70' class='img-fluid' alt='$product->name' /></td> <td><img src='$product->image&width=70' class='img-fluid' alt='$product->name' /></td>
<td><a href='?product_id=$product->id'>$product->name</a></td> <td><a href='?product_id=$product->id'>$product->name</a></td>
<td>$product->subTitle</td> <td>$product->subTitle</td><td><ul class='nav'>";
<td><a href='https://pl.ryobitools.eu/{$product->url}'>link</a></td> foreach ($product->categories as $category) {
echo '<li class="nav-item"><a class="nav-link" href="?category=' . $category . '">' . $category . '</a></li>';
}
echo "</td><td><a href='https://pl.ryobitools.eu/{$product->url}'>link</a></td>
<td>{$product->price->last()->price}</td> <td>{$product->price->last()->price}</td>
</tr>"; </tr>";
} }
echo "</table>";
}
if (!isset($_GET['product_id']) && !isset($_GET['category'])) {
$products = Product::with('price')->get();
echo "<table class='table table-hover'>";
foreach ($products as $product) {
echo "<tr>
<td><img src='$product->image&width=70' class='img-fluid' alt='$product->name' /></td>
<td><a href='?product_id=$product->id'>$product->name</a></td>
<td>$product->subTitle</td><td><ul class='nav'>";
foreach ($product->categories as $category) {
echo '<li class="nav-item"><a class="nav-link" href="?category=' . $category . '">' . $category . '</a></li>';
}
echo "</td><td><a href='https://pl.ryobitools.eu/{$product->url}'>link</a></td>
<td>{$product->price->last()->price}</td>
</tr>";
} }
echo "</table>"; echo "</table>";
}

View File

@@ -2,6 +2,7 @@
namespace Krzysiej\RyobiCrawler\Models; namespace Krzysiej\RyobiCrawler\Models;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
@@ -22,8 +23,17 @@ class Product extends Model
{ {
public $timestamps = true; public $timestamps = true;
public $fillable = ['skuID']; public $fillable = ['skuID'];
public function price(): HasMany public function price(): HasMany
{ {
return $this->hasMany(Price::class); return $this->hasMany(Price::class);
} }
protected function categories(): Attribute
{
return Attribute::make(
get: fn(string $value) => array_reverse(json_decode($value, 1)),
);
}
} }