S
S
Stanislav Pochepko2016-12-08 18:40:41
Laravel
Stanislav Pochepko, 2016-12-08 18:40:41

How to write a global scoup?

Good evening. Faced a problem. My task is the following. Depending on the role of the user, filter the selection of Users models according to some criteria. I decided to write a global Scope for this. I see that this is the most correct solution. Since this additional rule should always work, in all requests for Users.
The documentation says that the scope must be activated in the boot method of the model. BUT how can I make a filter by the current user and its role, if the models have not even been initialized yet.

if ($user->role->slug == 'moderator') {
    $builder->where('....');
} elseif ($user->role->slug == 'member') {
    $builder->where('....');
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alex Wells, 2016-12-11
@Alex_Wells

In the boot, you only register the scope, no manipulations take place there. They occur only when a request is made to the database, in which case the model is already an instance.
All code will not fit here, but in short - use separate classes of global scopes. https://laravel.com/docs/5.3/eloquent#global-scopes
/* PSEUDO CODE */
class RoleScope
apply blablabla
class MyModel
boot addGlobalScope new RoleScope
I don't know why, but Laravel only passes model instance to classes, not anonymous ones . You'll find out more there.)
In general - use Bouncer for roles and permissions. Not only does it use the built-in features of Laravel in the form of gates, but it also supports entity morphing and a bunch of other goodies. I legitimately call it the best representative of its kind and recommend it for use!)

S
Sergey delphinpro, 2017-04-12
@letehaha

foreach ($employee as $value) {
    echo '	<div class="new-form" data-employee-id="'.$value['id_employee'].'">
          <input type="text" disabled value="'.$value['first_name'].'">
          <input type="text" disabled value="'.$value['second_name'].'">
          <input type="text" disabled value="'.$value['middle_name'].'">
          <select name="list-position" id="list-position">';
          foreach ($positions as $val) {
            echo '<option value="'.$val['id_position'].'">'.$val['position'].'</option>';
          }
// echo забыл
echo '</select>
          <input type="text" disabled value="'.$value['position'].'">
          <input type="text" disabled value="'.$value['salary'].'">
          <button class="change-employee" data-id="'.$id.'">Изменить</button>
          <button id="js-save-change-employee" class="save-change-employee hide" data-id="'.$id.'">Сохранить изменения</button>
        </div>';
  }

But why write like that? You can arrange everything beautifully and not break your eyes
<?php foreach ($employee as $value) { ?>
    <div class="new-form" data-employee-id="<?= $value['id_employee'] ?>">
        <input type="text" disabled value="<?= $value['first_name'] ?>">
        <input type="text" disabled value="<?= $value['second_name'] ?>">
        <input type="text" disabled value="<?= $value['middle_name'] ?>">
        
        <select name="list-position" id="list-position">';
            <?php foreach ($positions as $val) { ?>
                <option value="<?= $val['id_position'] ?>"><?= $val['position'] ?></option>
            <?php } ?>
        </select>
        
        <input type="text" disabled value="<?= $value['position'] ?>">
        <input type="text" disabled value="<?= $value['salary'] ?>">
        <button class="change-employee" data-id="<?= $id ?>">Изменить</button>
        <button id="js-save-change-employee" class="save-change-employee hide" data-id="<?= $id ?>">Сохранить изменения</button>
    </div>
<?php } ?>

PS. Local illumination, of course, kills all the charm, but the editors highlight normally.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question