CreateaLaravelandAngularSinglePageCommentApplication|Scotch
(/)
158
Tutorials (https://scotch.io/category/tutorials)
laravel
FAVORIT
WPFPACTION=ADD&
61
shares
30
31
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
1/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
VIEW CODE
(HTTPS://GITHUB.COM/SCOTCHIO/LARAVEL-ANGULARCOMMENT-APP)
30
31
AD
AngularJSexperts
UKbasedAngularJSdevelopersCallusforafreeconsultation
VIEW CODE
(HTTPS://GITHUB.COM/SCOTCH-
AdvertiseHere(http://buysellads.com/buy/detail/219041/zone/1300582?
IO/LARAVEL-ANGULARutm_source=site_219041&utm_medium=website&utm_campaign=imagetext&utm_content=zone_1300582)
COMMENT-APP)
OUTLINE
and
What WellLaravel
Be Building
Angular have
(//srv.buysellads.com/a
(//srv.buysellads.com/ads/click/x/G
(//srv.buy
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
(//srv.buysellads.com/ads/click/x/G
(//srv.buy
2/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
(https://www.fa
(https://tw
(http
What Well Be
Building
(https://scotch.io/boo
your-first-node-js-
(https://scotch.io/books
your-first-node-js-ap
Email Address
Subscribe
POPULAR ON SCOT
Whats New in B
3/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
(https://scotch.io
talk/whats-new-
comments
Ability to create a
comment and see it
bootstrap-4)
Getting Started
An Interactive G
(https://scotch.io/tutorials/get
page refresh
started-with-vim-an-interactiv
Ability to delete a
comment and see it
removed from our list
w/o page refresh
Announcing Sco
Vagrant LAMP S
Improved
(https://scotch.io/bar-talk/ann
scotch-box-2-0-our-dead-simp
vagrant-lamp-stack-improved
How to Build a W
Plugin (part 1)
(https://scotch.io/tutorials/ho
build-a-wordpress-plugin-par
together.
A Lightweight Al
to AngularJS
(https://scotch.io/tutorials/bu
app-with-vue-js-a-lightweightalternative-to-angularjs)
Building a Slack
(https://scotch.io/tutorials/bu
slack-bot-with-node-js-and-ch
norris-super-powers)
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
4/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
(https://cask.scotch.io/2014/
02/laravel-angular-single-
(Part 1)
page-application1.jpg)
(https://scotch.io/tutorials/bu
etsy-clone-with-angular-and-s
The Laravel
Backend
part-1)
Setting Up Laravel
Go ahead and get your
Laravel setup ready. Well be
doing some basic things to
get our backend to do CRUD
on comments:
Create a database
migration
Seed our database with
sample comments
Create our routes for
our API
Creating a catch-all
route to let Angular
handle routing
Creating a resource
controller for comments
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
(//srv.buysellads.com/ads/click/x/GT
AdvertiseHere
(http://buysellads.com/buy/detail/21904
5/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
(http://bit.ly/1FxJ
phpartisanmigrate:make
A VAGRANT LAMP ST
THAT JUST WORK
create_comments_table
create=comments
6/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
timestamps so that we
know how long ago the
comment was made. Here is
the code for the comments
table:
//app/database/migrations/####_##_##_######_create_comments_table.php
...
/**
*Runthemigrations.
*
*@returnvoid
*/
15
16
publicfunctionup()
{
Schema::create('comments',function
{
$table>increments('id');
(http://bit.ly/1PY0
DEAD SIMPLE
OFF-CANVAS PANE
$table>string('text');
$table>string('author');
$table>timestamps();
});
}
...
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
7/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
(http://bit.ly/1QJLL
SCOTCH.IO STICKER
(https://cask.scotch.io/2014/
(http://shop.scotc
02/laravel-angularmigrate.jpg)
With our table made, lets
create an Eloquent model so
that we can interact with it.
Comment Model
AngularJS exper
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
8/57 IN
AUTOMATED ONLINE
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
AUTOMATED ONLINE IN
Eloquent
(http://laravel.com/docs/elo
quent) models to interact
with our database. This will
be very easy to do. Lets
create a model:
app/models/Comment.php .
(https://invoicebus.com
aff=8hlimbxx)
<?php
//app/models/Comment.php
(https://www.facebook
(https://twitter.co
(https://feed
classCommentextendsEloquent{//leteloquentknowthattheseattribu
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
9/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
10/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
<?php//app/database/seeds/CommentTableSeeder.php
classCommentTableSeederextendsSeeder{
publicfunctionrun()
{
DB::table('comments')>delete();
Comment::create(array(
'author'=>'ChrisSevilleja',
'text'=>'LookIamatestcomment.'
));
Comment::create(array(
'author'=>'NickCerminara',
'text'=>'Thisisgoingtobesupercrazy.'
));
Comment::create(array(
'author'=>'HollyLloyd',
'text'=>'IamamasterofLaravelandAngular.'
));
}
}
following:
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
11/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
//app/database/seeds/DatabaseSeeder.php
...
/**
*Runthedatabaseseeds.
*
*@returnvoid
*/
publicfunctionrun()
13
{
Eloquent::unguard();
$this>call('CommentTableSeeder');
$this>command>info('Commenttableseeded.'
}
...
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
12/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
(https://cask.scotch.io/2014/
02/laravel-angular-databaseseed.jpg)
Now we have a database
with a comment table, an
Eloquent model, and
samples in our database.
Not bad for a days work
but were not even close to
done yet.
Comment Resource
Controller
app/controllers/CommentController.php
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
13/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
14/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
(https://cask.scotch.io/2014/
02/laravel-angular-createcontroller.jpg)
Now weve created our
controller. We dont need
the create and edit
functions because Angular
will be handling showing
those forms, not Laravel.
Laravel is just responsible
for sending data back to our
frontend. We also took out
the update function for this
demo just because we want
to keep things simple. Well
handle creating, showing,
and deleting comments.
To send data back, we will
want to send all our data
back as JSON. Lets go
through our newly created
controller and fill out our
functions accordingly.
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
15/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
<?php//app/controllers/CommentController.php
classCommentControllerextends\BaseController
/**
*SendbackallcommentsasJSON
*
*@returnResponse
*/
publicfunctionindex()
{
returnResponse::json(Comment::get(
}
/**
*Storeanewlycreatedresourceinstorage.
*
*@returnResponse
*/
publicfunctionstore()
{
Comment::create(array(
'author'=>Input::get('author'
'text'=>Input::get('text')
));
returnResponse::json(array('success'
}
/**
*Removethespecifiedresourcefromstorage.
*
*@paramint$id
*@returnResponse
*/
publicfunctiondestroy($id)
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
16/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
publicfunctiondestroy($id)
{
Comment::destroy($id);
returnResponse::json(array('success'
}
17/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
18/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
URL:
http://example.com/api/com
ments . This just makes sense
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
19/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
<?php//app/routes.php
//HOMEPAGE===================================
//wedontneedtouseLaravelBlade
//wewillreturnaPHPfilethatwillholdallofourAngularcontent
//seethe"WheretoPlaceAngularFiles"belowtoseeideasonhowtos
Route::get('/',function(){
View::make('index');//willreturnapp/views/index.php
});
//APIROUTES==================================
Route::group(array('prefix'=>'api'),function
//sincewewillbeusingthisjustforCRUD,wewon'tneedcreatea
//Angularwillhandlebothofthoseforms
//thisensuresthatausercan'taccessapi/createorapi/editwhen
Route::resource('comments','CommentController'
array('only'=>array('index','store'
});
//CATCHALLROUTE=============================
//allroutesthatarenothomeorapiwillberedirectedtothefronten
//thisallowsangulartoroutethem
App::missing(function($exception){
returnView::make('index');
});
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
20/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
Handling Catch-All
Routes: In Laravel, you
can do this a few ways.
Usually it isnt ideal to do
the above code and have
a catch-all for your entire
application. The
alternative is that you
can use Laravel
Controller Missing
Methods
(http://laravel.com/docs/controllers#handlingmissing-methods) to
catch routes.
Testing All Our Routes
Lets make sure we have all
the routes we need. Well
use artisan and see all our
routes:
phpartisanroutes
21/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
(https://cask.scotch.io/2014/
02/laravel-angular-artisanroutes.jpg)
We can see the HTTP verb
and the route used to get all
comments, get a single
comment, create a
comment, and destroy a
comment. On top of those
API routes, we can also see
how a user get routed to our
Angular application by the
home page route.
Backend Done
Finally! Our Laravel API
backend is done. We have
done so much and yet,
theres still so much to do.
We have set up our
database and seeded it,
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
22/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
Where to Place
Angular Files
Ive seen this question asked
a lot. Where exactly should I
be putting Angular files and
how does Laravel and
Angular work together. We
did an article on getting
Laravel Blade and Angular to
work together
(https://scotch.io/bartalk/quick-tip-using-laravelblade-with-angularjs). This
article works under the
assumption that we arent
even going to use Blade.
To let Angular handle the
frontend, we will need
Laravel to pass our user to
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
23/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
//app/routes.php
Route::get('/',function(){
returnView::make('index');
});
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
24/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
//app/config/view.php
...
//makelaravellookinpublic/viewsforviewfiles
'paths'=>array(__DIR__.'/../../public/views'
...
Now return
View::make('index') will
look for
public/views/index.php . It
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
25/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
forward.
26/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
The Angular
Frontend
Getting Our
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
27/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
Getting Our
Application Ready
Everything for our Angular
application will be handled
in the public folder. This
lets us keep a good
separation of the backend in
the app folder.
Lets look at the application
structure we will have in our
public folder. Weve
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
28/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
public/
js/
controllers///wherewewillputourangularcontrollers
mainCtrl.js
services///angularservices
commentService.js
app.js
Angular Service
public/js/services/commentService.js
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
29/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
//public/js/services/commentService.js
angular.module('commentService',[])
.factory('Comment',function($http){
return{
//getallthecomments
get:function(){
return$http.get('/api/comments'
},
//saveacomment(passincommentdata)
save:function(commentData){
return$http({
method:'POST',
url:'/api/comments',
headers:{'ContentType':
data:$.param(commentData)
});
},
//destroyacomment
destroy:function(id){
return$http.delete('/api/comments/'
}
}
});
30/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
Angular Controller
public/js/controllers/mainCtrl.js
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
31/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
32/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
//public/js/controllers/mainCtrl.js
angular.module('mainCtrl',[])
//injecttheCommentserviceintoourcontroller
.controller('mainController',function($scope
//objecttoholdallthedataforthenewcommentform
$scope.commentData={};
//loadingvariabletoshowthespinningloadingicon
$scope.loading=true;
//getallthecommentsfirstandbindittothe$scope.commentsobj
//usethefunctionwecreatedinourservice
//GETALLCOMMENTS==============
Comment.get()
.success(function(data){
$scope.comments=data;
$scope.loading=false;
});
//functiontohandlesubmittingtheform
//SAVEACOMMENT================
$scope.submitComment=function(){
$scope.loading=true;
//savethecomment.passincommentdatafromtheform
//usethefunctionwecreatedinourservice
Comment.save($scope.commentData)
.success(function(data){
//ifsuccessful,we'llneedtorefreshthecommentlist
Comment.get()
.success(function(getData
$scope.comments=getData
$scope.loading=false
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
33/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
$scope.loading=false
});
})
.error(function(data){
console.log(data);
});
};
//functiontohandledeletingacomment
//DELETEACOMMENT================================================
$scope.deleteComment=function(id){
$scope.loading=true;
//usethefunctionwecreatedinourservice
Comment.destroy(id)
.success(function(data){
//ifsuccessful,we'llneedtorefreshthecommentlist
Comment.get()
.success(function(getData
$scope.comments=getData
$scope.loading=false
});
});
};
});
34/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
Connecting Our
Application public/js/app.js
On the Angular side of
things, we have created our
service and our controller.
Now lets link everything
together so that we can
apply it to our application
using ngapp and ng
controller .
35/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
//public/js/app.js
varcommentApp=angular.module('commentApp'
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
36/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
37/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
deleteComment():
Well attach this
function to a delete link
so that we can remove
the comment
Now lets get to the actual
code for our view. Well
comment out the main
important parts so we can
see how everything works
together.
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
38/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
<!app/views/index.php>
<!doctypehtml><htmllang="en"><head><meta
<!CSS>
<linkrel="stylesheet"href="//netdna.bootstrapcdn.com/bootstrap/3.1
<linkrel="stylesheet"href="//netdna.bootstrapcdn.com/fontawesome/
<style>
body{paddingtop:30px;}
form{paddingbottom:20px;
.comment{paddingbottom:20px;
</style>
<!JS>
<scriptsrc="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min
<scriptsrc="//ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular
<!ANGULAR>
<!allangularresourceswillbeloadedfromthe/publicfolder
<scriptsrc="js/controllers/mainCtrl.js
<scriptsrc="js/services/commentService.js
<scriptsrc="js/app.js"></script><!loadourapplication>
</head>
<!declareourangularappandcontroller>
<bodyclass="container"ngapp="commentApp"
<!PAGETITLE===============================================>
<divclass="pageheader">
<h2>LaravelandAngularSinglePageApplication
<h4>CommentingSystem</h4>
</div>
<!NEWCOMMENTFORM==============================================
<formngsubmit="submitComment()"><!ngsubmitwilldisablethed
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
39/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
<formngsubmit="submitComment()"><!ngsubmitwilldisablethed
<!AUTHOR>
<divclass="formgroup">
<inputtype="text"class="formcontrolinputsm
</div>
<!COMMENTTEXT>
<divclass="formgroup">
<inputtype="text"class="formcontrolinputlg
</div>
<!SUBMITBUTTON>
<divclass="formgrouptextright">
<buttontype="submit"class="btnbtnprimarybtnlg
</div>
</form>
<!LOADINGICON===============================================
<!showloadingiconiftheloadingvariableissettotrue>
<pclass="textcenter"ngshow="loading
<!THECOMMENTS===============================================
<!hidethesecommentsiftheloadingvariableistrue>
<divclass="comment"nghide="loading"
<h3>Comment#{{comment.id}}<small
<p>{{comment.text}}</p>
<p><ahref="#"ngclick="deleteComment(comment.id)
</div>
</div>
</body>
</html>
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
40/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
(https://cask.scotch.io/2014/
02/laravel-angular-singlepage-application1.jpg)
Now we finally have our
view that brings all of the
parts we created together.
You can go ahead and play
around with the application.
All the parts should fit
together nicely and creating
and deleting comments
should be done without a
page refresh.
Testing the
Application
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
41/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
Conclusion
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
42/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
Conclusion
Hopefully this tutorial gives
a good overview of how to
start an application using
Laravel and Angular. You
can bring this farther and
create a full application that
can handle multiple API calls
on the Laravel side, and
even create your own
Angular routing
(https://scotch.io/tutorials/ja
vascript/single-page-appswith-angularjs-routing-andtemplating) for multiple
pages.
Sound off in the comments
if you have any questions or
would like to see a specific
use case. We can also
expand on this demo and
start adding different things
like editing a comment, user
profiles, whatever.
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
43/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
VIEW CODE
(HTTPS://GITHUB.COM/SCOTCHIO/LARAVEL-ANGULARCOMMENT-APP)
(https://scotch.io/author/chris)
CHRIS SEVILLEJA
(HTTPS://SCOTCH.IO/AUTHOR/CHRIS)
(@SEVILAYHA
(HTTPS://TWITTER.COM/SEVILAYHA))
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
44/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
READ NEXT
(https://scotch.io/tutorials/setting(https://scotch.io/tutorials/creatingup-a-mean-
a-single-page-
stack-single-
todo-app-
page-
with-node-
application)
and-angular)
SETTING UP
CREATING A
A MEAN
SINGLE PAGE
STACK
TODO APP
SINGLE PAGE
WITH NODE
APPLICATION
AND
(HTTPS://SCOTCH.IO/TUTORIALS/SETTINGANGULAR
UP-A-MEAN(HTTPS://SCOTCH.IO/TUTORIALS/CREATINGSTACKA-SINGLESINGLEPAGE-TODOPAGEAPP-WITHAPPLICATION)
NODE-ANDANGULAR)
(https://scotch.io/tutorials/node(https://scotch.io/tutorials/singleand-angular-
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
45/57
9/20/2015
and-angular-
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
to-do-appapplicationorganization-
page-appswithangularjs-
NODE AND
SINGLE PAGE
ANGULAR
APPS WITH
TO-DO APP:
ANGULARJS
APPLICATION
ROUTING
ORGANIZATION
AND
AND
TEMPLATING
STRUCTURE
(HTTPS://SCOTCH.IO/TUTORIALS/SINGLE(HTTPS://SCOTCH.IO/TUTORIALS/NODEPAGE-APPSANDWITHANGULARANGULARJSTO-DO-APPROUTINGAPPLICATIONANDORGANIZATIONTEMPLATING)
ANDSTRUCTURE)
(https://scotch.io/tutorials/understanding(https://scotch.io/tutorials/simplelaravel-route-
and-easy-
parameters)
laravel-loginauthentication)
UNDERSTANDING
SIMPLE AND
LARAVEL
EASY
ROUTE
LARAVEL
PARAMETERS
LOGIN
(HTTPS://SCOTCH.IO/TUTORIALS/UNDERSTANDINGAUTHENTICATION
LARAVEL(HTTPS://SCOTCH.IO/TUTORIALS/SIMPLEROUTEAND-EASYPARAMETERS)
LARAVELLOGINAUTHENTICATION)
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
46/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
SUBSCRIBE
FOLLOW LIKE
+1
(HTTPS://SCOTCH.IO/FEED)
(HTTP://TWITTER.COM/SCOTCH_IO)
(HTTP://WWW.FACEBOOK.COM/SCOTCHDEVELOPM
(HTTP://PLUS.GOOGLE.COM/B/113854128
Comments
Community
Recommend 3
Login
SortbyBest
Jointhediscussion
twlizard 2yearsago
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
47/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
twlizard 2yearsago
greattutorialsofar!justonequestion,Iused
yourcodefortheroutesbutwhenIcheckmy
routesfromthecommandlineIdon'thavea
rowforapi.comments.show.Iwasn'texpecting
tohavethatroutesincewedidn'tcreatea
showcontrollerbutyouhavethatroute
availableinyourscreenshot.Shouldtherebea
showcontroller?IstheresomethingI'm
missing?
22
Reply Share
DavidKnight ayearago
Onsubmitanddestroysuccess,youdon't
needtorefreshthecommentlistusing.get(),
youshouldsimply$scope.comments.push(
$scope.commentData)and
$scope.comments.splice(index,1)where
indexispassedintothedestroyfunctionasa
parameter.Thiswouldsaveyoutwoadditional
XHRrequestsandunnecessarywaitandload
time.
6
Reply Share
Hasan>DavidKnight ayearago
No,youcannotdothat,becozithasto
communicatewithrestapiallthetime.If
youpushit,it'llpushcommentwithout
commentID,sothedeletefunction
wouldnotworkwithoutcommentid.
AndI'mnotsurewhyspliceisnot
working:/
1
Reply Share
Guest>Hasan
ayearago
I'manoobinAngularJS,andI
juststartedtolearnthisfw,butI
thinkthatyouarenotrighthere.
Ithinkyoueasilycan
add\removeacommentwithout
usingextracallof
`Comment.get()`justbecause
youuseAPI.Andhowweall
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
48/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
youuseAPI.Andhowweall
know,agoodAPIallthetime
musttoreturnallneededinfo
aboutnewcreatedrecord,likea
response,andexactlythisinfo
wecanuseto`push`thenew
recordinour`commentData`.
Sorryformyenglish!
Cheers!
1
Reply Share
Jaimin ayearago
Thisisthebesttutorialforangularjsand
laravel.Iwanteditcommentlinksothatyou
caneditcommentandupdateitindatabase.
Hasanyoneusedorimplementedupdatecode
forthisone?
5
Reply Share
Kevin ayearago
ThankyoufortheGreatTutorial!Onequestion
ishowIcaninjecttheCSRFtokeninAngular
andhandlesitbyLaravel?
3
Reply Share
ChrisSevilleja
ayearago
Bartender >Kevin
Heysorryforthelateresponse.Laravel
keepsatokeninSessionthatyoucan
grabwithSession::token().Youcould
useAngulartorequestatokenwhen
creatingaformandthenbindthattoa
variableforwhenyousendthePOST.
Thecsrftokenwouldtreatthatthe
sameasanormalPOST.
Reply Share
Jaimin ayearago
Hasanyoneaddedpaginationincommentsto
paginateusingthistutorial?
2
Reply Share
NickSalloum>Jaimin
8monthsago
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
49/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
8monthsago
Haven'tdoneso,butlaraveldoesmake
paginationeasy.Here'sthedocsforit
http://laravel.com/docs/4.2/pa...might
beaneasyimplementation?
Reply Share
Alex 2yearsago
Thanksforthatnicelittletutorial.Itallworked
straightaway(veryrarewithme)andiam
nowinspiredtocontinuedigginginthat
direction.Cheersandkeepupthegoodwork.
2
Reply Share
waseemmachloy 2yearsago
whatisbenefitforusingangular,jsinthis
commentsystem.:)justcuriousabout
angula.jsbenefit
2
Reply Share
Thisexampledoesn'tfullyshowthe
powerthatAngularcanprovideinthis
setup.HandlingtheAJAXcallsinan
Angularserviceandhavingseparated
codeoutintocontrollersletsusexpand
ourappeasierthanifwewerecreating
thiswithjustjQuery.
Sincetheserviceandcontrollerare
separateandmodular,wecanrununit
testsonourapptomakesurethat
everythingworksaccordingtoplan.
Whilethisdoesn'tshowtoomuchofthe
benefits,whenyouwanttobuildalarger
application,itcanhelpgreatlyfor
testability,scalabilityofcode,and
handlingthefrontenddataissomuch
easier.
2
Reply Share
JeroenKnockaert amonthago
Ihavefollowedeverysinglestepinthistutorial
andi'mgettingthiserror:
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
50/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
andi'mgettingthiserror:
"Useofundefinedconstantcomment
assumed'comment'"
HowcanIfixthis?
1
Reply Share
Sam>JeroenKnockaert
23daysago
Ihavethesameproblem.Didyoufigure
outwhatitis?
Reply Share
JeroenKnockaert>Sam
20daysago
I'mnotsureifmysolutionwas
therightwaytosolvethis,I
typed'@'beforeeveryvariable.
Forexample:@{{comment.id}}
insteadof{{comment.id}}
Thissolvedtheproblemforme
1
Reply Share
Sam>Jeroen
Knockaert
15daysago
Thanks!Thatworked.
Reply
Share
FlorianZemke 7monthsago
IcreatedaLaravel5andAngularJSstarter
boilerplateprojectgettingyoustartedwiththe
newLaravel5andAngularJSwithautomated
routingandmanyfeaturesthatwillgetyou
startedquickly.
https://github.com/Zemke/start...
1
Reply Share
FlorianZemke>FlorianZemke
5monthsago
Authenticationhasbeenadded.
Reply Share
Guest>FlorianZemke
6monthsago
well,igotthiserror
ErrorExceptioninhelpers.phpline
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
51/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
ErrorExceptioninhelpers.phpline
619:file_get_contents(/home/chabib/Code/starter
laravelangular/public/build/rev
manifest.json):failedtoopenstream:
Nosuchfileordirectory(View:
/home/chabib/Code/starterlaravel
angular/resources/views/layout.blade.php)
Reply Share
FlorianZemke>Guest
6monthsago
Hey,thankyoufortryingoutthe
project!Pleasefollowthe
installationinstructionsinthe
readme.Runningthecommands
willfixyourproblem.Running
`gulp`shouldfixyourproblem.
Havefun!
https://github.com/Zemke/start...
Reply Share
Guest>FlorianZemke
6monthsago
sweet,thisiswhatiamlookingfor
thanks
Reply Share
FlorianZemke>Guest
6monthsago
Happytohearthis!Ifyouhave
anysuggestions,questionsor
something,pleasereachoutto
meoropenanissue.Havefun!
Reply Share
Samfrombtheeuwes.com ayearago
Pleasenotetomakethedeletefunctionwork,
intheHTMLcode,use:
Delete
Note'datangclick'.Thanksforthetutorial!
1
Reply Share
DaveRev ayearago
Hi!Greatapp,it'salsoveryusefulto
understandandtestangularwithlaravel!
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
52/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
understandandtestangularwithlaravel!
I'mtryingtoimplementalsotheupdatemethod,
inlaravelandinangularservice,butIcan'tget
itworking!Canyouhelpmeplease?Ialso
haveabitbucketrepowithallmytests.
Thanks!
*Edit:isolved!:)
1
Reply Share
RubenKuipers>DaveRev
7monthsago
Canyoutellmehowyoudidit?I'm
currentlytryingtoimplementthatupdate
featuremyself,butIcan'treallygetmy
headaroundit.
Reply Share
EricLeroy 2yearsago
GreattutorialbutIalwaysgetthesameissue:
GEThttp://localhost:8888/api/comments404
(NotFound)
Unabletoviewexistingcomments.Thesame
whentryingtosaveacomment
Anyidea?
1
Reply Share
Fromyourcommandline,whenyou
typephpartisanrouteswhatroutesdo
yousee?
1
Reply Share
EricLeroy>ChrisSevilleja
2yearsago
Thesameasyou:
++
++
++
+
|Domain|URI|Name|Action|
BeforeFilters|AfterFilters|
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
53/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
BeforeFilters|AfterFilters|
++
++
++
+
||GET/||Closure|||
||GETapi/comments|
api.comments.index|
CommentController@index|||
||POSTapi/comments|
api.comments.store|
CommentController@store|||
||GET
api/comments/{comments}|
api.comments.show|
CommentController@show|||
||DELETE
api/comments/{comments}|
api.comments.destroy|
CommentController@destroy||
|
PleasenotethatI'musing
MAMPandthattherootfolderis
localhost:8888/myapp/public(as
forallLaravelapps).
++
++
++
+
1
Reply Share
ChrisSevilleja
Bartender >Eric
Leroy 2yearsago
Whenyoudo
Comment.get(),addan
errorintheresoyoucan
seeifanerrorcomes
backintheconsole.
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
54/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
Comment.get()
.success(function(data){
$scope.comments=data;
$scope.loading=false;
})
.error(function(data){
console.log(data);
});
Also,canyougotothe
routedirectlyinthe
browser?
http://localhost:8888/myapp/public
1
Reply
Share
EricLeroy>Chris
Sevilleja
2yearsago
Trappingerror:nothing
intheconsole.
With:
http://localhost:8888/larang/public/api/comments
Igetthejson:
[{"id":"1","text":"LookIam
atest
comment.","author":"Chris
Sevilleja","created_at":"2014
0212
22:40:17","updated_at":"2014
021222:40:17"},
{"id":"2","text":"Thisis
goingtobesuper
crazy.","author":"Nick
Cerminara","created_at":"2014
0212
22:40:17","updated_at":"2014
021222:40:17"},
{"id":"3","text":"Iama
masterofLaraveland
Angular.","author":"Holly
Lloyd","created_at":"2014
0212
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
55/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
0212
22:40:17","updated_at":"2014
021222:40:17"}]
Itisasif
CommentService.jswas
notcalledordidnotcall
routes.php
1
Reply
Share
ChrisSevilleja
Bartender >Eric
Leroy 2yearsago
IfthatistheURLyou
seedataat,thenusethat
URLinyour
CommentService.Since
therearenoconsole
errorsthentherearen't
anyerrorsloadingupthe
CommentService.
Tryusingthe
/larang/public/api/comments
astheURL.
1
Reply
(https://www.facebook.com/scotchdevelopment)
(https://twitter.com/scotch_io)
(https://plus.google.com/+ScotchIo/posts)
(http://feeds.feedblitz.com/scotch_io)
License (/license)
Advertise with Us (/advertise)
About (/about)
Join Us on Slack (https://scotch-slack.herokuapp.com/)
Store (http://shop.scotch.io)
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
56/57
9/20/2015
CreateaLaravelandAngularSinglePageCommentApplication|Scotch
Hire Us (http://bit.ly/18ib8jR)
(https://leanpub.com/mean-machine)
https://scotch.io/tutorials/createalaravelandangularsinglepagecommentapplication
57/57