lundi 18 juillet 2016

Get all data from pivot table

By now, I can get the data in the pivot table, but my question is, how can I get all the categories from my categories table and, if exists this category in my pivot table the input needs to be checked, how can I get this?

Project model

class Project extends Model
{
    protected $table = 'projects';

    protected $fillable = [
        'name',
        'slug',
        'header',
        'desc',
        'about',
        'url',
        'status'
    ];

    public function customer()
    {
        return $this->belongsTo(Customer::class);
    }

    public function category()
    {
        return $this->belongsToMany(Category::class)->withPivot('category_id');
    }
}

Category model

class Category extends Model
{
    protected $table = 'categories';

    protected $fillable = [
        'name',
        'status'
    ];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function subCategory()
    {
        return $this->hasMany(SubCategory::class);
    }

    public function projects()
    {
        return $this->belongsToMany(Project::class)->withPivot('project_id');
    }
}

Controller

public function getEdit(Customer $customer, $project)
{
    return view('admin.pages.admin.clientes.projetos.edit', [
        'customer' => $customer,
        'project' => Project::where('id', $project)->firstOrFail(),
        'categories' => $this->categoryRepository->allCategories(), //return all categories in the categories table
        'title' => $customer->name
    ]);
}

Form

{!! Form::model($project, ['class' => 's-form', 'route' => ['project.update', $customer->id, $project->id]]) !!}
    
    <div class="s-form-item text">
        <div class="item-title required">Nome do projeto</div>
        {!! Form::text('name', null, ['placeholder' => 'Nome do projeto']) !!}
        @if($errors->has('name'))
            <div class="item-desc"></div>
        @endif
    </div>
    <div class="s-form-item text">
        <div class="item-title">Descrição do projeto</div>
        {!! Form::text('desc', null, ['placeholder' => 'Descrição do projeto']) !!}
        @if($errors->has('desc'))
            <div class="item-desc"></div>
        @endif
    </div>
    <div class="s-form-item text">
        <div class="item-title">Sobre o projeto</div>
        {!! Form::text('about', null, ['placeholder' => 'Sobre o projeto']) !!}
        @if($errors->has('about'))
            <div class="item-desc"></div>
        @endif
    </div>
    <div class="s-form-item text">
        <div class="item-title">URL do projeto</div>
        {!! Form::text('url', null, ['placeholder' => 'URL do projeto']) !!}
        @if($errors->has('url'))
            <div class="item-desc"></div>
        @endif
    </div>
    <div class="s-form-item text">
        <div class="item-title">Imagem de cabeçalho do projeto</div>
        {!! Form::text('header', null, ['placeholder' => 'Imagem de cabeçalho do projeto']) !!}
        @if($errors->has('header'))
            <div class="item-desc"></div>
        @endif
    </div>
    <div class="s-form-item checkbox inline">
        <div class="item-title">Categorias do projeto</div>
        @foreach($project->category as $category)
            
            
                " type="checkbox" name="categories[]" value="">--}}
                "><span></span></label>--}}
            
        @endforeach
    </div>
    <div class="s-form-item radio inline">
        <div class="item-title required">Status do projeto</div>
        <div class="s-radio-input">
            {!! Form::radio('status', '1', null, ['id' => 'categoria-ativo']) !!}
            <label for="categoria-ativo"><span></span>Ativa</label>
        </div>
        <div class="s-radio-input">
            {!! Form::radio('status', '0', null, ['id' => 'categoria-inativo']) !!}
            <label for="categoria-inativo"><span></span>Inativa</label>
        </div>
    </div>
    <div class="s-form-item s-btn-group s-btns-right">
        <a href="" class="s-btn cancel">Voltar</a>
        <input class="s-btn" type="submit" value="Atualizar">
    </div>
{!! Form::close() !!}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire