Laravel将后台查询到的数据发送给前台模板

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33867131/article/details/86137767
    (1)后台查询数据
        public function root()
        {
            // 查询当前用户创建的所有项目
            $projects = request()->user()->projects()->get();
            // 将查询结果赋值给前端HTML页面(使用compact()函数)
            return view('welcome', compact('projects'));
        }	
    (2)前台遍历数据
	@foreach($projects as $project)
	    {{  $project->name }}
	@endforeach


下面为拓展:
@section('content')
    <div class="row">
        <div class="col-md-10 offset-md-1">
            <div class="card panel-default">
                <div class="card-header">收货地址列表</div>
                <div class="card-body">
                    <table class="table table-bordered table-striped">
                        <thead>
                        <tr>
                            <th>收货人</th>
                            <th>地址</th>
                            <th>邮编</th>
                            <th>电话</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        @foreach($addresses as $address)
                            <tr>
                                <td>{{ $address->contact_name }}</td>
                                <td>{{ $address->full_address }}</td>
                                <td>{{ $address->zip }}</td>
                                <td>{{ $address->contact_phone }}</td>
                                <td>
                                    <button class="btn btn-primary">修改</button>
                                    <button class="btn btn-danger">删除</button>
                                </td>
                            </tr>
                        @endforeach
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
@endsection

猜你喜欢

转载自blog.csdn.net/qq_33867131/article/details/86137767