Ver Mensaje Individual
  #6 (permalink)  
Antiguo 26/07/2018, 15:27
Avatar de ArturoGallegos
ArturoGallegos
Moderador
 
Fecha de Ingreso: febrero-2008
Ubicación: Morelia, México
Mensajes: 6.774
Antigüedad: 16 años, 2 meses
Puntos: 1146
Respuesta: Laravel error if en relationship

Pues acabo de limpiar todo para hacer la prueba y sigo con los mismos resultados, asi es como quedaron los modelos y el controlador, practicamente nada.

Modelo Inventario:
Código PHP:
Ver original
  1. <?php
  2.  
  3. namespace App;
  4.  
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\ModelNotFoundException;
  7.  
  8. class Inventario extends Model
  9. {
  10.     protected $table = 'inventario_2';
  11.  
  12.     protected $casts = array(
  13.         'ids' => 'array',
  14.     );
  15.  
  16.     public function getProductos(){
  17.         return $this->hasManyThrough('App\Productos', 'App\InventarioProductosPivote',
  18.             'inventario_id', 'id', 'id', 'producto_id');
  19.     }
  20.  
  21.     public function Ventas(){
  22.         return $this->hasMany('App\Ventas', 'inventario_id');
  23.     }
  24. }

Modelo Ventas:
Código PHP:
Ver original
  1. <?php
  2.  
  3. namespace App;
  4.  
  5. use Illuminate\Database\Eloquent\Model;
  6.  
  7. class Ventas extends Model
  8. {
  9.     public function getInventario(){
  10.         return $this->hasOne('App\Inventario', 'id', 'inventario_id');
  11.     }
  12.  
  13.     public function Pagos(){
  14.         return $this->hasMany('App\Pagos', 'venta_id');
  15.     }
  16.  
  17.     public function Articulos(){
  18.         return $this->hasMany('App\VentaArticulos', 'venta_id');
  19.     }
  20. }

Controlador:
Código PHP:
Ver original
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use App\Inventario;
  6. use Illuminate\Http\Request;
  7.  
  8. class InventarioController extends Controller
  9. {
  10.     public function detalle_evento($evento_id, $vendedor = false){
  11.         return Inventario::where('id', $evento_id)->when($vendedor, function ($query, $vendedor){
  12.             $query->with(['Ventas' => function($query) use($vendedor){
  13.                 $query->where('vendedor', $vendedor);
  14.             }, 'Ventas.Articulos']);
  15.         }, function ($query){
  16.             $query->with('Ventas', 'Ventas.Articulos');
  17.         })->first();
  18.     }
  19.  
  20.     public function detalle_evento_view_imprimir($evento_id, Request $request){
  21.       return $evento = $this->detalle_evento($evento_id, $request->vendedor);// si hago el return aqui el filtro se respeta
  22.       return $evento->ventas;// si hago el return aqui el filtro se ignora ,  porque???'
  23.     }
  24. }