【问题标题】:Pivot table selection LARAVEL数据透视表选择 LARAVEL
【发布时间】:2020-01-10 14:21:57
【问题描述】:

我有这个问题,我需要通过数据透视表中的 id 获得模式的初始值,但它会返回给我:

stdClass 类的对象无法转换为字符串

这是我的控制器

$filiere = Filiere::all();
$fcount =  count($filiere);
$filiere22 = DB::select('select id from filiere');
foreach ($filiere22 as $filiere2 ){
    $md = DB::select('select intitule from mode_formation where id in(SELECT mode_id from mode_filiere where filiere_id='.$filiere2.')');
} return view('pgsec',compact('md','ssec','s_secteur','filiere','secteur','sec','secteur2','niveau','niv','province','pr','fcount','region','r','op','operateur'));

这是我的刀片

@foreach($filiere as $f)
    <tr class="item{{$f->id}}">
        <td style="font-size: 13px;">
            <a href="f/{{$f->id}}">{{$f->intitule}}</a> 
        </td>
        @foreach($md as $m)
            <td style="font-size: 13px;">{{$m->intitule}}</td>
        @endforeach
    </tr>
@endforeach

【问题讨论】:

  • 映射filieremode_formationmode_filiere tables的模型是什么?此外,这些表中的每一个的外键和引用都会很有用。可以发一下吗?
  • 您可以通过使用 Eloquent 关系以非常简单的方式做到这一点。我想这就是@IGP 询问模型的原因。
  • 是的,谢谢,filiere 表有 id , intitule , mode_formation 有 , id 和 intitule 和 mode_filiere 表有 : filiere_id , mode_id

标签: php laravel pivot pivot-table laravel-blade


【解决方案1】:

所以根据 cmets,您的表格如下所示:

+--------+  +--------------+  +------------------------------+
|filiere |  |mode_formation|  |mode_filiere                  |
+--------+  +--------------+  +------------------------------+
|id (pk) |  |id (pk)       |  |filiere_id (fk to filiere)    |
|intitule|  |intitule      |  |mode_id (fk to mode_formation)|
+--------+  +--------------+  +------------------------------+

基于此和您的代码,我假设如下:

  • Filiere 模型使用 filiere 表。
  • mode_formation 表没有模型。
  • mode_filiere 表没有模型。

第 1 步:为mode_formation 表制作模型。

  1. 运行命令php artisan make:model ModeFormation
  2. 只是为了确保,编辑模型文件(默认位于app/ModeFormation.php)以添加它所代表的表。

第 2 步:在 FiliereModeFormation 模型之间添加 belongsToMany 关系

第 3 步:在控制器中使用 eager loading 以避免运行太多查询

第 4 步:在视图中使用关系

最后,您的模型应如下所示:

# app/Filiere.php
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Filiere extends Model
{
    protected $table = 'filiere';

    public function mode_formations()
    {
        return $this->belongsToMany(ModeFormation::class, 'mode_filiere', 'filiere_id', 'mode_id');
    }
}
# app/ModeFormation.php
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class ModeFormation extends Model
{
    protected $table = 'mode_formation';

    public function filieres()
    {
        return $this->belongsToMany(Filiere::class, 'mode_filiere', 'mode_id', 'filiere_id');
    }
}

你的控制器中的那部分应该是这样的:

// you only need this one variable for what you're trying to accomplish
$filieres = Filiere::with('mode_formations')->get();

你的看法:

@foreach($filieres as $filiere)
    <tr class="item{{ $filiere->id }}">
        <td style="font-size: 13px;">
            <a href="f/{{ $filiere->id }}">{{ $filiere->intitule }}</a> 
        </td>
        @foreach($filiere->mode_formations as $mode_formation)
            <td style="font-size: 13px;">{{ $mode_formation->intitule }}</td>
        @endforeach
    </tr>
@endforeach

【讨论】:

    猜你喜欢
    • 2015-02-14
    • 1970-01-01
    • 2021-08-21
    • 1970-01-01
    • 2018-04-10
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    相关资源
    最近更新 更多