1. 程式人生 > >Laravel將後臺查詢到的資料傳送給前臺模板

Laravel將後臺查詢到的資料傳送給前臺模板

    (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