Last active
September 26, 2017 10:39
-
-
Save almino/227707baa21037fbe3c4bdd95308515c to your computer and use it in GitHub Desktop.
Topics resource
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Route::get('/topics', function() { | |
return new TopicCollection(Topic::paginate()); | |
})->name('topics'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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, | |
]; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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