簡體   English   中英

如何使用 Laravel 檢索數據

[英]how to retrieve data using laravel

我是 php 的新手,尤其是 laravel 框架。 有人可以幫我解決嗎? 我不知道如何使用 php: laravel 框架從數據庫中檢索數據。

profile.blade.php 文件

<!doctype html>
<html>


<head>
    <title>Student Profile</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <style>

        div{
            background-color:lightcyan;
            margin-left: 180px;
            margin-right: 180px;
            margin-bottom: 10px;
            background-repeat: no-repeat;
            position:inherit;
            padding:2px;
            font-size: 14px;
        }

        body{
            margin-top: 5px;
            margin-bottom: 5px;
            background-image:url("image/bg.jpg");
            background-repeat: no-repeat;
            background-attachment: fixed;
        }


        h1 {
            font-family: "Georgia", Times, serif;
            font-size: 42px;
            color: firebrick;
            text-indent: 12%;
        }

        table, th, td {
            border: 1px solid lightcoral;
            text-transform: uppercase;
            text-align: left;
        }

        .header {
            background-color: lightcoral;
            width: 15%;
        }
        footer{
            background-color: black;
            color: white;
            font-weight: bold;
            padding: 10px;
            text-align: center;
        }
        #navbar {
            width:100%;
            margin-left:-2px;
            height:40px;
            background-color:rosybrown;
        }

        #navbar ul {
            width:100%;
            margin:0 auto 0 auto;
        }

        #navbar ul li {
            float:left;
            color:black;
            padding:0 20px 0 20px;
            font-size: 14px;
            font-weight: bold;
            height:40px;
            display:block;
            line-height:40px;
            text-align:center;
            cursor:pointer;
        }

        #navbar ul li:hover {
            background-color:#CCC;
        }
        li{
            text-transform: capitalize;
        }
    </style>
</head>


<body>
<div>

<h1>GUARDIAN PORTAL</h1><br>
<div id="navbar">

    <ul>
        <li>Home</li>
        <li>Student profile</li>
        <li>Exam result</li>
        <li>academic supervisor/Advisor</li>
        <li>change password</li>
        <li>contact us</li>
        <li>logout</li>
    </ul>

</div>

    <fieldset>
        <legend align="center"><b><font color="red">Student Profile</font></b></legend>
        <table width="100%">

            <tr>
                <td class="header">student id</td>
                <td></td>
            </tr>
            <tr>
                <td class="header"> name</td>
                <td></td>
            </tr>
            <tr>
                <td class="header">ic no</td>
                <td ></td>
            </tr>
            <tr>
                <td class="header">programme</td>
                <td></td>
            </tr>
            <tr>
                <td class="header">semester</td>
                <td></td>
            </tr>

            <tr>
                <td class="header">faculty</td>
                <td></td>
            </tr>

        </table>
        <table width="100%">
            <tr>
                <td class="header" width="15%">tel no</td>
                <td width="35%"></td>
                <td class="header" width="15%">email</td>
                <td width="35%"></td>
            </tr>
            <tr>
                <td class="header">race</td>
                <td></td>
                <td class="header">religion</td>
                <td></td>
            </tr>
            <tr>
                <td class="header">gender</td>
                <td></td>
                <td class="header">marital status</td>
                <td></td>
            </tr>
            <tr>
                <td class="header" width="15%">college name</td>
                <td width="35%"></td>
                <td class="header" width="15%">college no</td>
                <td width="35%"></td>
            </tr>
        </table>
        <table width="100%">
            <tr>
                <td class="header"><b>status</b></td>
                <td width="85%"></td>
            </tr>
        </table>
        <table width="100%">
            <td colspan="2" align="center"><b>permanent address</b></td>
            <td colspan="2" align="center"><b>current address</b></td>
            </tr>
            <tr>
                <td class="header" width="5%">address</td>
                <td width="35%"></td>
                <td class="header" width="5%">address</td>
                <td width="35%"></td>
            </tr>
            <tr>
                <td class="header">city</td>
                <td></td>
                <td class="header">city</td>
                <td></td>
            </tr>
            <tr>
                <td class="header">state</td>
                <td></td>
                <td class="header">state</td>
                <td></td>
            </tr>
            <tr>
                <td class="header">postcode</td>
                <td></td>
                <td class="header">postcode</td>
                <td></td>
            </tr>
            <tr>
                <td class="header">country</td>
                <td></td>
                <td class="header">country</td>
                <td></td>
            </tr>
        </table>
    </fieldset>
    </div>
</body>
</html>

學生控制器.php

public function index()
    {
        $table = DB::table('student')->get();

        return view('profile', ['student_id' => $table]);
    }

路由文件

Route::get('student', array('as'=>'student', 'uses'=>'studentcontroller'));

數據庫:student.php

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations
\Migration;

class Student extends Migration {

    public function up()
    {
        Schema::create('student',
            function(Blueprint $table)
            {
                $table->string('student_id');
                $table->string('student_name');
                $table->string('student_ic');
                $table->string('program');
                $table->string('semester');
                $table->string('faculty');
                $table->string('student_tel_no');
                $table->string('student_email');
                $table->string('student_marital_status');
                $table->string('student_religion');
                $table->string('student_race');
                $table->string('student_gender');
                $table->string('college_name');
                $table->string('college_no');
                $table->string('status');
                $table->string('p_address');
                $table->string('p_city');
                $table->string('p_state');
                $table->string('p_postcode');
                $table->string('p_country');
                $table->string('c_address');
                $table->string('c_city');
                $table->string('c_state');
                $table->string('c_postcode');
                $table->string('c_country');
                });
        DB::table('student')->insert(array(

        'student_id'=>'uk27721',

        'student_name'=>'sumaliza ismail',

        'student_ic'=>'123456789012',

        'program'=>'program',
        'semester'=>'1',

        'faculty'=>'faculty',

        'student_tel_no'=>'1',

        'student_email'=>'email',

        'student_marital_status'=>'status',

        'student_religion'=>'student_religion',

        'student_race'=>'student_race',

        'student_gender'=>'student_gender',

        'college_name'=>'college_name',

        'college_no'=>'college_no',

        'status'=>'status',

        'p_address'=>'p_address',

        'p_city'=>'p_city',

        'p_state'=>'p_state',

        'p_postcode'=>'p_postcode',

        'p_country'=>'p_country',

        'c_address'=>'c_address',

        'c_city'=>'c_city',

        'c_state'=>'c_state',

        'c_postcode'=>'c_postcode',

        'c_country'=>'c_country'
    ));
        DB::table('student')->insert(array(

        'student_id'=>'uk27771',

        'student_name'=>'sumaliza ismail',

        'student_ic'=>456543456456,

        'program'=>'program',
        'semester'=>'1',

        'faculty'=>'faculty',

        'student_tel_no'=>'1',

        'student_email'=>'email',

        'student_marital_status'=>'status',

        'student_religion'=>'student_religion',

        'student_race'=>'student_race',

        'student_gender'=>'student_gender',

        'college_name'=>'college_name',

        'college_no'=>'college_no',

        'status'=>'status',

        'p_address'=>'p_address',

        'p_city'=>'p_city',

        'p_state'=>'p_state',

        'p_postcode'=>'p_postcode',

        'p_country'=>'p_country',

        'c_address'=>'c_address',

        'c_city'=>'c_city',

        'c_state'=>'c_state',

        'c_postcode'=>'c_postcode',

        'c_country'=>'c_country'
    ));


    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('student');
    }

}

在您看來,您必須執行以下操作

@foreach($student_id as $student)
<p> {{ $student->name }} </p>
@endforeach

name 屬性是你在數據庫中的表這是如果你使用刀片引擎,否則使用普通的 php 標簽

然后在你的路線中,你必須告訴你想要使用控制器中的什么方法,即使你的函數被稱為 index ,在你的路線上指定函數是一種很好的做法。 要擁有一個好的代碼結構,請記住類總是以大寫字母開頭

我強烈建議您使用 eloquent 而不是查詢構建器

首先,我建議您閱讀Laravel 5 Fundamentals以了解有關該框架的一些知識。 現在,有幾件事。

  1. 在 Laravel 中,在編寫遷移時,不要將它們命名為Student.php之類的東西。 使用命令php artisan make:migration create_students_table 標准命名約定是描述遷移的操作。
  2. 如果要插入某些數據,請不要在遷移中進行。 相反,請查看設置播種機
  3. 對於您的Student我非常建議您創建一個模型。 Laravel 是一個 MVC 框架,如果你不習慣 MVC 結構,這可能和你習慣的有點不同。 模型只是邏輯捆綁在一起的某些數據集合的表示。 在您的數據庫中,這些數據最終排在一行。 然后,您可以執行Student::all()從您的students表中檢索所有數據。
  4. Laravel 中的命名約定規定了駝峰命名法的使用。 因此,你的所有文件都應該像你的類一樣,用駝峰命名。 這是說StudentController.phpclass StudentController
  5. 在你的routes.php ,指定調用哪個方法: Route::get('student', ['as' => 'student', 'uses' => 'StudentController@index']);

同樣,請查看開頭鏈接的 Laravel 5 基礎知識系列。 您將在那里學習如何正確處理 Laravel。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM