1. Go to this page and download the library: Download edulazaro/larasources library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
$this->config('units'); // inside the source
$source->config('units'); // from outside
use Illuminate\Database\Eloquent\Model;
use EduLazaro\Larasources\Concerns\HasSources;
use App\Sources\WeatherSource;
class City extends Model
{
use HasSources;
protected array $sources = [
'weather' => WeatherSource::class,
];
}
$city = City::find(1);
// Access external data (autoloaded from cache or fetched on miss)
$weather = $city->source('weather');
echo $weather->temperature;
echo $weather->humidity;
// Push data to the external API and persist locally. Failures throw.
$city->source('weather')->save();
// Same, reporting the outcome instead of throwing
$result = $city->source('weather')->trySave();
if ($result->failed()) {
return back()->withErrors($result->message);
}
// Read live from the API and refresh the cached record
$fresh = $city->source('weather')->fetch();
// Delete remote and clear cache
$city->source('weather')->delete();
namespace App\Sources;
use EduLazaro\Larasources\Source;
use EduLazaro\Larasources\Attributes\UsesOrigin;
use App\Origins\MyProviderOrigin;
#[UsesOrigin(MyProviderOrigin::class)]
class WeatherSource extends Source
{
protected $fillable = [
'temperature',
'humidity',
'description',
];
protected $casts = [
'temperature' => 'float',
'humidity' => 'integer',
];
protected function arguments(): array
{
return [
'city_id' => 'external_id', // maps to $city->external_id
];
}
public function getFeelsLikeAttribute(): float
{
return $this->temperature - ($this->humidity / 10);
}
}
namespace App\Origins;
use EduLazaro\Larasources\Origins\Origin;
use Illuminate\Support\Facades\Http;
class MyProviderOrigin extends Origin
{
public static function getAlias(): string
{
return 'my_provider';
}
public function fetch(array $arguments = []): array
{
$response = Http::withToken($this->config('api_key'))
->get('https://api.example.com/weather/' . $arguments['city_id']);
return $response->json();
}
public function save(array $data): array
{
$response = Http::withToken($this->config('api_key'))
->post('https://api.example.com/weather', $data);
return $response->json();
}
public function delete(): bool
{
return true;
}
}
$scraped = $article->source('scraped')->fetch();
$article->source('translation')
->useArgument('scraped', $scraped) // a model, a source, or its record
->fetch(); // the origin receives the scraped payload
// Has it ever been fetched/saved? One query, looked up once per instance
if ($source->record()) {
// It is stored locally
}
// What the origin says right now, cached on the way out
$source->fetch();
// Compare what is stored with what the origin has
$stored = $source->record()->toArray(); // name, variant, origin, attributes, dates
$live = $source->fetch()->toArray(); // the fields, straight from the service
// Clear the cache for this source
$source->clear();
use EduLazaro\Larasources\Enums\OriginStatus;
use EduLazaro\Larasources\OriginResult;
public function save(array $data): array|OriginResult
{
$response = $this->http()->post($this->endpoint, $data)->throw()->json();
return new OriginResult(
status: $response['published'] ? OriginStatus::Saved : OriginStatus::Processing,
data: $response,
message: $response['reason'] ?? null,
);
}
// Where the latency is yours to spend: a scheduled command, not a page render
foreach (SourceRecord::processing()->get() as $record) {
try {
$record->toSource(ListingSource::class)->reconcile();
} catch (OriginException $e) {
// One service being down does not stop the rest
}
}
// Or on the source you already have
$listing = $property->source('listing')->reconcile();
// No model exists yet: the external id is the identity
$scraped = ListingSource::make()->setExternalId($link)->fetch();
// Whatever it turns out to be
$property = Property::create(['reference' => $scraped->reference]);
// And the row becomes that model's source
$scraped->attachTo($property);
// What is still waiting
SourceRecord::unattached()->where('name', 'ListingSource')->get();