简体   繁体   中英

how to retrieve data from db and display it in the laravel?

i want to display the data in a table with edit and delete button for each record in laravel.

i have to display that in the existing page.

admin controller

<?php

namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Category;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Auth;

class AdminController extends Controller
{
    public function show()
    {
       return view('admin/admin');
    }

    public function addCategory()
    {
        $category=new Category;
        $category->category_name = Input::get('category');
        $category->save();
        return view('admin/admin');
    }
}

my category model

namespace App;

use Illuminate\\Database\\Eloquent\\Model;

class Category extends Model
{
    protected $table = "category";
    protected $fillable = [
        'category_name',
    ];
}

admin.blade.php

<!DOCTYPE html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <style>
        .bod{
            background-color:#f8f8f8 ;
            border-shadow: 5px 10px 20px white inset;
            border-width:5px;
            margin-top:-12px;
        }
        .h1{
            font-family:"Book Antiqua";
            font-size: 90px;
            color: red;
            text-align: center;
        }
        div {
            border-radius: 5px;
            /*background-color: #f2f2f2;*/
            /*background-color: #7CA8C1  ;*/
            padding: 20px;
            position: relative;
            border:2px solid white;
            height: 200px;
            width: 50%;
            margin-left: 50px;
        }
        .txt{
            width: 150px;
            height:25px;
            border: 2px solid white;
        }
        .btn{
            width: 100px;
            height:28px;
            font-weight: bold;
            border: 2px solid white;
        }
    </style>
</head>
<body class="bod">
<h1 class="h1">Admin</h1>
<div align="left">

    <form action="{{ route('addCategory') }}" method="get">
        {{ csrf_field() }}
        <label for="category" style="font-family: 'Book Antiqua';font-size: 48px;color: #9999FF;">Category</label> <br/>
        <table class="table ">
            <tr>
                <td align="center"><input class="txt" type="text" name="category" id="category" placeholder="Enter the Category" ></td>
                <td align="center"></td>
                <td align="center"> <input type="submit" name="add" id="add" value="ADD" class="btn"></td>
            </tr>
        </table>
    </form>
</div>
</body>
</html>

i want to display all categories added by the admin in the admin page with update and delete button how can i achieve that?

Address_type and Student_type are your create type.

You forget to create a table Student_tbl to carry your insert data.

You can refer this Demo

create type Address_type as object ( 
    hno char(4), 
    street varchar2(10), 
    city varchar2(10) 
);

create type Student_type as object ( 
    sno char(4), 
    sname varchar2(10), 
    adddress Address_type 
);

CREATE TABLE Student_tbl(
    V1 Student_type
);

insert into Student_tbl values (Student_type('4567',
                                             'Shan',
                                              Address_type('30','aaa','bbb')
                                             )
                               );

SQLFiddle

EDIT

If you want to get your customer type like Address_type you might do like this.

select t.SNO,
       t.SNAME,
       t.adddress.hno,
       t.adddress.street,
       t.adddress.city
from Student_tbl t

SQLFiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM