Skip to content

Instantly share code, notes, and snippets.

@almino
Last active September 26, 2017 10:39
Show Gist options
  • Save almino/227707baa21037fbe3c4bdd95308515c to your computer and use it in GitHub Desktop.
Save almino/227707baa21037fbe3c4bdd95308515c to your computer and use it in GitHub Desktop.
Topics resource
Route::get('/topics', function() {
return new TopicCollection(Topic::paginate());
})->name('topics');
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\Resource;
class Topic extends Resource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
$mime = getimagesizefromstring($this->picture)['mime'];
return [
'id' => $this->id,
'name' => $this->name,
'picture' => "data:{$mime};base64," . base64_encode($this->picture),
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Topic extends Model
{
/**
* Get the sentences for the topic.
*/
public function sentences() {
$this->hasMany('App\Sentences');
}
}
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\ResourceCollection;
class TopicCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
return parent::toArray($request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment