This design feedback application will allow designers to upload and share their work and get feedback in realtime using VueJs and Pusher.
In this tutorial, we are going to create a design feedback application using VueJs and Pusher. This will allow you to upload images, then send the link to someone else to leave feedback on your design that you can see in realtime.
Companies like Invision have built applications that designers use to get feedback from other people. A designer can just load the Invision application, upload their designs and send the link to the people that will leave feedback, and then they can leave their feedback on different parts of the design. This is good for the designer because they can see this feedback and act on them.
Here is a screen recording of what our application will be able to do once we are done building it:
Before we get started we need to have a few things in place first. Some of the requirements are as follows:
– Knowledge of PHP & the Laravel framework.
– Knowledge of JavaScript (ES6).
– Knowledge of Vue.js.
– PHP 7.0+ installed locally on your machine.
– Laravel CLI installed locally.
– Composer installed locally.
– NPM and Node.js installed locally.
– A Pusher application. Create one on pusher.com.
Once you have verified you have the above requirements we can start creating our application.
Let us get started with setting up our application. Create a new Laravel application using the command below:
1$ laravel new your_application_name
When the installation is complete, cd
to the application directory. Open the .env
file and let us make a couple of changes in the file.
Setting up our Database and Migrations
The first thing to do is set up our database and create its migrations. Let us start by setting up the database. Replace the configuration items below:
1DB_CONNECTION=mysql 2 DB_HOST=127.0.0.1 3 DB_PORT=3306 4 DB_DATABASE=homestead 5 DB_USERNAME=homestead 6 DB_PASSWORD=secret
with:
1DB_CONNECTION=sqlite
This will now make the application use SQLite as the database choice. In your terminal, run the command below to create a new SQLite database:
1$ touch database/database.sqlite
Now let us create some migrations which will create the required tables to the database. In your terminal, run the following command to create the migrations we will need:
1$ php artisan make:model Photo --migration --controller 2 $ php artisan make:model PhotoComment --migration
The above command will create a model and then the --migration
and --controller
flags will instruct it to create a migration and a controller alongside the model.
For now, we are interested in the Model and the migration. Open the two migration files created in the ./database/migrations
directory. Let us first edit the CreatePhotosTable
class. Replace the content of the up
method with the following below:
1public function up() 2 { 3 Schema::create('photos', function (Blueprint $table) { 4 $table->increments('id'); 5 $table->string('url')->unique(); 6 $table->string('image')->unique(); 7 $table->timestamps(); 8 }); 9 }
This will create the photos
table when the migrations are run using the artisan command. It will also create new columns inside the table as specified above.
Open the second migration class, CreatePhotoCommentsTable
, and replace the up
method with the contents below:
1public function up() 2 { 3 Schema::create('photo_comments', function (Blueprint $table) { 4 $table->increments('id'); 5 $table->unsignedInteger('photo_id'); 6 $table->text('comment'); 7 $table->integer('top')->default(0); 8 $table->integer('left')->default(0); 9 $table->timestamps(); 10 11 $table->foreign('photo_id')->references('id')->on('photos'); 12 }); 13 }
This will create the table photo_comments
when the migration is run and also will create a foreign key to the photos
table.
Now go to your terminal and run the command below to run the migrations:
1$ php artisan migrate
This should now create the database tables.
Setting up the models
Now that we have run our migrations, we need to make some changes to our model file so that it can work better with the table.
Open the Photo
model and replace the contents with the following:
1<?php 2 namespace App; 3 4 use Illuminate\Database\Eloquent\Model; 5 6 class Photo extends Model 7 { 8 protected $with = ['comments']; 9 10 protected $fillable = ['url', 'image']; 11 12 public function comments() 13 { 14 return $this->hasMany(PhotoComment::class); 15 } 16 }
In the above, we have added the fillable
property which stops us from having mass assignment exceptions when trying to update those columns using Photo::create
. We also set the with
property which just eager loads the comments
relationship.
We have defined an Eloquent relationship comments
that just says the Photo
has many PhotoComments
.
Open the PhotoComment
model and replace the contents with the following:
1<?php 2 namespace App; 3 4 use Illuminate\Database\Eloquent\Model; 5 6 class PhotoComment extends Model 7 { 8 protected $fillable = ['photo_id', 'comment', 'top', 'left']; 9 10 protected $appends = ['position']; 11 12 public function getPositionAttribute() 13 { 14 return [ 15 'top' => $this->attributes['top'], 16 'left' => $this->attributes['left'] 17 ]; 18 } 19 }
Just like the Photo
model, we have defined the fillable
property. We also use Eloquent accessors to configure a new property called position
which is then appended because we specified that in the appends
property.
Setting up the frontend for our application
The next thing we want to do is set up the frontend of our application. Let us start by installing a few NPM packages that we will need in the application. In your Terminal app, run the command below to install the needed packages:
1$ npm install --save laravel-echo pusher-js vue2-dropzone@^2.0.0 2 $ npm install
This will install Laravel Echo, the Pusher JS SDK and vue-dropzone. We will need these packages to handle realtime events later.
When the packages have been installed successfully, we can now start adding some HTML and JavaScript.
Open the ./routes/web.php
file and let’s add some routes. Replace the contents of the file with the contents below:
1<?php 2 3 Route::post('/feedback/{image_url}/comment', 'PhotoController@comment'); 4 Route::get('/feedback/{image_url}', 'PhotoController@show'); 5 Route::post('/upload', 'PhotoController@upload'); 6 Route::view('/', 'welcome');
In the code above, we have defined a few routes. The first one will be handling POST
ed feedback. The second route will display the image that is to receive feedback. The third route will handle uploads and the final route will display the homepage.
Now open the ./resources/views/welcome.blade.php
file and in there replace the contents with the following HTML code:
1<!doctype html> 2 <html lang="{{ app()->getLocale() }}"> 3 <head> 4 <meta charset="utf-8"> 5 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 6 <meta name="viewport" content="width=device-width, initial-scale=1"> 7 <meta name="csrf-token" content="{{csrf_token()}}"> 8 <title>Upload to get Feedback</title> 9 <link href="https://fonts.googleapis.com/css?family=Roboto:400,600" rel="stylesheet" type="text/css"> 10 <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> 11 <link rel="stylesheet" href="{{ asset('css/app.css') }}"> 12 </head> 13 <body> 14 <div id="app"> 15 <div class="flex-center position-ref full-height"> 16 <div class="content"> 17 <uploadarea></uploadarea> 18 </div> 19 </div> 20 </div> 21 <script src="js/app.js"></script> 22 </body> 23 </html>
This is a simple HTML document, and if you look closely, you will see a reference to an uploadarea
tag which does not exist in HTML but is a Vue component.
Open the ./resources/assets/sass/app.scss
file and paste the following code below the import statements:
1html, body { 2 background-color: #fff; 3 color: #636b6f; 4 font-family: 'Roboto', sans-serif; 5 font-weight: 100; 6 height: 100vh; 7 margin: 0; 8 } 9 .full-height { 10 height: 100vh; 11 } 12 .flex-center { 13 align-items: center; 14 display: flex; 15 justify-content: center; 16 } 17 .position-ref { 18 position: relative; 19 } 20 .content { 21 text-align: center; 22 } 23 .m-b-md { 24 margin-bottom: 30px; 25 } 26 .dropzone.dz-clickable { 27 width: 100vw; 28 height: 100vh; 29 .dz-message { 30 span { 31 font-size: 19px; 32 font-weight: 600; 33 } 34 } 35 } 36 #canvas { 37 width: 90%; 38 margin: 0 auto; 39 img { 40 width: 100%; 41 } 42 } 43 .modal { 44 text-align: center; 45 padding: 0!important; 46 z-index: 9999; 47 } 48 .modal-backdrop.in { 49 opacity: 0.8; 50 filter: alpha(opacity=80); 51 } 52 .modal:before { 53 content: ''; 54 display: inline-block; 55 height: 100%; 56 vertical-align: middle; 57 margin-right: -4px; 58 } 59 .modal-dialog { 60 display: inline-block; 61 text-align: left; 62 vertical-align: middle; 63 } 64 .image-hotspot { 65 position: relative; 66 > img { 67 display: block; 68 height: auto; 69 transition: all .5s; 70 } 71 } 72 .hotspot-point { 73 z-index: 2; 74 position: absolute; 75 display: block; 76 span { 77 position: relative; 78 display: flex; 79 justify-content: center; 80 align-items: center; 81 width: 1.8em; 82 height: 1.8em; 83 background: #cf00f1; 84 border-radius: 50%; 85 animation: pulse 3s ease infinite; 86 transition: background .3s; 87 box-shadow: 0 2px 10px rgba(#000, .2); 88 &:after { 89 content: attr(data-price); 90 position: absolute; 91 bottom: 130%; 92 left: 50%; 93 color: white; 94 text-shadow: 0 1px black; 95 font-weight: 600; 96 font-size: 1.2em; 97 opacity: 0; 98 transform: translate(-50%, 10%) scale(.5); 99 transition: all .25s; 100 } 101 } 102 svg { 103 opacity: 0; 104 color: #cf00f1; 105 font-size: 1.4em; 106 transition: opacity .2s; 107 } 108 &:before, 109 &:after { 110 content: ''; 111 position: absolute; 112 top: 0; 113 left: 0; 114 width: 100%; 115 height: 100%; 116 border-radius: 50%; 117 pointer-events: none; 118 } 119 &:before { 120 z-index: -1; 121 border: .15em solid rgba(#fff, .9); 122 opacity: 0; 123 transform: scale(2); 124 transition: transform .25s, opacity .2s; 125 } 126 &:after { 127 z-index: -2; 128 background:#fff; 129 animation: wave 3s linear infinite; 130 } 131 &:hover{ 132 span { 133 animation: none; 134 background: #fff; 135 &:after { 136 opacity: 1; 137 transform: translate(-50%, 0) scale(1); 138 } 139 } 140 svg { 141 opacity: 1; 142 } 143 &:before { 144 opacity: 1; 145 transform: scale(1.5); 146 animation: borderColor 2s linear infinite; 147 } 148 &:after { 149 animation: none; 150 opacity: 0; 151 } 152 } 153 } 154 @-webkit-keyframes pulse{ 155 0%, 100% { transform: scale(1); } 156 50% { transform: scale(1.1); } 157 } 158 @keyframes pulse{ 159 0%, 100% { transform: scale(1); } 160 50% { transform: scale(1.1); } 161 } 162 .popover { 163 min-width: 250px; 164 }
Save the file and exit. Now let’s move on to creating our Vue components.
Open the ./resources/assets/js/app.js
file and in there let us create our Vue component. In this file find the line below:
1Vue.component('example', require('./components/Example.vue'));
and replace it with:
1Vue.component('uploadarea', require('./components/UploadArea.vue')); 2 Vue.component('feedback', require('./components/FeedbackCanvas.vue'));
Now let us create our first Vue component. In the ./resources/assets/js/components
directory create a file called UploadArea.vue
. In the new file paste in the following:
1<template> 2 <dropzone ref="dropzone" id="dropzone" 3 url="/upload" 4 accepted-file-types="image/*" 5 v-on:vdropzone-success="showImagePage" 6 :headers="csrfHeader" 7 class="flex-center position-ref full-height"> 8 <input type="hidden" name="csrf-token" :value="csrfToken"> 9 </dropzone> 10 </template> 11 12 <script> 13 import Dropzone from 'vue2-dropzone'; 14 15 const LARAVEL_TOKEN = document.head.querySelector('meta[name="csrf-token"]').content 16 17 export default { 18 components: { Dropzone }, 19 data() { 20 return { 21 csrfToken: LARAVEL_TOKEN, 22 csrfHeader: { 'X-CSRF-TOKEN': LARAVEL_TOKEN } 23 } 24 }, 25 methods: { 26 showImagePage: (file, response) => { 27 if (response.url) { 28 return window.location = `/feedback/${response.url}`; 29 } 30 } 31 }, 32 mounted () { 33 this.$refs.dropzone.dropzone.on('addedfile', function (file) { 34 if (this.files.length > 1) { 35 this.removeFile(this.files[0]) 36 } 37 }) 38 } 39 } 40 </script>
In the template
section, we are simply using the Vue dropzone package to define an area through which files can be uploaded. You can view the documentation here.
In the script
section, we get the Laravel CSRF token from the header of the page and import the Dropzone
component into our current Vue component.
In the methods
property, we define a showImagePage
method that just redirects the user to the image page after the image has been successfully uploaded. In the mounted
method, we limit the dropzone file to allowing one file upload at a time.
Let us create our next Vue component. In the ./resources/assets/js/components
directory create a new file called FeedbackCanvas.vue
and paste in the following:
1<template> 2 <div class="feedback-area"> 3 <div class="content"> 4 <div id="canvas"> 5 <div class="image-hotspot" id="imghotspot"> 6 <transition-group name="hotspots"> 7 <a 8 href="#" 9 class="hotspot-point" 10 v-for="(comment, index) in image.comments" 11 v-bind:style="{ left: comment.position.left+'%', top: comment.position.top+'%' }" 12 :key="index" 13 @click.prevent 14 data-placement="top" 15 data-toggle="popover" 16 :data-content="comment.comment" 17 > 18 <span> 19 <svg class="icon icon-close" viewBox="0 0 24 24"> 20 <path d="M18.984 12.984h-6v6h-1.969v-6h-6v-1.969h6v-6h1.969v6h6v1.969z"></path> 21 </svg> 22 </span> 23 </a> 24 </transition-group> 25 <img ref="img" :src="'/storage/'+image.image" id="loaded-img" @click="addCommentPoint"> 26 </div> 27 </div> 28 </div> 29 <add-comment-modal :image="image"></add-comment-modal> 30 </div> 31 </template>
We have defined the template
for our Vue component. This is the area where the image will be displayed and where feedback will be given.
Let us break some parts of it down a little. The a
tag has a bunch of attributes set to it. The v-for
loops through each comment/feedback the image has.
The v-bind:style
applies a style
attribute to the a
tag using the left
and top
properties of the comment/feedback. We also have the :data-content
, data-toggle
and data-placement
which Bootstrap needs for its Popovers.
The img
tag has the @click
event that fires the function addCommentPoint
when an area of the image is clicked. And finally, there’s a Vue component add-comment-modal
that accepts a property image
. This component will display a form so anyone can leave a comment.
In this same file, after the closing template
tag, paste in the following code:
1<script> 2 let AddCommentModal = require('./AddCommentModal.vue') 3 4 export default { 5 props: ['photo'], 6 components: { AddCommentModal }, 7 data() { 8 return { image: this.photo } 9 }, 10 mounted() { 11 let vm = this 12 13 Echo.channel(`feedback-${this.photo.id}`) 14 .listen('.added', (e) => { 15 // Look through the comments and if no comment matches the 16 // existing comments, add it 17 if (vm.image.comments.filter((comment) => comment.id === e.comment.id).length === 0) { 18 vm.image.comments.push(e.comment) 19 $(document).ready(() => $('[data-toggle="popover"]').popover()) 20 } 21 }) 22 }, 23 created() { 24 /** Activate popovers */ 25 $(document).ready(() => $('[data-toggle="popover"]').popover()); 26 27 /** Calculates the coordinates of the click point */ 28 this.calculateClickCordinates = function (evt) { 29 let rect = evt.target.getBoundingClientRect() 30 return { 31 left: Math.floor((evt.clientX - rect.left - 7) * 100 / this.$refs.img.width), 32 top: Math.floor((evt.clientY - rect.top - 7) * 100 / this.$refs.img.height) 33 } 34 } 35 36 /** Removes comments that have not been saved */ 37 this.removeUnsavedComments = function () { 38 var i = this.image.comments.length 39 while (i--) { 40 if ( ! this.image.comments[i]['id']) { 41 this.image.comments.splice(i, 1) 42 } 43 } 44 } 45 }, 46 methods: { 47 addCommentPoint: function(evt) { 48 let vm = this 49 let position = vm.calculateClickCordinates(evt) 50 let count = this.image.comments.push({ position }) 51 52 // Show the modal and add a callback for when the modal is closed 53 let modalElem = $("#add-modal") 54 modalElem.data({"comment-index": count-1, "comment-position": position}) 55 modalElem.modal("show").on("hide.bs.modal", () => vm.removeUnsavedComments()) 56 } 57 }, 58 } 59 </script>
? The
**created**
and**mounted**
methods are hooks that are called automatically during the creation of the Vue component. You can learn about Vue lifecycle here.
In the mounted
method, we use Laravel Echo to listen to a Pusher channel. The channel name depends on the ID of the image currently being viewed. Each image will have broadcasts on a different channel based on the ID of the image.
When the added
event is triggered on the feedback-$id
channel, it looks through the available image.comments
and, if the comment broadcasted does not exist, it adds it to the comments array.
In the create
method, We activate Bootstrap popovers, define a function that calculates the coordinates of the click point, and we define a function that removes comments that have not been saved from the image.comments
array.
Under methods
we define the addCommentPoint
method which calculates the click coordinates, and then launches a new Bootstrap modal which is going to be created in the add-comment-modal
Vue component.
For Laravel Echo to work, we need to open the ./resources/assets/js/bootstrap.js
file and add the code below at the bottom of the file:
1import Echo from 'laravel-echo' 2 3 window.Pusher = require('pusher-js'); 4 5 window.Echo = new Echo({ 6 broadcaster: 'pusher', 7 key: 'PUSHER_KEY', 8 encrypted: true, 9 cluster: 'PUSHER_CLUSTER' 10 });
You should replace PUSHER_KEY
and PUSHER_CLUSTER
with the key and cluster for your Pusher application.
Now lets create our next Vue component, AddCommentModal.vue
. It is already referenced in our FeedbackCanvas.vue
Vue component.
Create an AddCommentModal.vue
file in the same directory as our other Vue components. In this file paste in the code below:
1<template> 2 <div id="add-modal" class="modal fade" role="dialog" data-backdrop="static" data-keyboard="false"> 3 <div class="modal-dialog"> 4 <div class="modal-content"> 5 <form method="post" :action="'/feedback/'+photo.url+'post'" @submit.prevent="submitFeedback()"> 6 <div class="modal-header"> 7 <h4 class="modal-title">Add Feedback</h4> 8 </div> 9 <div class="modal-body"> 10 <textarea name="feedback" id="feedback-provided" cols="10" rows="5" class="form-control" v-model="feedback" placeholder="Enter feedback..." required minlength="2" maxlength="2000"></textarea> 11 </div> 12 <div class="modal-footer"> 13 <button type="submit" class="btn btn-primary pull-right">Submit</button> 14 <button type="button" class="btn btn-default pull-left" data-dismiss="modal">Cancel</button> 15 </div> 16 </form> 17 </div> 18 </div> 19 </div> 20 </template> 21 22 <script> 23 export default { 24 props: ['image'], 25 data() { 26 return { photo: this.image, feedback: null } 27 }, 28 methods: { 29 submitFeedback: function () { 30 let vm = this 31 let modal = $('#add-modal') 32 let position = modal.data("comment-position") 33 34 // Create url and payload 35 let url = `/feedback/${this.photo.url}/comment`; 36 let payload = {comment: this.feedback, left: position.left, top: position.top} 37 axios.post(url, payload).then(response => { 38 this.feedback = null 39 modal.modal('hide') 40 vm.photo.comments[modal.data('comment-index')] = response.data 41 $(document).ready(() => $('[data-toggle="popover"]').popover()) 42 }) 43 } 44 } 45 } 46 </script>
In the template
section, we have defined a typical Bootstrap modal. In the modal form, we have attached a call to submitFeedback()
which is triggered when the form is submitted.
In the script
section, we have defined the submitFeedback()
method in the methods
property of the Vue component. This function simply sends a comment to the backend for storage. If there is a favorable response from the API, the Bootstrap modal is hidden and the comment is appended to the image.comments
array. The Bootstrap popover is then reloaded so it picks up the changes.
With that final change, we have defined all our Vue components. Open your terminal and run the command below to build your JS and CSS assets:
1$ npm run dev
Great! Now let’s build the backend.
In your terminal, enter the command below:
1php artisan make:event FeedbackAdded
This will create an event class called FeedbackAdded
. We will use this file to trigger events to Pusher when we add some feedback.This will make feedback appear in realtime to anyone looking at the image.
Open the PhotoController
class and replace the contents with the code below:
1<?php 2 namespace App\Http\Controllers; 3 4 use App\Events\FeedbackAdded; 5 use App\{Photo, PhotoComment}; 6 7 class PhotoController extends Controller 8 { 9 public function show($url) 10 { 11 $photo = Photo::whereUrl($url)->firstOrFail(); 12 13 return view('image', compact('photo')); 14 } 15 16 public function comment(string $url) 17 { 18 $photo = Photo::whereUrl($url)->firstOrFail(); 19 20 $data = request()->validate([ 21 "comment" => "required|between:2,2000", 22 "left" => "required|numeric|between:0,100", 23 "top" => "required|numeric|between:0,100", 24 ]); 25 26 $comment = $photo->comments()->save(new PhotoComment($data)); 27 28 event(new FeedbackAdded($photo->id, $comment->toArray())); 29 30 return response()->json($comment); 31 } 32 33 public function upload() 34 { 35 request()->validate(['file' => 'required|image']); 36 37 $gibberish = md5(str_random().time()); 38 39 $imgName = "{$gibberish}.".request('file')->getClientOriginalExtension(); 40 41 request('file')->move(public_path('storage'), $imgName); 42 43 $photo = Photo::create(['image' => $imgName, 'url' => $gibberish]); 44 45 return response()->json($photo->toArray()); 46 } 47 }
In the above, we have a show
method which shows an image so people can leave feedback on it. Next, there is the comment
method that saves a new comment on an image. The final method is the upload
method that simply uploads an image to the server and saves it to the database.
Let us create the view for the show
method. Create a new file in the ./resources/views
directory called image.blade.php
. In this file, paste the code below:
1<!doctype html> 2 <html lang="{{ app()->getLocale() }}"> 3 <head> 4 <meta charset="utf-8"> 5 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 6 <meta name="viewport" content="width=device-width, initial-scale=1"> 7 <meta name="csrf-token" content="{{csrf_token()}}"> 8 <title>Laravel</title> 9 <link href="https://fonts.googleapis.com/css?family=Roboto:400,600" rel="stylesheet" type="text/css"> 10 <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> 11 <link rel="stylesheet" href="{{ asset('css/app.css') }}"> 12 </head> 13 <body> 14 <div id="app"> 15 <feedback :photo='@json($photo)'></feedback> 16 </div> 17 <script src="{{asset('js/app.js')}}"></script> 18 </body> 19 </html>
In the above, the only thing that stands out is the feedback
tag and it is basically in reference to the feedback Vue component we built earlier in the article. Every other thing is just basic Blade and HTML.
Now that we have created the view, we need to add the directory for uploads defined in the upload
method. In your terminal, run the command below:
1$ php artisan storage:link
This command will create a symlink from the ./storage
directory to the ./public/storage
directory. If you look in the ./public
directory you should see the symlink.
Now that we have created the backend to support our web application, we need to add Pusher to the backend so that the comments made are broadcasted and can be picked up by other people browsing the image.
Open your terminal and enter the command below to install the Pusher PHP SDK:
1$ composer require pusher/pusher-php-server "~3.0"
Open the .env
file and scroll to the bottom and configure the Pusher keys as seen below:
1PUSHER_APP_ID="PUSHER_ID" 2 PUSHER_APP_KEY="PUSHER_KEY" 3 PUSHER_APP_SECRET="PUSHER_SECRET"
Also in the same file, look for the BROADCAST_DRIVER
and change it from log
to pusher
.
Next, open the ./config/broadcasting.php
and scroll to the pusher
key. Replace the options
key of that configuration with the code below:
1// ... 2 'options' => [ 3 'cluster' => 'PUSHER_CLUSTER', 4 'encrypted' => true 5 ], 6 // ...
? Remember to replace the
**PUSHER_ID**
,**PUSHER_KEY**
,**PUSHER_SECRET**
and**PUSHER_CLUSTER**
with the values from your Pusher application.
Now, open the FeedbackAdded
class and replace the contents with the code below:
1<?php 2 3 namespace App\Events; 4 5 use Illuminate\Broadcasting\Channel; 6 use Illuminate\Queue\SerializesModels; 7 use Illuminate\Foundation\Events\Dispatchable; 8 use Illuminate\Broadcasting\InteractsWithSockets; 9 use Illuminate\Contracts\Broadcasting\ShouldBroadcast; 10 11 class FeedbackAdded implements ShouldBroadcast 12 { 13 use Dispatchable, InteractsWithSockets, SerializesModels; 14 15 public $comment; 16 17 public $photo_id; 18 19 public function __construct(int $photo_id, array $comment) 20 { 21 $this->comment = $comment; 22 $this->photo_id = $photo_id; 23 } 24 25 public function broadcastOn() 26 { 27 return new Channel("feedback-{$this->photo_id}"); 28 } 29 30 public function broadcastAs() 31 { 32 return 'added'; 33 } 34 }
In the class above, we define the comment
object and the photo_id
which will be used to compose the channel name in the broadcastOn
method. We also define the broadcastAs
method which will allow us to customize the name of the event being sent to Pusher.
That’s all. Now, let’s run our application. In your terminal, run the code below:
1$ php artisan serve
This should start a new PHP server and you can then use that to test your application. Go to the URL given and you should see your application.
In this article, we have successfully created a prototype application’s feedback feature that will allow designers to share their designs with others and receive feedback on them. If you have questions or feedback, please leave them as a comment below.
The source code to the application is available on GitHub.