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

Related Posts:

  • Laravel Octane  Laravel Octane is a package developed by Laravel that allows you to optimize the performance and scalability of your Laravel applications. It ac… Read More
  • Sharing Google Maps Data Across Separate Livewire ComponentsCreate a new Livewire component that will handle the Google Maps functionality. Let's call it MapComponent.goCopy codephp artisan make:livewire MapCom… Read More
  • Easy way to serve a realtime loggerHi,I have a set of log files generated on one server (by Python and Supervisor), that I would like to view on another server's web application, where … Read More
  • How to implement chat GPT in LaravelInstall the guzzlehttp/guzzle package using Composer:bashCopy codecomposer require guzzlehttp/guzzle Create a new controller that will handle the chat… Read More
  • Laravel 10.9 Multiple Image Upload In PHP <form action="uploading.php" method="POST" enctype="multipart/form-data">    <input type="file" name="image_path[]">&… Read More
Newer Post Older Post Home

0 comments:

Post a Comment

Thanks

Meta

Popular Posts

  • Spring boot app (error: method getFirst()) failed to run at local machine, but can run on server
    The Spring boot app can run on the online server. Now, we want to replicate the same app at the local machine but the Spring boot jar file f...
  • Log activity in a Laravel app with Spatie/Laravel-Activitylog
      Requirements This package needs PHP 8.1+ and Laravel 9.0 or higher. The latest version of this package needs PHP 8.2+ and Laravel 8 or hig...
  • Failed to install 'cordova-plugin-firebase': CordovaError: Uh oh
    I had follow these steps to install an configure firebase to my cordova project for cloud messaging. https://medium.com/@felipepucinelli/how...
  • Laravel auth login with phone or email
          <?php     Laravel auth login with phone or email     <? php     namespace App \ Http \ Controllers \ Auth ;         use ...
  • Vue3 :style backgroundImage not working with require
    I'm trying to migrate a Vue 2 project to Vue 3. In Vue 2 I used v-bind style as follow: In Vue 3 this doesn't work... I tried a...

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)

  • Failed to install 'cordova-plugin-firebase': CordovaError: Uh oh - 9/21/2024
  • pyspark XPath Query Returns Lists Omitting Missing Values Instead of Including None - 9/20/2024
  • SQL REPL from within Python/Sqlalchemy/Psychopg2 - 9/20/2024
  • MySql Explain with Tobias Petry - 9/20/2024
  • How to combine information from different devices into one common abstract virtual disk? [closed] - 9/20/2024

Laravel News

  • Arr::from() Method in Laravel 12.14 - 5/14/2025
  • Streamline API Resources with Laravel's Fluent Methods - 5/13/2025
  • Customize URL Handling with Laravel's Macroable URI Class - 5/13/2025
  • Use Passkeys in Your Laravel App - 5/13/2025
  • Laravel Seeder Generator - 5/12/2025

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