Added a way to starr a product, so it floats on top of the list of products

This commit is contained in:
Krzysztof Płaczek
2024-05-19 11:43:56 +02:00
parent 584bdcdf89
commit a204439a27
4 changed files with 21 additions and 8 deletions

View File

@@ -18,7 +18,8 @@ if (isset($_GET['product_id'])) {
} }
if (isset($_GET['category'])) { if (isset($_GET['category'])) {
$template = 'productList.html.twig'; $template = 'productList.html.twig';
$products = Product::with('price')->selectRaw('products.*')->fromRaw('products, json_each(products.categories)')->whereRaw('json_each.value = ?', [$_GET['category']])->get(); $products = Product::with('price')->selectRaw('products.*')->fromRaw('products, json_each(products.categories)')->whereRaw('json_each.value = ?', [$_GET['category']])
->orderByDesc('starred')->orderByDesc('created_by')->get();
} }
if (isset($_GET['search'])) { if (isset($_GET['search'])) {
$template = 'productList.html.twig'; $template = 'productList.html.twig';
@@ -26,9 +27,12 @@ if (isset($_GET['search'])) {
->orWhere([['name', 'like', "%{$_GET['search']}%"]]) ->orWhere([['name', 'like', "%{$_GET['search']}%"]])
->orWhere([['subTitle', 'like', "%{$_GET['search']}%"]])->get(); ->orWhere([['subTitle', 'like', "%{$_GET['search']}%"]])->get();
} }
if (isset($_GET['star'])) {
Product::find($_GET['star'])->toggleStarred()->save();
header('Location: '.$_SERVER['HTTP_REFERER']);
}
if (empty($_GET)) { if (empty($_GET)) {
$template = 'productList.html.twig'; $template = 'productList.html.twig';
$products = Product::with('price')->get(); $products = Product::with('price')->orderByDesc('starred')->orderByDesc('created_by')->get();
} }
$twig->display($template, ['products' => $products, 'product' => $product, 'search' => $_GET['search']]); $twig->display($template, ['products' => $products, 'product' => $product, 'search' => $_GET['search']]);

View File

@@ -31,6 +31,7 @@ if (!Capsule::schema()->hasTable('products')) {
$table->string('variantCode'); $table->string('variantCode');
$table->string('modelCode'); $table->string('modelCode');
$table->string('url'); $table->string('url');
$table->boolean('starred')->default(false);
$table->timestamps(); $table->timestamps();
}); });
} }

View File

@@ -18,6 +18,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
* @property string $variantCode * @property string $variantCode
* @property string $modelCode * @property string $modelCode
* @property string $url * @property string $url
* @property boolean $starred
*/ */
class Product extends Model class Product extends Model
{ {
@@ -29,6 +30,12 @@ class Product extends Model
return $this->hasMany(Price::class); return $this->hasMany(Price::class);
} }
public function toggleStarred(): self
{
$this->starred = !$this->starred;
return $this;
}
protected function categories(): Attribute protected function categories(): Attribute
{ {
return Attribute::make( return Attribute::make(

View File

@@ -4,18 +4,19 @@
<table class='table table-hover'> <table class='table table-hover'>
{% for product in products %} {% for product in products %}
<tr> <tr>
<td class="align-middle font-weight-bold h3"><a class="text-warning text-decoration-none" href="?star={{ product.id }}">{% if product.starred %}{% else %}{% endif %}</a></td>
<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 class="align-middle"><a href='?product_id={{ product.id }}'>{{ product.name }}</a></td>
<td>{{ product.subTitle }}</td> <td class="align-middle">{{ product.subTitle }}</td>
<td> <td class="align-middle">
<ul class='nav'> <ul class='nav'>
{% for category in product.categories %} {% for category in product.categories %}
<li class="nav-item"><a class="nav-link" href="?category={{ category }}"> {{ category }} </a></li> <li class="nav-item"><a class="nav-link" href="?category={{ category }}"> {{ category }} </a></li>
{% endfor %} {% endfor %}
</ul> </ul>
</td> </td>
<td><a href='https://pl.ryobitools.eu/{{ product.url }}'>link</a></td> <td class="align-middle"><a href='https://pl.ryobitools.eu/{{ product.url }}'>link</a></td>
<td>{{ product.price.last.price }}</td> <td class="align-middle">{{ product.price.last.price }}</td>
</tr> </tr>
{% endfor %} {% endfor %}
</table> </table>