如何使用laravel检索数据

时间:2015-11-16 07:50:07

标签: php laravel-5

我是php的新手,尤其是laravel框架。可能有人帮我解决了吗?我不知道如何使用php:laravel framework从数据库中检索数据。

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>

studentcontroller.php

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

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

route.php

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

}

2 个答案:

答案 0 :(得分:1)

在您的视图中,您将执行以下操作

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

name属性是数据库中的表 这是如果你使用刀片引擎,否则使用普通的php标签

然后在你的路线中你必须告诉你想要使用的控制器中的哪种方法,尽管你的函数被称为索引,但是指定你的路线上的函数是很好的实践。为了获得良好的代码strutuce,请记住Classes总是以大写字母开头

我强烈建议您使用eloquent而不是查询构建器

答案 1 :(得分:0)

首先,我建议您浏览Laravel 5 Fundamentals以了解有关框架的内容。现在,有一些事情。

  1. 在Laravel中,在编写迁移时,不要将它们命名为Student.php。使用命令php artisan make:migration create_students_table。标准命名约定是描述迁移的操作。
  2. 如果您要插入某些数据,请不要在迁移中执行此操作。相反,请查看设置a seeder
  3. 对于您的Student我非常建议您创建一个模型。 Laravel是一个MVC框架,如果你不习惯MVC结构,这可能与你习惯的有点不同。模型只是逻辑捆绑在一起的一些数据集合的表示。在您的数据库中,这些数据最终会在一行中。然后,您可以Student::all()students表中检索所有数据。
  4. Laravel中的命名约定决定了camelCase的使用。因此,所有文件都应该像驼峰一样命名,就像你的类一样。这就是说StudentController.phpclass StudentController
  5. routes.php中,指定要调用的方法:Route::get('student', ['as' => 'student', 'uses' => 'StudentController@index']);
  6. 再次,请看一下开头链接的Laravel 5基础系列。你将学会在那里妥善处理Laravel。