Jump to content

Laravel display data from database

Anduin

Hello, I'm trying to learn php with Laravel. I want to get data from my table and display it. This is what I have:

 

form.blade.php

Spoiler

<!doctype html>
<html>
<head>
</head>
<body>
    <form action="/insert" method="post">
    <table>
        <tr>
            {{csrf_field()}}
            <td>Course ID: </td>
            <td><input type="text" name="CourseID"></td>
        </tr>
        <tr>
            <td>Specialty ID: </td>
            <td><input type="text" name="SpecialtyID"></td>
        </tr>
        <tr>
            <td>First Name: </td>
            <td><input type="text" name="FirstName"></td>
        </tr>
        <tr>
            <td>Last Name: </td>
            <td><input type="text" name="LastName"></td>
        </tr>
        <tr>
            <td>Email: </td>
            <td><input type="text" name="Email"></td>
        </tr>
        <tr>
            <td>Faculty Number: </td>
            <td><input type="text" name="FacNumber"></td>
        </tr>
        <tr>
            <td>Education Form: </td>
            <td><input type="text" name="EduForm"></td>
        </tr>
        <tr>
            <td><input type="submit" name="submit" value="Add"></td>
        </tr>
    </table>
</form>

            <table>
                <tr>
                    <td>Course ID</td>
                    <td>Specialty ID</td>
                    <td>First Name</td>
                    <td>Last Name</td>
                    <td>Email</td>
                    <td>Faculty Number</td>
                    <td>Education Form</td>
                    <td>Action</td>
                </tr>
                @foreach($data as $value)
                    <tr>
                        <td> <?php echo $value->CourseID ?> </td>
                        <td> <?php echo $value->SpecialtyID ?> </td>
                        <td> <?php echo $value->FirstName ?> </td>
                        <td> <?php echo $value->LastName ?> </td>
                        <td> <?php echo $value->Email ?> </td>
                        <td> <?php echo $value->FacNumber ?> </td>
                        <td> <?php echo $value->EduForm ?> </td>
                        <td><a href="#"><button>Edit</button></a>&nbsp <a href="#"><button>Delete</button></a></td>

                    </tr>
                @endforeach
            </table>
</body>
</html>

 

Controller.php

Spoiler

<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use DB;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function insert(Request $req)
    {
        $CourseID = $req->input('CourseID');
        $SpecialtyID = $req->input('SpecialtyID');
        $FirstName = $req->input('FirstName');
        $LastName = $req->input('LastName');
        $Email = $req->input('Email');
        $FacNumber = $req->input('FacNumber');
        $EduForm = $req->input('EduForm');

        $data = array('CourseID'=>$CourseID,'SpecialtyID'=>$SpecialtyID,'FirstName'=>$FirstName,'LastName'=>$LastName,
            'Email'=>$Email,'FacNumber'=>$FacNumber,'EduForm'=>$EduForm);
        DB::table('students')->insert($data);
        echo "Success";
        $this->getData();
    }

    //Function to get data from table
    public function getData()
    {
        $data['data'] = DB::table('students')->get();
        if(count($data)>0)
        {
            return view('insertForm', $data);
        }
        else
        {
            return view('insertForm');
        }
    }
}

 

 

I'm getting this error:

 

Spoiler

Untitled.thumb.png.09412821bbef4360e38a43c66b31e2c6.png

 

I would appreciate if anyone could help me solve it. I've been stuck in there for the past 1, maybe 2 hours T_T

Link to comment
Share on other sites

Link to post
Share on other sites

You are kinda breaking Laravel's advantages now, which is totally understandable if you new to it.

You probably used to do it a certain way and now is the time to learn the advantages of a framework like Laravel, now I can explain you how it works with pulling data from the Model etc but the easier answer and shorter one for me would be is to watch a Laracast serie.

 

https://laracasts.com/series/laravel-from-scratch-2017

 

Pretty early (Episode 5) they talk about what you need I believe.

Quote or mention me if not feel ignored 

Link to comment
Share on other sites

Link to post
Share on other sites

I would advice to watch a few videos, the documentation can be a bit overwhelming at first.

Quote or mention me if not feel ignored 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×