administration crud

3

I am working in an Online Store.

On my system:

  • There are several registered users in the database.
  • Each user adds their products, their categories, and has their own customers.
  • Upon entering the administration panel everyone sees the categories of all and their products.

I want each user to only see their categories and products.

I have searched for documentation but I have not found exactly what I'm looking for.

Categories of All Users

Here I leave the code that I have only of the categories

<?php

namespace laravel\Http\Controllers;

use Illuminate\Http\Request;

use laravel\Http\Requests;
use laravel\Categoria;
use Illuminate\Support\Facades\Redirect;
use laravel\Http\Requests\CategoriaFormRequest;
use DB;
use Session;

class CategoriaController extends Controller
{


    public function index(Request $request)
    {
       if ($request)
       {
        $query=trim($request->get('searchText'));
        $categorias=DB::table('categoria as c')
        ->join('proveedor as p','c.idProveedor','=','p.idProveedor')
        ->select('c.idCategoria','c.nombre','p.nombre as proveedor')
        ->where('c.nombre','LIKE','%'.$query.'%')
    ->where ('condicon','=','0')
        ->orderBy('idCategoria','desc')
    ->paginate (7);
        return view('almacen.categoria.index',["categorias"=>$categorias,"searchText"=>$query]);
       }
    }
    public function create ()
    {

      $proveedores=DB::table('proveedor')->get();
      return view("almacen.categoria.create",["proveedores"=>$proveedores]);
    }

    public function store(CategoriaFormRequest $request)
    {
    $categoria=new Categoria;
    $categoria->idProveedor=$request->get('idProveedor');
    $categoria->nombre=$request->get('nombre');
    $categoria->save();
    return Redirect::to('almacen/categoria');
     }
     public function show($id)
     { 

        return view("almacen.categoria.show",["categoria"=>Categoria::findOrFail($id)]);
    }
    public function edit($id)
     { 
        $categoria=Categoria::findOrFail($id);
        $proveedores=DB::table('proveedor')->get();
        return view("almacen.categoria.edit",["categoria"=>$categoria,"proveedores"=>$proveedores]);
    }

    public function update(CategoriaFormRequest $request,$id)
    {

         $categoria=Categoria::findOrFail($id);
         $categoria->idProveedor=$request->get('idProveedor');
         $categoria->nombre=$request->get('nombre');
         $categoria->update();
         return Redirect::to('almacen/categoria');

    }
    public function destroy($id)
    {
        $categoria=Categoria::findOrFail($id);
        $categoria->condicon='-1';        
        $categoria->update();
    return Redirect::to('almacen/categoria');
    }
}
    
asked by Rodrigo A. Moreno 28.12.2018 в 07:37
source

1 answer

1

What would normally be done in Laravel is to define the relationships in the models, in this case they would be User - Category - Product, and from there it is very easy to obtain the corresponding categories and products for each user.

For that you have to define the 3 models according to the documentation: link

link

Next I will show how the part that interests us of these models would be (the relationships):

User

class User extends ...
{
    // ...

    public function categorias()
    {
        return $this->hasMany('Categoria::class');
    }
}

Category

class Categoria extends ...
{
    // ...

    public function productos()
    {
        return $this->hasMany('Producto::class');
    }
}

Reciprocal relationships should also be defined, including those of the Product model, but let's keep it simple for this case.

Subsequently, the corresponding categories and products can be loaded in various ways:

$query = $request->searchText;

$categorias = Categoria::where('nombre','LIKE','%'.$query.'%')
                       ->whereUserId(auth()->user()->id)
                       ->paginate(7);

To load the necessary relationships as products or suppliers, you can use Eager Loading: link

    
answered by 28.12.2018 в 15:23