0

I am new to Laravel and as I was watching beginner's course of Laravel an author used protected in a model that he created. Here is the code:

class PostsAdmin extends Model
{
    protected $table='posts';  
    protected $primaryKey='id'; 
}

So, it is unclear for me why to use protected in above cases. Guys I understand the meaning of protected but I cannot understand why protected is used in model in Laravel .

Mirasan
  • 259
  • 1
  • 4
  • 16
  • Possible duplicate of [What is the difference between public, private, and protected?](https://stackoverflow.com/questions/4361553/what-is-the-difference-between-public-private-and-protected) – Mike Aug 16 '18 at 04:15
  • because the Model(Parent) need to access PostAdmin(Child) properties – azisuazusa Aug 16 '18 at 04:16
  • @AzisAbdulBachar, But I think it is possible to not use protected and just write $table='posts'; and $primaryKey='id'; if not can you clarify – Mirasan Aug 16 '18 at 04:27
  • *"I understand the meaning of protected but I cannot understand why protected is used"* - You honestly don't seem to understand if you're asking this question. I would suggest reading the answers to the above link again to get a better understanding of all three. You use public/protected/private properties to control where in your program these properties, methods or constants are visible. If you omit them, they are given `public` visibility. – Mike Aug 16 '18 at 04:30
  • @Mike, ok Mike I got it – Mirasan Aug 16 '18 at 05:17
  • @Mirasan That's almost correct. Giving it protected visibility makes it accessible from that class, any classes that it extends (parent) and any class that extends it (child). This is not something specific to Laravel, but PHP in general as well as other languages that use OOP. – Mike Aug 16 '18 at 05:23

1 Answers1

0

Laravel use protected to protect you from a breach into your database.. Like an Sql injection. It also contains statements that escape any threat a user will pass through your forms

  • 2
    You're thinking of mass assignment protection, this is the scope modifer `protected`, which is states that an instance variable is accessible to itself and children, but not accessible to outside classes. – Michael Ryan Soileau Jul 28 '20 at 20:07