Laravel如何将数据从控制器传递到刀片

时间:2019-01-23 12:01:28

标签: laravel

我正在建立一个laravel网站。我希望提交的表单数据运行查询,并将结果插入表格格式。我正在使用oracle db,并且已将其连接到laravel。我也创建了表格,查询给出了正确的结果。我无法做的是将结果成功传递到表中。

我的文件如下:

1)Routes.php(或对于较新的laravel版本为web.php):

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/



Route::get('/search', 'PagesController@search'); //find student data!
Route::post('/dedomena', 'NewController@psaxe');

2)search.blade.php(采用post方法的形式)

<!DOCTYPE html>
<html lang="en">
<head>


    <meta charset="UTF-8">
    <!--<link rel="stylesheet" type="text/css" href="form_style.css">  -->
    <title>Εισαγωγή Δεδομένων Χρήστη</title>
</head>
<body>

<!--
<k1 class = "myhome">Εισαγωγή Δεδομένων</k1>
-->
Εισαγωγή Δεδομένων

<form method="post" action="/dedomena" id="forma">

  {{csrf_field()}}

    <fieldset>
        <legend>Εισαγωγή</legend>

        Αριθμός Μητρώου:<br>
        <input type="number" name="AM" min="1000000" max="9999999"><br>
        <input type="submit" value="Αναζήτηση">
    </fieldset>
</form>

</body>
</html>

3)运行查询的我的控制器(NewController.php)。结果正确显示,但以列表格式显示。我想将它们以表格格式放置。

<?php namespace App\Http\Controllers;
use Illuminate\Http\Request;

use DB;

class NewController extends Controller

{

    public function psaxe(Request $request)
    {

        $myVar = $request->get('AM');
        echo('Αποτελέσματα για τον φοιτητή με ΑΜ: ');
        echo($myVar);
        $entries = DB::table('SCC_ANSWER')->where('AM', $myVar)->get();
        dd($entries);
    }
}

我想在另一个名为pinakas.blade.php的刀片服务器上通过,该刀片服务器应如下所示。我怎样才能做到这一点?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Αποτελέσματα</title>
</head>
<body>

<table border="1">
    <tr>
        <th>AM</th>
        <th>ST</th>
        <th>CS</th>
        <th>REGYEAR</th>
        <th>REGTERMIN</th>
        <th>CSYEAR</th>
        <th>REGTYPE</th>
        <th>REGDESCR</th>
        <th>STATUS</th>
        <th>STGROUP</th>
        <th>SEM1</th>
        <th>SEM2</th>
        <th>SEM3</th>
        <th>SEM4</th>
        <th>SEM5</th>
        <th>SEM6</th>
        <th>SEM7</th>
        <th>SEM8</th>
        <th>SEM9</th>
        <th>SEM10</th>
        <th>SEM11</th>
        <th>SEM12</th>
        <th>SEM1B</th>
        <th>SEM2B</th>
        <th>SEM3B</th>
        <th>SEM4B</th>
        <th>SEM5B</th>
        <th>SEM6B</th>
        <th>SEM7B</th>
        <th>SEM8B</th>
        <th>SEM9B</th>
        <th>SEM10B</th>
        <th>SEM11B</th>
        <th>SEM12B</th>
        <th>SEM1P</th>
        <th>SEM2P</th>
        <th>SEM3P</th>
        <th>SEM4P</th>
        <th>SEM5P</th>
        <th>SEM6P</th>
        <th>SEM7P</th>
        <th>SEM8P</th>
        <th>SEM9P</th>
        <th>SEM10P</th>
        <th>SEM11P</th>
        <th>SEM12P</th>
        <th>TOTAL</th>
        <th>TOTALB</th>
        <th>TOTALP</th>
    </tr>

    <tr>
       // <td><?php $entries?></td>

    </tr>

</table>

</body>
</html>

6 个答案:

答案 0 :(得分:1)

尝试

public function psaxe(Request $request) {
    $myVar=$request->get('AM');
    $entries=DB::table('SCC_ANSWER')->where('AM',$myVar)->get();
    return view('view_file_location')->with('entries' => $entries);
}

// $ entries可以在刀片页面中使用

答案 1 :(得分:0)

您需要使用@foreach遍历结果:

<tr>
    @foreach($entries as $entry)
        <td>{{ $entry->am }}</td>
        <td>{{ $entry->st }}</td>
        <td>{{ $entry->cs }}</td>
        <td>....</td>
    @endforeach
</tr>

答案 2 :(得分:0)

将数据传递到刀片视图中是更好的方法。

$entries = DB::table('SCC_ANSWER')->where('AM', $myVar)->get();


return view('foldername.viewName',compact('entries'));

在刀片视图中

 @if(!empty($entries))


                        @foreach($entries as $entry)

                                 <tr class="warning" >
                                    <td >{{ $entry->AM}}</td>
                                    <td >{{ $entry->ST}}</td>
                                    <td >{{ $entry->CS}}</td>
                                    <td >{{ $entry->REGYEAR}}</td>
                                    <td >{{ $entry->role_name }}</td>
                                    <td >{{ $entry->created_at }}</td>
                                    <td ></td>
                                     .............
                                </tr>

                                @endforeach

                       @endif

答案 3 :(得分:0)

return view('path.to.your.view', compact('entries'));

在您看来,您只需执行以下操作:

@foreach($entries as $entry)
<tr>
...

等等

答案 4 :(得分:0)

return view('greetings', ['name' => 'Victoria']);

以这种方式传递信息时,数据应为具有键/值对的数组。然后,您可以在视图内部使用其对应的键(例如<?php echo $key; ?>)访问每个值。除了将完整的数据数组传递给视图助手功能之外,您还可以使用with方法将单个数据段添加到视图中

答案 5 :(得分:0)

谢谢大家的提示。问题是我在刀片中使用的大写字母!如果有人在未来的项目中遇到相关问题,我将在摘录下面添加以下内容:

1)Routes.php(或对于较新的laravel版本为web.php):

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/


Route::get('/search', 'PagesController@search'); //find student data!
Route::post('/dedomena', 'NewController@psaxe');

2)Search.blade.php

<!DOCTYPE html>
<html lang="en">
<head>


    <meta charset="UTF-8">
    <!--<link rel="stylesheet" type="text/css" href="form_style.css">  -->
    <title>Εισαγωγή Δεδομένων Χρήστη</title>
</head>
<body>

<!--
<k1 class = "myhome">Εισαγωγή Δεδομένων</k1>
-->
Εισαγωγή Δεδομένων

<form method="post" action="/dedomena" id="forma">

  {{csrf_field()}}

    <fieldset>
        <legend>Εισαγωγή</legend>

        Αριθμός Μητρώου:<br>
        <input type="number" name="AM" min="1000000" max="9999999"><br>
        <input type="submit" value="Αναζήτηση">
    </fieldset>
</form>

</body>
</html>

3)控制器(NewController.php)

<?php namespace App\Http\Controllers;
use Illuminate\Http\Request;

use DB;


class NewController extends Controller


{



    public function psaxe(Request $request)
    {

        $myVar = $request->get('AM');
        echo('Αποτελέσματα για τον φοιτητή με ΑΜ: ');
        echo($myVar);
        $entries = DB::table('SCC_ANSWER')->where('AM', $myVar)->get();
       // dd($entries);

        return view('pinakas', ['entries1' => $entries]);


    }}

4)刀片(pinakas.blade.php)

<!DOCTYPE html>
<html lang="el">
<head>
    <meta charset="UTF-8">
    <title>Αποτελέσματα</title>
</head>
<body>

<table border="1">
    <tr>
        <th>AM</th>
        <th>ST</th>
        <th>CS</th>
        <th>REGYEAR</th>
        <th>REGTERMIN</th>
        <th>CSYEAR</th>
        <th>REGTYPE</th>
        <th>REGDESCR</th>
        <th>STATUS</th>
        <th>STGROUP</th>
        <th>SEM1</th>
        <th>SEM2</th>
        <th>SEM3</th>
        <th>SEM4</th>
        <th>SEM5</th>
        <th>SEM6</th>
        <th>SEM7</th>
        <th>SEM8</th>
        <th>SEM9</th>
        <th>SEM10</th>
        <th>SEM11</th>
        <th>SEM12</th>
        <th>SEM1B</th>
        <th>SEM2B</th>
        <th>SEM3B</th>
        <th>SEM4B</th>
        <th>SEM5B</th>
        <th>SEM6B</th>
        <th>SEM7B</th>
        <th>SEM8B</th>
        <th>SEM9B</th>
        <th>SEM10B</th>
        <th>SEM11B</th>
        <th>SEM12B</th>
        <th>SEM1P</th>
        <th>SEM2P</th>
        <th>SEM3P</th>
        <th>SEM4P</th>
        <th>SEM5P</th>
        <th>SEM6P</th>
        <th>SEM7P</th>
        <th>SEM8P</th>
        <th>SEM9P</th>
        <th>SEM10P</th>
        <th>SEM11P</th>
        <th>SEM12P</th>
        <th>TOTAL</th>
        <th>TOTALB</th>
        <th>TOTALP</th>
    </tr>
    @foreach($entries1 as $entry)
        <tr>
            <td>{{ $entry->am }}</td>
            <td>{{ $entry->st }}</td>
            <td>{{ $entry->cs }}</td>
            <td>{{ $entry->regyear }}</td>
            <td>{{ $entry->regtermin }}</td>
            <td>{{ $entry->csyear }}</td>
            <td>{{ $entry->regtype }}</td>
            <td>{{ $entry->regdescr }}</td>
            <td>{{ $entry->status }}</td>
            <td>{{ $entry->stgroup }}</td>
            <td>{{ $entry->sem1 }}</td>
            <td>{{ $entry->sem2 }}</td>
            <td>{{ $entry->sem3 }}</td>
            <td>{{ $entry->sem4 }}</td>
            <td>{{ $entry->sem5 }}</td>
            <td>{{ $entry->sem6 }}</td>
            <td>{{ $entry->sem7 }}</td>
            <td>{{ $entry->sem8 }}</td>
            <td>{{ $entry->sem9 }}</td>
            <td>{{ $entry->sem10 }}</td>
            <td>{{ $entry->sem11 }}</td>
            <td>{{ $entry->sem12 }}</td>
            <td>{{ $entry->sem1b }}</td>
            <td>{{ $entry->sem2b }}</td>
            <td>{{ $entry->sem3b }}</td>
            <td>{{ $entry->sem4b }}</td>
            <td>{{ $entry->sem5b }}</td>
            <td>{{ $entry->sem6b }}</td>
            <td>{{ $entry->sem7b }}</td>
            <td>{{ $entry->sem8b }}</td>
            <td>{{ $entry->sem9b }}</td>
            <td>{{ $entry->sem10b }}</td>
            <td>{{ $entry->sem11b }}</td>
            <td>{{ $entry->sem12b }}</td>
            <td>{{ $entry->sem1p }}</td>
            <td>{{ $entry->sem2p }}</td>
            <td>{{ $entry->sem3p }}</td>
            <td>{{ $entry->sem4p }}</td>
            <td>{{ $entry->sem5p }}</td>
            <td>{{ $entry->sem6p }}</td>
            <td>{{ $entry->sem7p }}</td>
            <td>{{ $entry->sem8p }}</td>
            <td>{{ $entry->sem9p }}</td>
            <td>{{ $entry->sem10p }}</td>
            <td>{{ $entry->sem11p }}</td>
            <td>{{ $entry->sem12p }}</td>
            <td>{{ $entry->total }}</td>
            <td>{{ $entry->totalb }}</td>
            <td>{{ $entry->totalp }}</td>
        </tr>
    @endforeach

</table>

</body>
</html>

5)和PagesController.php

<?php namespace App\Http\Controllers;

use Illuminate\Http\Request;  //sos:: needed only for function emfanisi!!!!
use DB;                       //sos:: needed only for function emfanisi!!!!


class PagesController extends Controller
{




    public function search()
    {
        return view('search');
    }
}