【问题标题】:How to show records from two different tables in same view using Laravel?如何使用 Laravel 在同一视图中显示来自两个不同表的记录?
【发布时间】:2020-08-20 13:57:30
【问题描述】:

我想在同一个视图中显示两个不同的表记录。我该怎么做?

模型数字化顺序

namespace App;
use Illuminate\Database\Eloquent\Model;

class Digitizingorder extends Model
{
    protected $table="digitizing_orders";
    protected $fillable =['id','order_name','height','width','image', 'order_placement','required_format','order_fabric','instruction','user_id'];
} 

模型向量顺序

namespace App;

use Illuminate\Database\Eloquent\Model;

class Vectororder extends Model
{
    protected $table ="vector_orders";
    protected $fillable =['id','vector_name','height','width','image','color', 'order','order_fabric','instruction','user_id'];

HTML 视图

<table id="selection-datatable" class="table table-sm mb-2">
    <thead>
        <tr>
            <th>S.N </th>
            <th>Order No</th>
            <th>Design Name </th>
            <th>Order Type</th>
            <th>Received Date</th>
            <th>Released Date   </th>
            <th>price</th>
            <th>Order Details</th>
        </tr>
    </thead>
    <tbody>

    </tbody>
</table>

【问题讨论】:

  • 你的问题看不懂。。请展示你做了什么,你想要什么,先自己尝试
  • 其实我想在同一个视图中显示两个不同表的记录

标签: laravel


【解决方案1】:

在您返回视图的方法中调用您的模型:

public function view(){
$value1 = App\Digitizingorder::all();
$value2 = App\Vectororder::all();
return view('home')->with(['Digitizingorder'=>$value1,'Vectororder'=>$value2]);

然后在你的 home.blade.php 中你可以像这样得到它:

<p>{{$Digitizingorder}}<p>
<p>{{$Vectororder}}<p>

【讨论】:

    【解决方案2】:

    您似乎有两个具有不同表字段名称的模型。但在视图中,您正在显示表中的公共字段。因此,您只需将模型数据传递给视图并逐个迭代它们即可轻松完成。

    你能做什么?

    $digitizingorders = App\Digitizingorder::all();

    $vectororders = App\Vectororder::all();

    return view('your-view-file-name')->with(compact("digitizingorders","vectororders"));

    现在,这两个变量将在您的视图中可用,您可以像下面这样轻松地迭代值。

    <table id="selection-datatable" class="table table-sm mb-2">
        <thead>
            <tr>
                <th>S.N </th>
                <th>Order No</th>
                <th>Design Name </th>
                <th>Order Type</th>
                <th>Received Date</th>
                <th>Released Date   </th>
                <th>price</th>
                <th>Order Details</th>
            </tr>
        </thead>
        <tbody>
            @foreach($digitizingorders as $dorder)
              <tr>
                <th>S.N </th>
                <th>Order No</th>
                <th>Design Name </th>
                <th>Order Type</th>
                <th>Received Date</th>
                <th>Released Date   </th>
                <th>price</th>
                <th>Order Details</th>
            </tr>
            @endforeach
            @foreach($vectororders as $vorder)
              <tr>
                <th>S.N </th>
                <th>Order No</th>
                <th>Design Name </th>
                <th>Order Type</th>
                <th>Received Date</th>
                <th>Released Date   </th>
                <th>price</th>
                <th>Order Details</th>
            </tr>
            @endforeach
        </tbody>
    </table>
    

    【讨论】:

    • 其实我有一个问题,我可以用一个循环来处理两个表吗?
    • 你的解决方案完全错误!!你不能在内部使用紧凑的方法
    • @arya_la 你的评论“你不能在内部使用紧凑的方法”!你是在哪里拿到的?请查看 laravel 文档。仅供参考,请查看stackoverflow.com/questions/22412832/laravel-compact-and-with
    • @Zubair 要在一个循环中完成,您需要合并两个数据集合。一种方法是将集合转换为数组,然后将这些数组合并为一个数组。接下来,将其传递给视图。 laravel.com/docs/7.x/collections#method-toarray
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-30
    • 1970-01-01
    • 2015-07-12
    相关资源
    最近更新 更多