تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Laravel Routes and Blade0:00

Well, maybe you're thinking, what about the Laravel way of doing things? What if I want to make a controller and just pass data into my Blade templates and do things that way? Maybe you already have an application, a lot of your templates are already hooked up that way, and that's how you'd like to use it. That's totally fine, Xanadu can do that as well. Let me give you some tools on how to build your site that way. Alright, let's just open up the routes file, and we'll create a new test route to play around in. Alright, we'll return a Vue, called test, and we'll fetch some data and send it in.

Alright, we'll return a Vue, called test, and we'll fetch some data and send it in. Go to our resources and views, make a new template, we'll call it test, and we'll give it the blade.php extension. Let's come back to our browser, and here we go. So you'll see that this works just like normal, because we're literally just using Laravel here. But if you want that to be rendered inside your Statamic layout file, maybe you're mixing and matching.

Render Blade in Layout1:04

using Laravel here. But if you want that to be rendered inside your Statamic layout file, maybe you're mixing and matching antlers and Blade, we can do something like this. Pop open your layout, and yield out the name of a section, and then in that test template, we'll extend the layout, and then write all of our content here in the content section. Quick little refresh, sorry, extends,

Query Entries in Route1:36

Quick little refresh, sorry, extends, and there we go. Next, let's fetch some data. Instead of wiring up a controller, let's just work in this closure. We're going to use a StaticRepository facade, there's one for every type of data, we have Entry, and Collection, and Taxonomy, and User, and so on. So we're going to fetch some entries. So let's do, $entries = Entry::query()->where(), this is a fluent syntax.

So let's do, entries equals Entry::query(), where, this is a fluent syntax just like Eloquent, where collection is news, and then we just want to get those results and we'll pass them to the view. Next, we come back to our view and we can do, foreach, entries as entry, close the loop, get the link,

Create and Save Entries2:40

loop, get the link, we'll get the title, and we'll refresh. Look at that. That easy. This class can also create content. So if we wanted to, we can make an Entry entry make and we'll tell it the collection, in this case news give it a slug of test

give it a slug of test we'll set some data like title test title, and we'll tell it to be published as false. We'll save that entry and let's go into the control panel and I have a missing array close bracket. Alright, go into the control panel go to collections, go to news article and then we're going to hit that test URL and create that entry.

Use Antlers in Blade4:16

counting, all that kind of stuff works just like Eloquent. Now, one other thing before we move on is you can actually use antlers tags inside Blade. So instead of fetching in a controller, passing data in, replace this with a call to statemic tag, passing the name of the tag which in our case would be collection news maybe we'll limit three and then we'll scope that to an entry variable. Refresh

an entry variable. Refresh this is actually limit, not take there you go. This works for all tags there's also methods for using modifiers, fetching globals so really give you the tools you need to build your site the way you want to build your own site.

Content QueriesUsing BladeData RepositoriesCreating Entries Programmatically

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟