【问题标题】:How to edit pivoted table Checkbox Data in Laravel 6?如何在 Laravel 6 中编辑数据透视表复选框数据?
【发布时间】:2020-03-18 09:21:54
【问题描述】:

我正在尝试编辑和更新透视表数据,但我无法编辑。我在listingsproject_types 表之间有belongsToMany 关系。

这是我的 Listing.php 模型...

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Listing;
use App\ProjectType;
class Listing extends Model
{
protected $primaryKey = 'lstId';
protected $guarded = [];

public function proType(){
    return $this->belongsToMany(ProjectType::class, 'properties_searchable_tags', 'lstId', 'typeId');
}
}

这是我的 ProjectType.php 模型...

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\City;
use App\Listing;
use App\ProjectType;

class ProjectType extends Model
{
use SoftDeletes;
protected $primaryKey = 'typeId';
protected $guarded = [];
protected $with = ['listing'];

public function propertytype(){
    return $this->belongsTo(ProjectType::class, 'ctId','ctId');
}

public function listing()
{
    return $this->belongsToMany(ProjectType::class, 'properties_searchable_tags','typeId','lstId');
}
}

这是我的 ProjectTypeContrroller.php 文件..

 public function edit($projectType)
{
    $city=City::all();
    $locality=Locality::all();
    $listingdata=Listing::all();
    $propertyType=ProjectType::findOrFail($projectType);
    return view('admin.propertytype.edit', compact('city', 'propertyType','locality','listingdata'));
}

这是我的edit.blade.php

    <tbody>
                                            @foreach($listingdata as $lists)
                                            <tr>
                                                <td style="width: 10%">
                                                    <div class="checkbox">
                                                        <label>
                                                            <input type="checkbox" class="icheck" name="{{$lists->proptype}}" id="proptype[0]" value="{{$lists->typeId}}">
                                                        }
                                                    </label>
                                                </div>
                                            </td>
                                            <td>{{$lists->prop_name}}</td>
                                            @if($lists->property->builder)
                                            <td>{{$lists->property->builder->name}}</td>
                                            @endif
                                            <td>{{$lists->property->proLocation->locations}}</td>
                                            <td>{{$lists->property->proLocality->name}}</td>
                                        </tr>
                                        @endforeach
                                    </tbody>

【问题讨论】:

    标签: laravel eloquent laravel-6 laravel-6.2


    【解决方案1】:

    似乎你的 projectType 模型中的 propertytype() 关系是错误的,因为它说 ProjectType 属于 ProjectType,它不是一个关系,需要删除。

    此外,您的列表关系应该类似于:

    public function listing()
    {
        return $this->belongsToMany(Listing::class, 'properties_searchable_tags','typeId','lstId');
    }
    

    您的编辑控制器/视图仅用于渲染和压缩从控制器到视图的数据,要更新您需要“更新”功能和另一个带有“POST”方法的路由,当然还有视图中的表单。

    如果您不打算在视图中使用表单,那么您可能需要使用 AJAX 更新,您仍然需要在控制器中“更新”功能和接受“POST”方法的路由。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-24
      • 2021-09-20
      • 2015-08-01
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      • 2018-04-10
      • 1970-01-01
      相关资源
      最近更新 更多