Laravel Policies: Add Custom Methods

Поделиться
HTML-код
  • Опубликовано: 6 янв 2025

Комментарии • 14

  • @AhsanKhan89
    @AhsanKhan89 2 месяца назад +2

    Please create a project, where we can define row-level restrictions (each record level ) and inside each record field level restriction. Very helpful for large applications.

    • @LaravelDaily
      @LaravelDaily  2 месяца назад +1

      Row level is usually done with global scopes, in my experience. Field level I haven't done much, personally.

  • @Hardik_26922
    @Hardik_26922 2 месяца назад

    Very useful content

  • @Jonstonrich
    @Jonstonrich 2 месяца назад +1

    Hello Povilas. Thank you for the useful lessons. I have one question. What about the viewAny method? I understand that I need to pass a model that is tied to a policy. But in the viewAny method this model is not transmitted. Have a nice day!

    • @Jonstonrich
      @Jonstonrich 2 месяца назад +1

      Thank you for your answer. I understand that this is permission to display a list of models. I meant how to use this permission. I've already figured it out. You need to pass the class of the associated model to can $user->can(''viewAny", Model::class). Instead of a specific instance of the object

  • @iconicae8814
    @iconicae8814 2 месяца назад +1

    but how do i deal with views ? showing edit button to post owner and admin for example not anyone else ? and what is the difference between policies and gates?

    • @spicynoodle7419
      @spicynoodle7419 2 месяца назад +1

      You can use the blade directives for policies.
      @can('edit', [$post])
      Edit
      @endcan
      There is also the inverse: @cannot / @endcannot

    • @spicynoodle7419
      @spicynoodle7419 2 месяца назад

      You can wrap the button with:
      @can('edit', [$post])
      button
      @endcan

    • @spicynoodle7419
      @spicynoodle7419 2 месяца назад

      A policy is a class of multiple gates and a gate is a standalone authorization. You should prefer policies

    • @LaravelDaily
      @LaravelDaily  2 месяца назад +3

      In short, Policy is a set of Gates around one Eloquent Model.

  • @CodeWithJagadish
    @CodeWithJagadish 2 месяца назад

    If Gate::before() returns true when using can(), how can I make cannot() return false or continue checking other policy methods?

  • @shinichi9do
    @shinichi9do 2 месяца назад +1

    could you please revisit your old videos? maybe there are some things we could improve🙂thank you!

    • @LaravelDaily
      @LaravelDaily  2 месяца назад +1

      Which ones do you mean exactly? Yes I'm constantly talking about old topics but with new examples.

    • @shinichi9do
      @shinichi9do 2 месяца назад

      @@LaravelDaily Yes like old topics with new examples, sorry for my English :)