我想从数据库中检索数据,需要在包含编辑和删除按钮的表中显示?

时间:2018-03-15 08:27:10

标签: php mysql laravel

我的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>
$results = DB::table('category')
    <table>
        <tr>
            <th>Category Name</th>
        </tr>
                foreach ($category as $results)
                {
                    <tr>
                        <td>
                            {$results -> $dis}
                        </td>
                    </tr>
                }
    </table>
</div>
</body>
</html>

我的分类模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

}

我的管理员控制器

<?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()
    {
        $a = '';
        return view('admin/admin',[
            'category' => $a
        ]);

        // echo "here";
    }

    /**
     *
     */
    public function addCategory()
    {
        $category = new Category;
        $category->category_name = Input::get('category');

        $category->save();
        return Redirect::to('admin');
    }
}

我想从db中获取所有类别,我需要在表格中使用删除和编辑按钮在同一页面中显示它吗? 我尝试了上面的代码但我无法显示它显示我显示的每个循环而不是表

我怎样才能实现这一目标?

4 个答案:

答案 0 :(得分:0)

你需要尝试show方法

public function show()
    {
        $category=Category::all();
        return view('your view name')->withCategory($category);

    }

并在视图中接收变量

 @foreach($category as $value)
   {{ $value->your field name }}
    @endforeach

或者像那样单独的视图重定向

return redirect()->route('separate route name')->withCategory($category);

答案 1 :(得分:0)

我建议你在存储库中写下你的所有请求。您的控制器不得包含逻辑代码或数据库请求。 使用Eloquent执行SELECT可以这样做:

$query = (new Category())->newQuery();

return $query->select('*')->get();

如果您想获得有关查询构建器的更多信息,请阅读文档:Database: Query Builder

答案 2 :(得分:0)

您的观点中有拼写错误

<?php
$results = DB::table('category')->get();
?>
<table>
    <tr>
        <th>Category Name</th>
    </tr>
           @foreach ($results as $result)

                <tr>
                    <td>
                        {{$result -> $dis}}
                    </td>
                </tr>
            @endforeach
</table>

您可以阅读更多相关信息here

答案 3 :(得分:0)

您想要从数据库中检索数据,是否需要在包含编辑和删除按钮的表格中显示?它很简单,只需将按钮放在foreach中

    <table>
    <tr>
        <th>Category Name</th>
    </tr>
            foreach ($category as $results)
            {
                <tr>
                    <td>
                        {{$results -> your_data_column}}
                    </td>
                    <td>
                       <a href='edit?{{$results->id}}'><button>edit</button></a>
                       <a href='delete?{{$results->id}}'><button>delete</button></a>
                    </td>
                </tr>
            }
     </table>

你的控制器

    public function show()
    {
       $data['category'] = DB::table('category')->get();
       return view('admin/admin',$data);
    }