我正在尝试检索数据并将其显示在laravel 5.3中的页面中

时间:2016-12-08 08:54:12

标签: laravel laravel-5.3

以下是显示数据的显示页面代码:

@extends('layouts.app')

@section('content')


<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div 
                    class="panel-heading">subnet_behind_clients
                </div>

                <div class="panel-body">
                    <form class="form-horizontal" role="form" method="GET" action="{{ url('/subnet_behind_clients') }}">
                        {{ csrf_field() }}
                            <table class="table table-hover">
                                <thead>
                                    <tr>
                                        <th></th>
                                        <th>client_id</th>
                                        <th>ip_address</th>
                                        <th>netmask</th>
                                    </tr>
                                </thead>
                                <tbody>
                                @foreach($clients as $client)
                                    <tr>
                                        <td>{{ ++$i }}</td>
                                        <td>{{ $client->ip_address }}</td>
                                        <td>{{ $client->netmask }}</td>
                                        <td>
                                            <button type = "button" class = "btn btn-danger " data-target = "del/{{$client->id}}">Delete</button> 
                                        </td>
                                    /tr>
                                @endforeach
                            </tbody>
                        </table>
                    <div class="panel-body">
                        <a href="{{ url('/home') }}">Home</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

以下是我的控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Subnet_behind_client;
use DateTime;

class Subnet_Behind_ClientController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {

                /* $datas = Subnet_behind_client::all();
                $data = $datas->first();*/

                $data = Subnet_behind_client::first();

                return view('subnet_behind_clients',compact('data'));
    }


    public function create(Request $request)
    {

                $data = new Subnet_behind_client;
                return view('subnet_behind_clients1',compact('data'));
                Subnet_behind_client::create([
                    //'client_id' => $request->input('1'),
                    'ip_address' => $request->input('ip_address'),
                    'netmask' => $request->input('netmask'),
                ]);
    }



    public function store(Request $request)
    {
                $datas = Subnet_behind_client::all();
                $data = new Subnet_behind_client;
                $data->client_id = '1';
                $data->ip_address = $request->ip_address;
                $data->netmask = $request->netmask;

                $data->save();
                return back();

    }

    public function show(Request $request)
    {
                $clients = Subnet_behind_client::all();
                return view('view2',compact('clients'));
    }
            public function destroy($id)
            {
                $clients = Subnet_behind_client::findOrFail( $id );
                $clients->delete();
                return view('view2',compact('clients'));
            }  
    }

请告诉我foreach中的代码有什么问题 第一部分是视图页面 第二页是控制器部分 我编辑了我之前的帖子。 Plz看一看,让我知道。

3 个答案:

答案 0 :(得分:0)

试试这个

控制器

use App\Subnet_behind_clients

public function show(Request $request)
{
     $datas = Subnet_behind_clients::all();
     return view('view2',compact('datas'));
}

视图

@foreach($datas as $data)
<tr>

    <td>{{ ++$i }}</td>

    <td>{{ $data->ip_address }}</td>

    <td>{{ $data->netmask }}</td>
</tr>

@endforeach

答案 1 :(得分:0)

如果你有Class 'App\Http\Controllers\Subnet_behind_clients' not found

在控制器

中的命名空间后使用use App\Subnet_behind_clients(模型名称空间)

您忘记在控制器中添加模型的名称空间

答案 2 :(得分:0)

您的控制器应如下所示:

use App\Subnet_behind_clients;

public function show(Request $request)
{
    $clients = Subnet_behind_clients::all();
    return view('view2', compact('clients '));
}

并查看:

@foreach($clients as $client)
    ....
@endforeach

切勿在视图中使用Eloquent或Query Builder。