Introduction
There could situations in your application where you need a section to be aware of the model of the
page the user is on.
An example could be that you're building a blog. You have an Blog model in your
application which has some extra data associated with it, it could be a title, an author, and a
featured image. You also are starting to build a section to display a blog entry. So you have
routing setup so that if you go to https://fictionary.test/blog/my-first-blog-post
it pulls in
that my-first-blog-post
Blog model. Normal Laravel practice so far. But whouldn't it be nice if
the section you're building out could know about which blog model the user is on.
Model awareness
The approach to having you section be aware of the model the use is on is pretty straightforward.
All you have to do, is on your section you add the method dependsOn()
and return the class of the
model you need to work with. You section depends on a model in order to work.
So in this example you would add something like:
1class BlogPost extends Section2{3 public static function dependsOn() : string4 {5 return Blog::class;6 }
Now in your section you have access to the given blog model in your section through a new property
that Strata creates for you, which will be the camel-cased name of your model. In this case
$this->blog
. So you can do something like $this->blog->title
or $this->blog->author
.
You can of course also use this in your Blade view for the section, {{ $this->blog->title }}
.
When you use the
dependsOn()
method you will only be allow to use that section when building out that model's section in the customizer. I.e. you will not be able to use the section on for example aPage
.