PHP code example of engageinteractive / laravel-view-models
1. Go to this page and download the library: Download engageinteractive/laravel-view-models 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/ */
engageinteractive / laravel-view-models example snippets
namespace App\Domain\Posts;
use EngageInteractive\LaravelViewModels\Mapper;
use App\Domain\Posts\Post;
class PostShowMapper extends Mapper
{
/**
* Map a Post to a basic PHP array.
*
* @param \App\Domain\Posts\Post
* @return array
*/
public function map(Post $post)
{
return $post->only('title', 'author_name');
}
}
namespace App\Domain\Posts;
use App\Domain\Posts\Post;
use App\Domain\Posts\PostMapper;
use App\Http\Controllers\Controller;
class PostController extends Controller
{
/**
* Show a Post.
*
* @param \App\Domain\Posts\Post
* @param \App\Domain\Posts\PostMapper
* @return \Illuminate\Views\View
*/
public function show(Post $post, PostShowMapper $model)
{
return view('post.show', [
'model' => $mapper->one($post),
]),
}
}
namespace App\Domain\Posts;
use EngageInteractive\LaravelViewModels\ViewModel;
use Illuminate\Support\Str;
class PostViewModel extends ViewModel
{
protected $post;
/**
* Intialise the View ViewModel.
*
* @param \App\Domain\Posts\Post
* @return void
*/
public function __construct(Post $post): void
{
$this->post = $post;
}
/**
* Returns the post title in title-case.
*
* @return string
*/
public function postTitle(): string
{
if (!isset($this->post->title)) {
return 'Untitled';
}
return Str::title($this->post->title);
}
}
namespace App\Domain\Posts;
use App\Domain\Posts\Post;
use App\Domain\Posts\PostViewModel;
use App\Http\Controllers\Controller;
class PostController extends Controller
{
/**
* Show a Post.
*
* @param \App\Domain\Posts\Post
* @return \Illuminate\Views\View
*/
public function show(Post $post)
{
$model = new PostViewModel($post);
return view('post.show', $model->array()),
}
}
namespace App\Domain\Posts;
use EngageInteractive\LaravelViewModels\Mapper;
use App\Domain\Posts\Post;
class PostsMapper extends Mapper
{
/**
* Map a Post to a basic PHP array.
*
* @param \App\Domain\Posts\Post
* @return array
*/
public function map(Post $post)
{
return $post->only('title', 'author_name');
}
}
namespace App\Domain\Posts;
use EngageInteractive\LaravelViewModels\ViewModel;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Str;
class PostArchiveViewModel extends ViewModel
{
protected $posts;
/**
* Initialise the View model.
*
* @param \Illuminate\Database\Eloquent\Collection
* @return void
*/
public function __construct(Collection $posts): void
{
$this->posts = $posts;
}
/**
* Returns an array of posts.
*
* @return string
*/
public function posts(): array
{
return (new PostsMapper)->all($this->posts);
}
/**
* Returns the application home URI.
*
* @return string
*/
public function HomeUri(): string
{
return route('home');
}
}
namespace App\Domain\Posts;
use App\Domain\Posts\Post;
use App\Domain\Posts\PostArchiveViewModel;
use App\Http\Controllers\Controller;
class PostArchiveController extends Controller
{
/**
* Show a Post.
*
* @return \Illuminate\Views\View
*/
public function show()
{
$posts = Post::all();
$model = new PostArchiveViewModel($posts);
return view('post-archive.show', $model->array()),
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.