Retrieve the labels of a food

0

I have a Many To Many relationship between Food and Labels. The problem is that when I want to edit a food in the view to edit it, I want to retrieve the different assigned labels but these are collected in the creation view through a checkbox and I would like the marked checkbox to appear in the edit view.

Next I put the code that I am using:

  • AlimentoController:

    public function create() {
    return view('Alimento/crear');
    }
    
    public function store(Request $request)        {
    $this-> validate($request,
    ['nombre'=>'required', 'kcal'=>'required', 'proteinas'=>'required', 'carbohidratos'=>'required','grasas'=>'required'  ]);
    
    $etiqueta=new Etiqueta();
    $alimento = new Alimento();
    
    $alimento->nombre = $request->nombre;
    $alimento->kcal = $request->kcal;
    $alimento->grasas = $request->grasas;
    $alimento->proteinas=$request->proteinas;
    $alimento->carbohidratos=$request->carbohidratos;
    
    $etiqueta->etiqueta=$request->etiqueta;
    $alimento->etiquetas()->attach($etiqueta->id);
    
    $etiqueta->save();
    $alimento->save();
    
    }
    
    public function edit($id) {
    $alimento = Alimento::find($id);
    $etiquetas= $alimento->etiquetas();
    return view ('Alimento/editar')->with(['alimento'=>$alimento, 'etiquetas'=>$etiquetas]);
     }
    
    public function update(Request $request, $id){
    
    $this-> validate($request,
        ['nombre'=>'required', 'kcal'=>'required', 'proteinas'=>'required', 'carbohidratos'=>'required',
            'grasas'=>'required'  ]);
    
    
    $alimento = Alimento::find($id);
    $etiqueta = $alimento->etiquetas();
    
    $alimento->nombre = $request->nombre;
    $alimento->kcal = $request->kcal;
    $alimento->grasas = $request->grasas;
    $alimento->proteinas=$request->proteinas;
    $alimento->carbohidratos=$request->carbohidratos;
    $etiqueta->etiqueta=$request->etiqueta;
    
    
    
    $etiqueta->save();
    $alimento->save();
    
    if($alimento->save()){
        return redirect('alimentos');
    }else{
        return back()->with('nmsj','No se ha actualizado el alimento correctamente');
    }
    
    }
    
  • Create-Food View (code portion to collect the labels):

<div class="checkbox">
<label><input type="checkbox" name="etiqueta" value="pescados"><strong>Pescados y mariscos</strong></label>
</div>
<div class="checkbox">
<label><input type="checkbox" name="etiqueta" value="carnes"><strong>Carnes y derivados cárnicos</strong></label>
 </div>
 <div class="checkbox">
 <label><input type="checkbox" name="etiqueta" value="huevos"><strong>Huevos</strong></label>
 </div>
 <div class="checkbox">
 <label><input type="checkbox" name="etiqueta" value="cereales"><strong>Cereales</strong></label>
 </div>
  • Edit-Food View (portion of code to retrieve labels)

<div class="checkbox">
<label><input type="checkbox" name="etiqueta" value="pescados" {{$alimento->etiquetas()=='pescados' ? 'checked':''}}><strong>Pescados y mariscos</strong></label>
 </div>
 <div class="checkbox">
 <label><input type="checkbox" name="etiqueta" value="carnes" {{$alimento->etiquetas()=='carnes' ? 'checked':''}}><strong>Carnes y derivados cárnicos</strong></label>
  </div>
  <div class="checkbox">
  <label><input type="checkbox" name="etiqueta" value="huevos" {{$alimento->etiquetas()=='huevos' ? 'checked':''}}><strong>Huevos</strong></label>
  </div>
  <div class="checkbox">
  <label><input type="checkbox" name="etiqueta" value="cereales" {{$alimento->etiquetas()=='cereales' ? 'checked':''}}><strong>Cereales</strong></label>
 </div>

Thank you very much!

    
asked by Roledi 20.06.2017 в 13:47
source

1 answer

0

You are confusing $alimento->etiquetas() and $alimento->etiquetas .

$alimento->etiquetas() is a query that you do directly to the relationship, so as you can see in the dump, it gives you an object of type BelongsToMany .

$alimento->etiquetas is a dynamic or "magic" property that normally contains a collection composed of the elements (labels) related to that food.

If you want to compare with in_array() (assuming it is one dimensional) or similar, then you should use the dynamic property instead of the query to the relationship.

in_array('etiqueta1', $alimento->etiquetas)
    
answered by 20.06.2017 в 16:45