在Laravel中显示数据以一对多关系查看

时间:2019-05-21 04:31:13

标签: mysql laravel

我有2张桌子:

Type: id, name
Question: id, content, type_id

我已经使用过滤器功能显示数据,但是当我尝试显示类型名称和级别名称时,出现此错误

  

试图获取非对象的属性“名称”

当我转储($ data)时,我得到了:

LengthAwarePaginator {#217 ▼
  #total: 6
  #items: Collection {#229 ▼
    #items: array:6 [▼
      0 => Question {#230 ▶}
      1 => Question {#231 ▶}
      2 => Question {#232 ▶}
      3 => Question {#233 ▶}
      4 => Question {#234 ▶}
      5 => Question {#235 ▶}
    ]
  }
  #path: "http://127.0.0.1:8000/test"
  #query: array:1 [▶]
  ...
}

这是我的模特 Question.php

protected $table = 'question';
    public $primaryKey = 'id';
    public $timestamps = false;

    public function types()
    {
        return $this->hasMany(Type::class,'type_id','id');
    }

Type.php

protected $table = 'type';
    public $primaryKey = 'id';
    public $timestamps = false;
    protected $fillable = ['type_id'];

    public function questions(){
        return $this->belongsTo(Question::class);
    }

TestController.php

public function getTest(Request $request){
        $types = Type::all();
        $model = Question::where('id', '>', 0);

        if (isset($request->type))
            $model = $model->where('type_id', $request->type);

        $data = $model->paginate(15)->appends(request()->query());

        return view('filter', compact( 'data', 'request', 'types'));
    }

这是我的观点

<form method="get">
    <select name="type" onchange="this.form.submit()">
        <option value="">All types</option>
        @foreach ($types as $item)
            <option value="{{ $item->id }}" @if ($request->type == $item->id) selected @endif>{{ $item->name }}</option>
        @endforeach
    </select>
</form>
<table border="1">
    <tr>
        <th>ID</th>
        <th>Content</th>
        <th>Type</th>
    </tr>
    @foreach ($data as $q)
        <tr>
            <td>{{ $q->id }}</td>
            <td>{{ $q->content }}</td>
            <td>{{ $q->type_id->name }}</td>
        </tr>
    @endforeach
</table>

4 个答案:

答案 0 :(得分:3)

尝试一下:

问题模型:

class Question extends Model
{
    protected $table = 'question';
    public $timestamps = true;
    protected $guarded = [];

    public function type(){
        return $this->hasOne(Type::class, 'id', 'type_id');
    }
}

在类型模型中:

class Type extends Model
{
    public $timestamps = true;
    protected $table = 'type';
    protected $guarded = [];
}

在控制器中:

public function getQuestions(Request $request)
{
    $type = $request->input('type', 0);
    $builder = Question::where('id','>', 0)->with('type');

    if ($type !== 0) {
        $builder->where('type_id', '=', $type);
    }

    $data = $builder->paginate(15);
    return response()->json($data);
}

刀片式服务器

$data[i]->content
$data[i]->type->name

答案 1 :(得分:1)

 <td>{{ $q->type_id->name }}</td>

选中此行,与类型模型无关。

$q->type->name

也许您想做类似的事情,但是可以使用hasMany来做,您需要使用一对一的关系

答案 2 :(得分:1)

我本可以这样做

enter image description here

ModelQuestion:

class ModelQuestion extends Model
{
    public $timestamps = true;
    protected $table = 'question';
    protected $guarded = [];


    public function scopeIdGreaterThan($query, $id){
        return $query->where('id','>', $id);
    }

    public function scopeOfType($query, $type){
        return $query->where('type_id', '=', $type);
    }


    public function type(){
        return $this->hasOne(ModelType::class, 'id', 'type_id');
    }
}

ModelType:

class ModelType extends Model
{
    public $timestamps = true;
    protected $table = 'type';
    protected $guarded = [];
}

控制器中的功能

public function getQuestions(Request $request) {
    $type = (isset($request->type)) ? $request->type : 0;
    $perPage = 15;

    $data = ModelQuestion::with('type')->idGreaterThan(0);

    //whatever condition you want
    if ($type !== 0) $data->ofType($type);

    $data = $data->paginate($perPage);
    return response()->json($data);
}

输出:https://pastebin.com/JdSq5kSb

在刀片中,您可以像这样:

$data[i]->content
$data[i]->type->name

注意:我不知道您要在$request->query

中添加什么内容

答案 3 :(得分:0)

谢谢你们。我找到了解决方案:

@foreach ($data as $q)
    <?php $typeName = App\Type::find($q->type_id); ?>
    <tr>
        <td>{{ $q->id }}</td>
        <td>{{ $q->content }}</td>
        <td>{{ $typeName->name }}</td>
    </tr>
@endforeach