【问题标题】:Get all data from pivot table从数据透视表中获取所有数据
【发布时间】:2016-11-21 12:38:59
【问题描述】:

到目前为止,我可以在数据透视表中获取数据,但我的问题是,我如何从类别表中获取所有类别,如果我的数据透视表中存在此类别,则需要检查输入,如何我可以得到这个吗?

项目模型

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');
    }
}

类别模型

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');
    }
}

控制器

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::model($project, ['class' => 's-form', 'route' => ['project.update', $customer->id, $project->id]]) !!}
    {{ method_field('PUT') }}
    <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">{{ $errors->first('name') }}</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">{{ $errors->first('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">{{ $errors->first('about') }}</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">{{ $errors->first('url') }}</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">{{ $errors->first('header') }}</div>
        @endif
    </div>
    <div class="s-form-item checkbox inline">
        <div class="item-title">Categorias do projeto</div>
        @foreach($project->category as $category)
            {{-- Here I get all the categories, and the categories that exists in the pivot table will be checked--}}
            {{--<div class="s-checkbox s-radio-input">--}}
                {{--<input id="category-{{ str_slug($category->name) }}" type="checkbox" name="categories[]" value="{{ $category->id }}">--}}
                {{--<label for="category-{{ str_slug($category->name) }}"><span></span>{{ $category->name }}</label>--}}
            {{--</div>--}}
        @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="{{ url('admin/clientes/editar') . '/' . $customer->id }}" class="s-btn cancel">Voltar</a>
        <input class="s-btn" type="submit" value="Atualizar">
    </div>
{!! Form::close() !!}

【问题讨论】:

    标签: php laravel laravel-5.1


    【解决方案1】:

    在您的控制器中,您需要选择项目及其类别:

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

    然后在你的 HTML 中:

    <div class="s-form-item checkbox inline">
        <div class="item-title">Categorias do projeto</div>
        @foreach($categories as $category)
            <div class="s-checkbox s-radio-input">
                <input id="category-{{ str_slug($category->name) }}" type="checkbox" name="categories[]" {{$project->category->contains($category) ?  'checked="true"' : ''}} value="{{ $category->id }}">
                <label for="category-{{ str_slug($category->name) }}"><span></span>{{ $category->name }}</label>
            </div>
        @endforeach
    </div>
    

    使用所选类别更新项目:

    $project = Project::findOrFail($project);
    $project->category()->sync($request->input('categories', []));
    $project->update($request->except(['_token','_method', 'categories']);
    

    希望对你有帮助。

    【讨论】:

    • 工作就像一个魅力!我怎样才能更新它和所有的枢轴一起?到目前为止,无需更新我的枢轴,这是我的代码:Project::where('id', $project)-&gt;update($request-&gt;except(['_token','_method']));
    • 请查看更新后的答案。如果这解决了问题,请将其标记为已接受(以便将来遇到相同问题的人能够轻松发现它)。
    • 为您服务,先生!
    猜你喜欢
    • 2022-07-21
    • 1970-01-01
    • 2021-09-10
    • 2019-09-29
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    • 2019-07-25
    • 1970-01-01
    相关资源
    最近更新 更多