DNABeast wrote a comment+100 XP
3d ago
DNABeast wrote a comment+100 XP
2w ago
This is a very good time to point out that the subtract and add items buttons need accessibility. You can add a title to the svgs <title>Substract Item</title> or add an aria title to the button. Something to remember on the remove item in the checkout section as well.
Yes it's a bit pedantic in a lesson like this but I find it's one of those things that can get missed if you're not on top of it all that time :)
DNABeast was awarded Best Answer+1000 XP
1mo ago
This was done by creating a custom relationship.
namespace App\Relations;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
// This was put together by Windsurf.
class LikeRelationship extends Relation
{
protected $localKey;
protected $foreignKey;
/**
* Create a new relationship instance.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Model $parent
* @param string $foreignKey
* @param string $localKey
* @return void
*/
public function __construct(Builder $query, Model $parent, string $foreignKey, string $localKey)
{
$this->foreignKey = $foreignKey;
$this->localKey = $localKey;
parent::__construct($query, $parent);
}
/**
* Set the base constraints on the relation query.
*
* @return void
*/
public function addConstraints()
{
if (static::$constraints) {
$parentValue = $this->getParentKey();
if ($parentValue === null) {
$this->query->whereRaw('1 = 0');
} else {
$this->query->whereRaw('? LIKE ' . $this->foreignKey, [$parentValue]);
}
}
}
/**
* Set the constraints for an eager load of the relation.
*
* @param array $models
* @return void
*/
public function addEagerConstraints(array $models)
{
$keys = $this->getKeys($models, $this->localKey);
if (empty($keys)) {
$this->query->whereRaw('1 = 0');
return;
}
// For LIKE relationships, we need to use OR conditions
$this->query->where(function ($query) use ($keys) {
foreach ($keys as $key) {
$query->orWhereRaw('? LIKE ' . $this->foreignKey, [$key]);
}
});
}
/**
* Initialize the relation on a set of models.
*
* @param array $models
* @param string $relation
* @return array
*/
public function initRelation(array $models, $relation)
{
foreach ($models as $model) {
$model->setRelation($relation, $this->related->newCollection());
}
return $models;
}
/**
* Match the eagerly loaded results to their parents.
*
* @param array $models
* @param \Illuminate\Database\Eloquent\Collection $results
* @param string $relation
* @return array
*/
public function match(array $models, Collection $results, $relation)
{
foreach ($models as $model) {
$parentValue = $model->getAttribute($this->localKey);
$matching = $results->filter(function ($result) use ($parentValue) {
$foreignValue = $result->getAttribute($this->foreignKey);
return $this->matchesLikePattern($parentValue, $foreignValue);
});
$model->setRelation($relation, $matching);
}
return $models;
}
/**
* Get the results of the relationship.
*
* @return mixed
*/
public function getResults()
{
return $this->query->get();
}
/**
* Get the key for comparing against the parent key in "has" query.
*
* @return string
*/
public function getForeignKeyName()
{
return $this->foreignKey;
}
/**
* Get the key for comparing against the parent key in "has" query.
*
* @return string
*/
public function getLocalKeyName()
{
return $this->localKey;
}
/**
* Get the parent's key value.
*
* @return mixed
*/
public function getParentKey()
{
return $this->parent->getAttribute($this->localKey);
}
/**
* Check if a value matches a LIKE pattern.
*
* @param string $value
* @param string $pattern
* @return bool
*/
protected function matchesLikePattern($value, $pattern)
{
// Convert SQL LIKE pattern to regex pattern
$regexPattern = str_replace(
['%', '_'],
['.*', '.'],
preg_quote($pattern, '/')
);
return preg_match("/^{$regexPattern}$/i", $value);
}
}
DNABeast wrote a reply+100 XP
1mo ago
This was done by creating a custom relationship.
namespace App\Relations;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
// This was put together by Windsurf.
class LikeRelationship extends Relation
{
protected $localKey;
protected $foreignKey;
/**
* Create a new relationship instance.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Model $parent
* @param string $foreignKey
* @param string $localKey
* @return void
*/
public function __construct(Builder $query, Model $parent, string $foreignKey, string $localKey)
{
$this->foreignKey = $foreignKey;
$this->localKey = $localKey;
parent::__construct($query, $parent);
}
/**
* Set the base constraints on the relation query.
*
* @return void
*/
public function addConstraints()
{
if (static::$constraints) {
$parentValue = $this->getParentKey();
if ($parentValue === null) {
$this->query->whereRaw('1 = 0');
} else {
$this->query->whereRaw('? LIKE ' . $this->foreignKey, [$parentValue]);
}
}
}
/**
* Set the constraints for an eager load of the relation.
*
* @param array $models
* @return void
*/
public function addEagerConstraints(array $models)
{
$keys = $this->getKeys($models, $this->localKey);
if (empty($keys)) {
$this->query->whereRaw('1 = 0');
return;
}
// For LIKE relationships, we need to use OR conditions
$this->query->where(function ($query) use ($keys) {
foreach ($keys as $key) {
$query->orWhereRaw('? LIKE ' . $this->foreignKey, [$key]);
}
});
}
/**
* Initialize the relation on a set of models.
*
* @param array $models
* @param string $relation
* @return array
*/
public function initRelation(array $models, $relation)
{
foreach ($models as $model) {
$model->setRelation($relation, $this->related->newCollection());
}
return $models;
}
/**
* Match the eagerly loaded results to their parents.
*
* @param array $models
* @param \Illuminate\Database\Eloquent\Collection $results
* @param string $relation
* @return array
*/
public function match(array $models, Collection $results, $relation)
{
foreach ($models as $model) {
$parentValue = $model->getAttribute($this->localKey);
$matching = $results->filter(function ($result) use ($parentValue) {
$foreignValue = $result->getAttribute($this->foreignKey);
return $this->matchesLikePattern($parentValue, $foreignValue);
});
$model->setRelation($relation, $matching);
}
return $models;
}
/**
* Get the results of the relationship.
*
* @return mixed
*/
public function getResults()
{
return $this->query->get();
}
/**
* Get the key for comparing against the parent key in "has" query.
*
* @return string
*/
public function getForeignKeyName()
{
return $this->foreignKey;
}
/**
* Get the key for comparing against the parent key in "has" query.
*
* @return string
*/
public function getLocalKeyName()
{
return $this->localKey;
}
/**
* Get the parent's key value.
*
* @return mixed
*/
public function getParentKey()
{
return $this->parent->getAttribute($this->localKey);
}
/**
* Check if a value matches a LIKE pattern.
*
* @param string $value
* @param string $pattern
* @return bool
*/
protected function matchesLikePattern($value, $pattern)
{
// Convert SQL LIKE pattern to regex pattern
$regexPattern = str_replace(
['%', '_'],
['.*', '.'],
preg_quote($pattern, '/')
);
return preg_match("/^{$regexPattern}$/i", $value);
}
}
DNABeast wrote a reply+100 XP
1mo ago
DNABeast started a new conversation+100 XP
1mo ago
I've got a Model with an id in three parts
'id' => 'FIRSTNAME_SECONDNAME_TITLE'
I've got a second model where I have matching foreign_ids but the data that goes in there has wildcards.
'name_id'=> '???_SMITH_??'
I want to be able to set up a relationship when all the parts that are filled in match but the wildcards are ignored.
I think I need subQueries in my hasMany method but can't work out how to do it.
In my Name Model
public function relatedNames(): HasMany
{
return $this->hasMany(RelatedName::class, 'name_id', 'id')
->orWhere(function ($query) {
$query->where(function ($query) {
$query->where('firstname', $this->firstname)
->orWhere('firstname', '!==', '???');
})
->where(function ($query) {
$query->where('lastname', $this->lastname)
->orWhere('lastname', '!==', '???');
})
->where(function ($query) {
$query->where('title', $this->title)
->orWhere('title', '!==', '??');
});
});
}
In this demo every firstname and last name has the same amount of characters and they are in the database as stored generated columns.
Relationships don't seem to allow orWhere methods on them. How do I made this relationship include these partial matches?
DNABeast wrote a comment+100 XP
2mos ago
DNABeast wrote a comment+100 XP
2mos ago
I'm bitter. I started when the goal was to make the whole site 64k or less uncompressed. Carefully crafting the site by hand to make the user experience excellent. Doing things close to the metal meant something. Handwritten HTML. Barely any javascript.
Using LLMs to create pages like this feels so far away from the metal. My goal was to use as few resources as possible to create a grand user experience, now I have the option to set my LLM to thrash servers overnight to do the same thing. It leaves a sour taste in my mouth.
On top of this I'm racked with the ethical concerns. Water and power usage. IP issues. GPU availability. I left Twitter because it was making me complicit in great injustices. What is the ethical weight of vibe coding? Creating on the web is a great power akin to publishing newspapers in the 1800s. This sort of power needs to be wielded with strong ethics. We've seen what happens when it's not.