Search code examples
laravellaravel-5.3

I am trying to retrieve data and display it in a page in laravel 5.3


Following is my show page code to display the data:

@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

and following is my controller:

<?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'));
            }  
    }

plz let me know what is wrong for the code in foreach the first part is the view page and the second page is for the controller part I made edited my earlier post. Plz take a look and let me know.


Solution

  • You controller should look like this:

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

    And view:

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

    Never use Eloquent or Query Builder in views.