Laravel checkbox checked or not checked as database

4

I need a help in Laravel how to "check the checkboxes" of a form. I have a color table, today with 12 different colors but can be as many as I want. And a one product byproduct table. The cores.id = corproduto.cor_id. When I register the product I choose the colors that it will have. So far so good. But when I do the editing of this product, I would like to see the list of all the colors and the ones that are used (checked). I checked this link, link but I could not. The image below is from the registration. Thanks for any help. Valdir

    
asked by anonymous 14.09.2016 / 18:37

2 answers

0

To do this, you must first have the relationships correctly configured in your model.

For example:

class Cor extends Eloquent {

}


class Produto extends Eloquent {
    public function cores() {
       return $this->hasMany(Cor::class, 'cor_id');
    }
}

If you are using Form::model of Laravel, you simply need to do so:

{{ Form::model($produto) }}

 @foreach($cores as $cor)
    {{ Form::checkbox('cores[]', $cor->id) }} {{ $cor->nome }}
 @endforeach

{{ Form::close() }}

When doing this, automatically the colors that are present in the Produto relationship will be marked in the checkbox.

If internal nomenclatures are complicated to do with the above form, you also have the option to use the method contains , present in Collection .

See:

 @foreach($cores as $cor)
    {{ 
       Form::checkbox(
           'cores[]',
           $cor->id,
           $produto->cores->contains($cor->id)
       ) 
     }}
     {{ $cor->nome }}
 @endforeach

This answer applies if you are using Laravel 4, or Laravel 5 along with the Laravel Collective library. If you are not using it, you will have to if on hand, in HTML.

    
14.09.2016 / 18:56
0

There is then a Muitos para Muitos ( N: M ) relationship, where tables Produto and Cores has an intermediate table with its corresponding keys: / p>

Model Cor :

<?php

namespace App\Models;    
use Illuminate\Database\Eloquent\Model;

class Cor extends Model
{
    protected $table = 'cor';    
    protected $fillable = array('descricao');    
    protected $primaryKey = 'id';    
    public $timestamps = false;  

    //relação muitos para muitos.
    public function produtos()
    {
        return $this->belongsToMany(Produto::class,'produto_cor','cor_id', 'produto_id');
    }
}

Model Produto :

<?php

namespace App\Models;    
use Illuminate\Database\Eloquent\Model;

class Produto extends Model
{
    protected $table = 'produto';    
    protected $fillable = array('description', 'cor_id');    
    protected $primaryKey = 'id';
    public $timestamps = false;

    //relação muitos para muitos.
    public function cores()
    {
        return $this->belongsToMany(Cor::class,'produto_cor','produto_id', 'cor_id');
    }
}

No Controller

public function index()
{
    $id = 1; // código do produto
    $result = Cor::leftJoin('produto_cor', function($join) use ($id)
        {
            $join->on('produto_cor.cor_id', '=', 'cor.id')
                ->where('produto_cor.produto_id', (int)$id);

        })
        ->select(\DB::raw('cor.id, cor.descricao, if(produto_id is null, 0, 1) corstatus'))
        ->orderBy('cor.descricao')
        ->get();
    return view('test.index', compact('result'));
}

In the variable $result return to color list with an extra field corstatus where% the color is present in that product, different not.

And finally in corstatus = 1 :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Laravel - Test</title>
    {{ Html::script('js/jquery.v1.11.0.js') }}
</head>
<body>
    @foreach($result as $r)     
        <div>
        <input @if((int)$r->corstatus==1) {!! 'checked="checked" ' !!} @endif type="checkbox" name="cor[]" value="{{$r->id}}">{{$r->descricao}}
        </div>
    @endforeach
</body>
</html>
    
15.09.2016 / 02:20