CoderFunda
  • Home
  • About us
    • Contact Us
    • Disclaimer
    • Privacy Policy
    • About us
  • Home
  • Php
  • HTML
  • CSS
  • JavaScript
    • JavaScript
    • Jquery
    • JqueryUI
    • Stock
  • SQL
  • Vue.Js
  • Python
  • Wordpress
  • C++
    • C++
    • C
  • Laravel
    • Laravel
      • Overview
      • Namespaces
      • Middleware
      • Routing
      • Configuration
      • Application Structure
      • Installation
    • Overview
  • DBMS
    • DBMS
      • PL/SQL
      • SQLite
      • MongoDB
      • Cassandra
      • MySQL
      • Oracle
      • CouchDB
      • Neo4j
      • DB2
      • Quiz
    • Overview
  • Entertainment
    • TV Series Update
    • Movie Review
    • Movie Review
  • More
    • Vue. Js
    • Php Question
    • Php Interview Question
    • Laravel Interview Question
    • SQL Interview Question
    • IAS Interview Question
    • PCS Interview Question
    • Technology
    • Other

17 May, 2022

limit the view depending on the values - Laravel - Spatie

 Programing Coderfunda     May 17, 2022     Laravel, php     No comments   

I need, depending on the role/permission of each user, to limit the database records that are displayed in the view index.

For example, I need a certain user to only be able to see the records with the "pendiente" value in the "estado" column.

code of my controller index function:

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();
    }

code of my index.blade view

@extends('layouts.app')


@section('content')
 



    <section class="section">
        <div class="section-header">
            <h3 class="page__heading">Siniestros</h3>
        </div>
        <div class="section-body">
            <div class="row">
                <div class="col-lg-12">
                    <div class="card">
                        <div class="card-body">
                
            
                        @can('crear-siniestro')
                        <a class="btn btn-primary" href="{{ route('siniestros.create') }}">Nuevo</a>
                        @endcan
            
                        <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>
                                    @can('derivar-siniestro')
                                    <!-- <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>
                                    @endcan
                                    <th style="color:#fff;">Acciones</th>                                                                   
                              </thead>
                              <tbody>
                            @foreach ($siniestros as $siniestro)
                            <tr>
                                <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>
                                @can('derivar-siniestro')
                                <!-- <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>
                                @endcan
                                <td>
                                    <form action="{{ route('siniestros.destroy',$siniestro->id) }}" method="POST">                                        
                                        @can('editar-siniestro')
                                        <a class="btn btn-outline-success btn-sm" href="{{ route('siniestros.edit',$siniestro->id) }}">Editar</a>
                                        @endcan
                                        
                                        
                                      
                                        @csrf
                                        @method('DELETE')
                                        @can('borrar-siniestro')
                                        <button type="submit" class="btn btn-outline-danger btn-sm">Borrar</button>
                                        @endcan

                                    </form>
                                    
                                </td>
                            </tr>
                            @endforeach
                            </tbody>
                        </table>

                        <!-- Paginacion a la derecha -->
                        <div class="pagination justify-content-end">
                            {!! $siniestros->links() !!}
                        </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
   

    
@endsection

@section('javas')

<script>
    $(document).ready(function() {
    $('.tablita').DataTable({
        responsive: true,
        processing: true,

        
    });
})
</script>
<!-- DataTables JS -->

<script src="{{ asset('assets/js/jquery.min.js') }}"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/pdfmake.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/vfs_fonts.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/v/bs4/jszip-2.5.0/dt-1.11.5/b-2.2.2/b-colvis-2.2.2/b-html5-2.2.2/b-print-2.2.2/r-2.2.9/datatables.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.0/js/bootstrap.min.js"></script>
<script>
        $(function() {
            const languages = {
                'es': 'https://cdn.datatables.net/plug-ins/1.10.19/i18n/Spanish.json'
            };

            $.extend(true, $.fn.dataTable.Buttons.defaults.dom.button, {
                className: 'btn btn-sm'
            })
            $.extend(true, $.fn.dataTable.defaults, {
                responsive: true,
                language: {
                    url: languages['es']
                },
                pageLength: 25,
                dom: 'lBfrtip',
                buttons: [{
                        extend: 'copy',
                        className: 'btn-light',
                        text: 'Copiar',
                        exportOptions: {
                            columns: ':visible'
                        }
                    },
                    {
                        extend: 'csv',
                        className: 'btn-light',
                        text: 'CSV',
                        exportOptions: {
                            columns: ':visible'
                        }
                    },
                    {
                        extend: 'excel',
                        className: 'btn-light',
                        text: 'Excel',
                        exportOptions: {
                            columns: ':visible'
                        }
                    },
                    {
                        extend: 'pdf',
                        className: 'btn-light',
                        text: 'PDF',
                        exportOptions: {
                            columns: ':visible'
                        }
                    },
                    {
                        extend: 'print',
                        className: 'btn-light',
                        text: 'Imprimir',
                        exportOptions: {
                            columns: ':visible'
                        }
                    },
                    {
                        extend: 'colvis',
                        className: 'btn-light',
                        text: 'Visibilidad Columnas',
                        exportOptions: {
                            columns: ':visible'
                        }
                    }
                ]
            });
        });
    </script>

@endsection

Attached screenshot of my index.blade view

index.blade view

Of course I can filter with my DataTable, but the idea is that for security reasons, each user can only view the records according to their "estado"/value

I thank you in advance, and tell me if it is relevant that I attach other information

I need to do this, but in the backend, with the roles/permissions

Model Siniestro.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Wildside\Userstamps\Userstamps;



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);
   }



}



1 Answer

0

After your edit, I am writing this controller for you:

public function index()
    {

        $siniestros = Siniestro::paginate(50);

        if($user->hasDirectPermission('crear-siniestro')){
            $siniestros->makeHidden(['estado']);
        }
        
        return view('siniestros.index', compact('siniestros'));

        $now = Carbon::now();
    }

This should work if I'm not mistaken.

  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Thanks

Meta

Popular Posts

  • Write API Integrations in Laravel and PHP Projects with Saloon
    Write API Integrations in Laravel and PHP Projects with Saloon Saloon  is a Laravel/PHP package that allows you to write your API integratio...
  • Features CodeIgniter
    Features CodeIgniter There is a great demand for the CodeIgniter framework in PHP developers because of its features and multiple advan...
  • Laravel Breeze with PrimeVue v4
    This is an follow up to my previous post about a "starter kit" I created with Laravel and PrimeVue components. The project has b...
  • Credit card validation in laravel
      Validation rules for credit card using laravel-validation-rules/credit-card package in laravel Install package laravel-validation-rules/cr...
  • Fast Excel Package for Laravel
      Fast Excel is a Laravel package for importing and exporting spreadsheets. It provides an elegant wrapper around Spout —a PHP package to ...

Categories

  • Ajax (26)
  • Bootstrap (30)
  • DBMS (42)
  • HTML (12)
  • HTML5 (45)
  • JavaScript (10)
  • Jquery (34)
  • Jquery UI (2)
  • JqueryUI (32)
  • Laravel (1017)
  • Laravel Tutorials (23)
  • Laravel-Question (6)
  • Magento (9)
  • Magento 2 (95)
  • MariaDB (1)
  • MySql Tutorial (2)
  • PHP-Interview-Questions (3)
  • Php Question (13)
  • Python (36)
  • RDBMS (13)
  • SQL Tutorial (79)
  • Vue.js Tutorial (68)
  • Wordpress (150)
  • Wordpress Theme (3)
  • codeigniter (108)
  • oops (4)
  • php (853)

Social Media Links

  • Follow on Twitter
  • Like on Facebook
  • Subscribe on Youtube
  • Follow on Instagram

Pages

  • Home
  • Contact Us
  • Privacy Policy
  • About us

Blog Archive

  • September (100)
  • August (50)
  • July (56)
  • June (46)
  • May (59)
  • April (50)
  • March (60)
  • February (42)
  • January (53)
  • December (58)
  • November (61)
  • October (39)
  • September (36)
  • August (36)
  • July (34)
  • June (34)
  • May (36)
  • April (29)
  • March (82)
  • February (1)
  • January (8)
  • December (14)
  • November (41)
  • October (13)
  • September (5)
  • August (48)
  • July (9)
  • June (6)
  • May (119)
  • April (259)
  • March (122)
  • February (368)
  • January (33)
  • October (2)
  • July (11)
  • June (29)
  • May (25)
  • April (168)
  • March (93)
  • February (60)
  • January (28)
  • December (195)
  • November (24)
  • October (40)
  • September (55)
  • August (6)
  • July (48)
  • May (2)
  • January (2)
  • July (6)
  • June (6)
  • February (17)
  • January (69)
  • December (122)
  • November (56)
  • October (92)
  • September (76)
  • August (6)

Loading...

Laravel News

Loading...

Copyright © CoderFunda | Powered by Blogger
Design by Coderfunda | Blogger Theme by Coderfunda | Distributed By Coderfunda