简体   繁体   English

在数据库的相同ID下一一显示多行

[英]Display multiple rows one by one under same id from database

I have a table hobbies tables that has multiple rows under parent ID. 我有一个表hobbies表,在父ID下有多行。 In my view, my records are displayed altogether. 在我看来,我的记录将全部显示。 But I want to display these row records one-by-one or individually. 但是我想一次或一次显示这些行记录。

My Table: 我的桌子:

在此处输入图片说明

Parent.php Parent.php

class Parent extends Model
{
    public function feature() 
    {
        return $this->hasMany(Feature::class, 'product_id');
    }
}

Feature.php Feature.php

class Feature extends Model
{

    public function feat()
    {
        return $this->belongsTo(Parent::class);
    }
}

Controller.php Controller.php

public function display($url)
{
    $parent = Parent::where('url', $url)->firstorfail();
    $feat = Feature::with(['feat','user'])->get();

    return view ('display', compact('parent', 'feat'));
}

display.blade.php display.blade.php

@foreach($feat as $show)
    <img alt="{{ $show->col_name }}" src="{{url($show->col_img) }}"/>
@endforeach

I want to display like this 我想这样显示 在此处输入图片说明

I assume that you want to get a parent (or is it a product) by the given url. 我假设您想通过给定的网址获取父项(或者是产品)。 In your view you want to display all features attached/related to/with this parent. 在您的视图中,您想要显示与此父级关联/相关/与之关联的所有功能。

First of all, change your Models as shown below (assuming your Models are stored under app/ : 首先,如下所示更改您的模型(假设您的模型存储在app/

class Parent extends Model
{
    public function features() 
    {
        return $this->hasMany('App\Feature', 'product_id');
    }
}


class Feature extends Model
{
    public function parent()
    {
        return $this->belongsTo('App\Parent');
    }
}

Now it is enough to search for your parent in your controller: 现在就可以在控制器中搜索父母:

public function display($url)
{
    $parent = Parent::where('url', $url)->firstorfail();
    $feature = Feature::where('parent_id', $parent->id)->first();
    return view ('display', ['parent' => $parent, 'feature' => $feature]);
}

Finally you can access your features in your template-file as shown below: 最后,您可以在模板文件中访问功能,如下所示:

// Show your parent-information
<h1>{{ $parent->name }}</h1>

<img src="{{ $feature->col_img }}" alt="{{ $feature->col_name }}">

I hope that this is what you were searching for! 我希望这就是您要寻找的!

To display random images at the top, middle and bottom of your page, you could use Laravel's array_random-function : 要在页面的顶部,中间和底部显示随机图像,可以使用Laravel的array_random-function

@php
    // store your features in a temporary variable
    $features = $parent->features;

    // collect feature and remove it from array
    $feature1 = array_random($features);
    array_forget($features, $feature1);
    $feature2 = array_random($features);
    array_forget($features, $feature1);
    $feature3 = array_random($features);
@endphp

<!-- Top -->
<img src="{{ $feature1->col_img }}" alt="{{ $feature1->col_name }}">

<!-- Middle -->
<img src="{{ $feature2->col_img }}" alt="{{ $feature2->col_name }}">

<!-- Bottom -->
<img src="{{ $feature3->col_img }}" alt="{{ $feature3->col_name }}">

I don't know if Laravel's array_random works this way . 我不知道Laravel的array_random可以这种方式工作。 So maybe you have to remove the returned item another way. 因此,也许您必须以另一种方式删除返回的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM