【问题标题】:Database sql server with results but view is empty带有结果但视图为空的数据库 sql server
【发布时间】:2019-05-21 16:18:35
【问题描述】:

发生了一件非常奇怪的事情。所以我有一个名为 Lactinfo_News 的表,有 1 行。我还创建了一个名为 LACTINFO_VW_LatestNews 的视图,其中包含“SELECT * FROM Lactinfo_News”并返回相同的 1 行。

我正在使用 Eloquent,并且在我的新闻管理器中,

public function GetLatestNews($rowsPerPage) {
        $list = DB::table('LACTINFO_VW_LatestNews')
            ->orderBy('RegistedDate', 'DESC')
            ->paginate($rowsPerPage);

        return $list;
    }

where $rowsPerPage = 30.

在我的控制器中,

// >> current page
$page = '1';
if (! empty ( $request->query ( 'page' ) )) {
$page = $request->query ( 'page' );
}

// >> search
$nM = new NewsManager();
$list = $nM->GetLatestNews($page, $this->nbOfRowsPage);

return view ('admin.news.index', [
                'results' => compact($list),
                'page' => $page,
                'startDate' => $startDate,
                'endDate'=>$endDate 
        ] );
    }

在我看来,

<table id="news-results" class="hover responsive" style="margin-top: 20px;">
    <thead>
        <tr>
            <th scope="column">Título</th>          
            <th scope="column">Descrição</th>
            <th scope="column">Data Início</th>
            <th scope="column">Ficheiro</th>
            <th scope="column">Registada em</th>
            <th scope="column">Criada por</th>
        </tr>
    </thead>

    <tbody>
        @if (count($results) > 0) 
            @foreach ($results as $r)
                <tr>
                    <td>{{ $r->title }}</td>
                    <td>{{ $r->description }}</td>
                    <td>{{ Carbon\Carbon::parse($r->startDate)->format('d/m/Y') }}</td> 
                    <td>{{ $r->fileURL }}</td>
                    <td>{{ $r->registedDate }}</td>     
                    <td>{{ $r->createdBy }}</td>            
                </tr>
            @endforeach

        @else
            <tr><td colspan="11">Não existem notícias criadas</td></tr>
        @endif
    </tbody>
</table>

如果我转储 $results 变量为空..

发生了什么事?这真的很奇怪:S 我认为我不需要执行任何工匠命令,因为我已经向我的经理添加了一个新功能,但我被这个问题困扰了几天......

【问题讨论】:

  • 尝试compact('list') 而不是compact($list)
  • 没什么,我已经完成了 {{dd($results)}} 并且在我的数组列表中,查询键有 []...
  • 如果您在控制器中 dd($list); 在此行 $list = $nM-&gt;GetLatestNews($page, $this-&gt;nbOfRowsPage); 之后。 $list 变量中有一些数据吗?
  • 是这样!!非常感谢大佬!!我会更新我的答案!

标签: php laravel laravel-5 eloquent


【解决方案1】:

您在LengthAwarePaginator 类型的对象上使用compact,恕我直言,这有点奇怪。

为什么不在你的视图中传递$list

【讨论】:

  • 我这样做是因为我收到了错误 Undefined property: stdClass::$title (View) 并且解决方案之一是这个..
  • 只需 dump($r) 在您的 foreach 中查看您拥有的对象。也许你拼错了你的财产的大写
【解决方案2】:

我的问题出在控制器和视图中,

所以我将控制器返回到

return view ('admin.news.index', [
                'results' => $list,
                'page' => $page,
                'startDate' => $startDate,
                'endDate'=>$endDate
        ] );

在我看来,我并没有以错误的方式调用我的参数...

<tbody>
        @if (count($results) > 0) 
            @foreach ($results as $r)
                <tr>
                    <td>{{ $r->Title }}</td>
                    <td>{{ $r->Description }}</td>
                    <td>{{ Carbon\Carbon::parse($r->StartDate)->format('d/m/Y') }}</td> 
                    <td>{{ $r->fileURL }}</td>
                    <td>{{ Carbon\Carbon::parse($r->RegistedDate)->format('d/m/Y') }}</td>      
                    <td>{{ $r->refCredential }}</td>            
                </tr>
            @endforeach

        @else
            <tr><td colspan="11">Não existem notícias criadas</td></tr>
        @endif
    </tbody>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-30
    • 2020-07-23
    • 2012-03-14
    • 2020-07-17
    • 2013-04-24
    • 1970-01-01
    相关资源
    最近更新 更多