2

i am new in php especially laravel framework. could be someone help me to resolve it? i don't know how to retrieve data from database using php: laravel framework.

profile.blade.php file

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

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

}
7
  • 1
    You'll have to turn on debugging in Laravel to see what the error is. Here's how: laravel.com/docs/5.1/errors#configuration Commented Nov 16, 2015 at 7:53
  • Why aren't you using seeding? Commented Nov 16, 2015 at 8:05
  • @madforstrength i tried use it but it said, invalid argument exception Commented Nov 16, 2015 at 8:21
  • The code you posted doesn't show how are you trying to retrieve the data in your view? Commented Nov 16, 2015 at 8:24
  • how can it retrieve it @Jeemusu ? can u help me Commented Nov 16, 2015 at 8:29

2 Answers 2

1

In your view you will hav to do the following

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

the name property is your table in database This is if you are using the blade engine else use normal php tags

And then in your route you have to tell what method in you controller you want to use eventhough your function is called index it is good pratice to specify the function on your route. To have a good code strutuce remember that Classes always starts with capital letters

I would strongly suggest you to use eloquent instead of the query builder

Sign up to request clarification or add additional context in comments.

3 Comments

That won't work since the query builder returns an array of arrays, so rather $student['name'] :)
@Phroggyy no it dont returns an array see this laravel.com/docs/5.1/queries
@Phroggyy no problem :)
0

First of all, I would recommend you look through Laravel 5 Fundamentals in order to learn a bit about the framework. Now, a few things.

  1. In Laravel, when writing migrations, don't name them things like Student.php. Use the command php artisan make:migration create_students_table. The standard naming convention is to describe the action of the migration.
  2. If you want to insert certain data, don't do it in your migration. Instead, look at setting up a seeder.
  3. For your Student I would very much recommend that you create a model. Laravel is an MVC framework, and if you're not used to the MVC structure, this might be a bit different to what you're used to. A model is simply a representation of some collection of data that is logically bundled together. In your database, this data is what ends up being in one row. You could then do Student::all() to retrieve all data from your students table.
  4. The naming convention in Laravel dictates the use of camelCase. Hence, all your files should be named, just as your classes, with camel case. This is to say StudentController.php and class StudentController
  5. In your routes.php, specify which method to call as well: Route::get('student', ['as' => 'student', 'uses' => 'StudentController@index']);

Again, please have a look at the Laravel 5 fundamentals series linked in the beginning. You will learn to handle Laravel properly there.

1 Comment

thank u guys for your sharing !! finally, i did it ! :')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.