首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在数据表中同一个表中多个外键与Laravel的关系

在数据表中同一个表中多个外键与Laravel的关系
EN

Stack Overflow用户
提问于 2021-12-16 18:32:12
回答 1查看 585关注 0票数 0

我现在用的是Laravel 8,口才和数据几天。实际上,我发现如何用datatable和datatable中的一个表显示datatable。

我现在的问题是,我需要在列中显示名称(您可以在"appellations“表中找到),而不是他的外键。

我的数据的实际结果的截图

这是我实际拥有的内容的屏幕截图,我的目标是显示"abrege“,您可以在我用红色显示的第2列的"appellations”表中找到它("App“和”abrege“),而不是显示它们的外键。

我的代码实际上是:

我的"SousParcelle“模型:

代码语言:javascript
复制
class SousParcelle extends Model
{
    use HasFactory;
    protected $table = 'sous_parcelles';
    protected $fillable = ['id',
        'parcelle_id',
        'cepage_id',
        'appellation_cvi_id',
        'dist_entre_rang',
        'dist_sur_rang',
        'densite_theorique',
        'superficie',
        'annee_plantation',
        'modification_par',
        'modification_le',
        'validee',
        'affectation_appellation_id',
        'affectation_superficie'];

    public function appellation(){
        return $this->belongsTo(Appellation::class, 'affectation_appellation_id', 'id');
    }
}

我的“称谓”模型:

代码语言:javascript
复制
class Appellation extends Model
{
    use HasFactory;
    protected $table = 'appellations';
    protected $fillable = ['id', 'libelle', 'abrege', 'siqo_id'];

    public function sousparcelles(){
        return $this->hasMany(Appellation::class, 'affectation_appellation_id', 'id');
    }
}

我的"SousParcellesController“数据表控制器:

代码语言:javascript
复制
class SousParcellesController
{
    public function index()
    {
        return view('welcome');
    }

    public function getSousParcelles(Request $request)
    {
        if ($request->ajax()) {
            $query = SousParcelle::with(['appellation' => function($query){
                $query->select('abrege');
            }]);

            return DataTables::of($query)
            ->addIndexColumn()
            ->addColumn('App engagee', function(SousParcelle $sousparcelles){
                return $sousparcelles->appellations()->abrege;
            })
            ->make(true);
        }
    }
}

我的观点"welcome.blade.php“的脚本:

代码语言:javascript
复制
        <table id="sousparcelles" class="table table-bordered yajra-datatable">
            <thead>
            <tr>
                <th>Référence Cadastrale</th>
                <th>Commune</th>
                <th>Plantation</th>
                <th>Cepage</th>
                <th>App CVI</th>
                <th>Sup CVI</th>
                <th>App engagee</th>
                <th>Sup engagee</th>
            </tr>
            </thead>
            <tbody>
            </tbody>
        </table>


 <script type="text/javascript">
            function load_table(appellation_id) {

                if ($.fn.dataTable.isDataTable('.yajra-datatable')) {
                    $('.yajra-datatable').DataTable().destroy();
                }

                var table = $('.yajra-datatable').DataTable({
                    processing: true,
                    serverSide: true,
                    searching: true,

                    ajax: "{{ route('sousparcelles.liste') }}",
                    columns: [
                        {data: 'dist_entre_rang', name: 'dist_entre_rang'},
                        {data: 'parcelle_id', name: 'parcelle_id'},
                        {data: 'annee_plantation', name: 'annee_plantation'},
                        {data: 'cepage_id', name: 'cepage_id'},
                        {data: 'appellation_cvi_id', name: 'appellation_cvi_id', type: 'selection'},
                        {data: 'superficie', name: 'superficie'},
                        {data: 'affectation_appellation_id', name: 'appellations.abrege'},
                        {data: 'affectation_superficie', name: 'affectation_superficie'},
                    ]
                });
            }
            $(function () {
                load_table(5);
            });

            function change_appellation() {
                load_table(6);
            }
</script>

有人能告诉我这段关系的沟通吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-22 08:51:13

更新!我找到解决办法了!我决定改变整个要求,现在一切顺利。这是我的解决方案:

更改后的部件un控制器(有更多的“连接”到其他表,“where”只包含已连接帐户的条目):

代码语言:javascript
复制
    public function getSousParcelles(Request $request)
    {
        if ($request->ajax()) {
            $id = Auth::id();
            $userData = DB::table('sous_parcelles')
                ->join('appellations as affectation', 'affectation.id', '=', 'sous_parcelles.affectation_appellation_id')
                ->join('appellations as engagee', 'engagee.id', '=', 'sous_parcelles.appellation_cvi_id')
                ->join('parcelles', 'parcelles.id', '=', 'sous_parcelles.parcelle_id')
                ->join('users', 'users.id', '=', 'parcelles.user_id')
                ->select('sous_parcelles.*', 'affectation.abrege as affectationlib', 'engagee.abrege as engageelib', 'parcelles.campagne as parcellescamp')->where('user_id', '=', $id);

            return Datatables::of($userData)->filter(function ($query) use ($request) {
            })->make(true);
        }

这是我的新JS脚本:

代码语言:javascript
复制
var table = $('.yajra-datatable').DataTable({
                    processing: true,
                    serverSide: true,
                    searching: false,

                    ajax: "{{ route('sousparcelles.liste') }}",
                    columns: [
                        {data: 'parcellescamp', name: 'parcelles.campagne'},
                        {data: 'parcelle_id', name: 'sous_parcelles.parcelle_id'},
                        {data: 'annee_plantation', name: 'sous_parcelles.annee_plantation'},
                        {data: 'cepage_id', name: 'sous_parcelles.cepage_id'},
                        {data: 'affectationlib', name: 'affectation.abrege'},
                        {data: 'superficie', name: 'sous_parcelles.superficie'},
                        {data: 'engageelib', name: 'engagee.abrege'},
                        {data: 'affectation_superficie', name: 'sous_parcelles.affectation_superficie'},
                    ]
                });

所有其他文件都不会因此问题而更改。

我希望它能对某人有用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70383972

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档