php – Filter records by value – Laravel

I need that depending on the user who is logged in, different records/files are displayed. Each user will be assigned different records/files to manage. Although they can be filtered with the search bar, the idea would be that everyone can see the ones assigned to them, for a matter of security and neatness. From where would it be more convenient to do this?

This is the Index view of my system, where only the records/files assigned to the user should appear

I leave you a portion of this index view of the controller

    namespace AppHttpControllers;
    use IlluminateHttpRequest;
    use AppModelsSiniestro;
    use IlluminateSupportFacadesDB;
    use AppMailContactanosMailable;
    use IlluminateSupportFacadesMail;
    class SiniestroController extends Controller
        function __construct()
            $this->middleware('permission:crear-siniestro', ['only'=>['create','store']]);
            $this->middleware('permission:editar-siniestro|derivar-siniestro', ['only'=>['edit','update']]);
            $this->middleware('permission:borrar-siniestro', ['only'=>['destroy']]);
         * Display a listing of the resource.
         * @return IlluminateHttpResponse
        public function index()
            $siniestros = Siniestro::paginate(50);
              //return DB::select('select localidad from siniestros'); //---> Devuelve los datos de la columna estados
            return view('siniestros.index', compact('siniestros'));
            $now = Carbon::now();
         * Show the form for creating a new resource.
         * @return IlluminateHttpResponse

view blade.index

        <section class="section">
            <div class="section-header">
                <h3 class="page__heading">Siniestros</h3>
            <div class="section-body">
                <div class="row">
                    <div class="col-lg-12">
                        <div class="card">
                            <div class="card-body">
                            <a class="btn btn-primary" href="{{ route('siniestros.create') }}">Nuevo</a>
                            <table class="table table-sm m-1 p-1 table-bordered table-hover table-striped tablita" style="width:100%">
                                    <thead style="background-color:hsl(213, 99%, 49%)">                                     
                                        <th style="display: none;">ID</th>
                                        <th style="color:#fff;">Siniestro</th>
                                        <th style="color:#fff;">Coordinador</th>
                                        <th style="color:#fff;">Actualizado</th>                                    
                                        <th style="color:#fff;">Patente</th>
                                        <th style="color:#fff;">Cliente</th>
                                        <th style="color:#fff;">Fecha ingreso</th>
                                        <th style="color:#fff;">Fecha gestión</th>
                                        <th style="color:#fff;">Captura de pantalla</th>
                                        <th style="color:#fff;">Estado</th>
                                        <th style="color:#fff;">Modalidad</th>
                                        <th style="color:#fff;">Observaciones</th>
                                        <!-- <th style="color:#fff;">Screenshot</th> -->
                                        <th style="color:#fff;">Dirección</th>
                                        <th style="color:#fff;">Localidad</th>
                                        <th style="color:#fff;">Inspector</th>
                                        <th style="color:#fff;">Motivo</th>
                                        <th style="color:#fff;">Enviar Orden</th>
                                        <th style="color:#fff;">Acciones</th>                                                                   
                                @foreach ($siniestros as $siniestro)
                                    <td style="display: none;">{{ $siniestro->id }}</td>                                
                                    <td>{{ $siniestro->siniestro }}</td>
                                    <td>{{ $siniestro->creator->name }}</td>
                                    <td>{{ $siniestro->editor->name }}</td>
                                    <td>{{ $siniestro->patente }}</td>
                                    <td>{{ $siniestro->cliente }}</td>
                                    <td>{{ $siniestro->created_at }}</td>
                                    <td>{{ $siniestro->updated_at }}</td>
                                    <td><img alt="img" src="/img/{{ $siniestro->imagen }}" width="100px"></td>
                                    <td>{{ $siniestro->estado }}</td>
                                    <td>{{ $siniestro->modalidad }}</td>
                                    <td>{{ $siniestro->observaciones }}</td>
                                    <!-- <td><a href="{{ $siniestro->url }}" target="blank_" >Ver documento</a></td> -->
                                    <td>{{ $siniestro->direccion }}</td>
                                    <td>{{ $siniestro->localidad }}</td>
                                    <td>{{ $siniestro->inspector }}</td>
                                    <td>{{ $siniestro->motivo }}</td>
                                    <td>{{ $siniestro->enviarorden }}</td>
                                        <form action="{{ route('siniestros.destroy',$siniestro->id) }}" method="POST">                                        
                                            <a class="btn btn-outline-success btn-sm" href="{{ route('siniestros.edit',$siniestro->id) }}">Editar</a>
                                            <button type="submit" class="btn btn-outline-danger btn-sm">Borrar</button>
                            <!-- Paginacion a la derecha -->
                            <div class="pagination justify-content-end">
                                {!! $siniestros->links() !!}


    namespace AppModels;
    use IlluminateDatabaseEloquentFactoriesHasFactory;
    use IlluminateDatabaseEloquentModel;
    use WildsideUserstampsUserstamps;
    class Siniestro extends Model
        use HasFactory;
        use Userstamps;
        protected $fillable = ['created_by', 'imagen', 'updated_by', 'deleted_by', 'siniestro', 'patente', 'cliente', 'fechaip', 'estado', 'modalidad',
        'observaciones', 'fechacierre', 'compania', 'contacto', 'codigoinspeccion', 'inspector', 'direccion', 'localidad', 'telefono', 'motivo', 'link', 'enviarorden', 'email'];
       public function archivos()
           return $this->hasMany(Archivo::class);

It occurs to me that the best thing would be to add a column to my database called, for example, “assigned to”, and depending on the value, which would be the name of each user (Marcoz Perez, Germán López, etc. , be the records/files that are listed.

I think I might be able to solve it with a “where” statement in the controller; but first, I don’t know how to use it properly… and second, I don’t know if from there I can get the result I’m looking for, paying attention to the different users.

Leave a Comment