composer require effectiveactivism/sparql-client
config/packages/sparql_client.yaml:
sparql_client:
query_endpoint: "http://your-sparql-endpoint/sparql"
update_endpoint: "http://your-sparql-endpoint/sparql"
shacl_endpoint: "http://validator-endpoint/shacl" # Optional
use EffectiveActivism\SparQlClient\Client\SparQlClientInterface;
public function index(SparQlClientInterface $client) {
$client->setExtraNamespaces(['schema' => 'http://schema.org/']);
$result = $client->select(['?subject'])
->where([new Triple(new Variable('subject'), new PrefixedIri('schema', 'headline'), new PlainLiteral('Test'))])
->execute();
return $result->getRows();
}
Fetching Data:
$result = $client->select(['?article'])
->where([new Triple(new Variable('article'), new PrefixedIri('schema', 'headline'), new PlainLiteral('Laravel'))])
->execute();
Updating Data:
$client->insert([new Triple(
new Iri('urn:example:article1'),
new PrefixedIri('schema', 'headline'),
new PlainLiteral('Updated Title')
)])
->execute();
CRUD Operations:
insert() with a Triple or Quad.select() with where() clauses.replace() (DELETE+INSERT) or delete() + insert().delete() with a where() filter.Complex Queries:
$select->groupBy([new Variable('subject')])
->having(new GreaterThan(new Count(new Variable('subject')), new TypedLiteral(5)));
$subquery = $client->select([new Variable('temp')])
->where([new Triple(new Variable('temp'), new PrefixedIri('schema', 'headline'), new PlainLiteral('Subquery'))]);
$select->where([new Triple(new Variable('main'), new PrefixedIri('schema', 'related'), $subquery)]);
Graph Patterns:
$select->where([new Triple($subject, $predicate, $object)])
->optional([new Triple($subject, new PrefixedIri('schema', 'author'), new Variable('author'))]);
SparQlClientInterface in controllers/services.setExtraNamespaces() or per-query:
$client->setExtraNamespaces(['foaf' => 'http://xmlns.com/foaf/0.1/']);
execute() in try-catch:
try {
$client->execute($statement);
} catch (SparQlClientException $e) {
// Log or handle error
}
$validator = $client->getShaclValidator();
$validator->validate($data, 'http://shapes-graph');
$variables = ['?subject', '?predicate'];
$select = $client->select($variables)
->where([new Triple(new Variable('subject'), new Variable('predicate'), new Variable('object'))]);
$triples = [];
foreach ($articles as $article) {
$triples[] = new Triple(
new Iri($article['id']),
new PrefixedIri('schema', 'headline'),
new PlainLiteral($article['title'])
);
}
$client->insert($triples)->execute();
Endpoint Mismatch:
query_endpoint for updates (e.g., insert()).update_endpoint is configured for write operations.UpdateResultInterface status codes (e.g., getStatusCode()).Namespace Scope:
setExtraNamespaces() are not inherited by subqueries.Variable Binding:
select() but using them in where().select():
$select = $client->select([new Variable('subject')])->where([...]);
Literal Datatypes:
PlainLiteral vs TypedLiteral).TypedLiteral for typed values (e.g., new TypedLiteral('2023-01-01', 'xsd:date')).SHACL Validation:
shacl_endpoint and handle ShaclValidationException.$query = $statement->getQueryString();
dump($query);
limit() and offset() for pagination:
$select->limit(100)->offset(200);
where() clauses.TermInterface for domain-specific terms (e.g., CustomIri).public function findArticlesByHeadline(SparQlClientInterface $client, string $headline) {
return $client->select(['?article'])
->where([new Triple(new Variable('article'), new PrefixedIri('schema', 'headline'), new PlainLiteral($headline))])
->execute();
}
$rows = $result->getRows();
return array_map(function ($row) {
return new Article($row['article']->getIri());
}, $rows);
$client->addMiddleware(function ($statement, callable $next) {
// Pre-process statement
$result = $next($statement);
// Post-process result
return $result;
});
FROM NAMED, ensure the named graph exists in your endpoint.PATCH for updates. Configure the client to use the correct HTTP method if needed.http_client configuration if queries hang.<http://example/shapes/ArticleShape>
a sh:NodeShape ;
sh:targetClass schema:Article ;
sh:property [
sh:path schema:headline ;
sh:datatype xsd:string ;
] .
$iri = new Iri('http://example.org/resource');
if (!$iri->isValid()) {
throw new \InvalidArgumentException('Invalid IRI');
}
How can I help you explore Laravel packages today?