【问题标题】:Laravel select 1lineLaravel 选择 1 行
【发布时间】:2020-12-04 14:43:43
【问题描述】:

在 pma "ogsett" 中有一个表,你需要在其中选择一行。 在模型中,Ogsett 指出了使用哪个表。 在控制器(ServiceController)中我指出了以下代码

<?php

namespace App\Http\Controllers;

use App\Service;
use App\Ogsett;
use Illuminate\Http\Request;

class ServiceController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $og = Ogsett::wherePages('service');
        return view('price.main', compact('og'));
    }

在页面上,我是这样显示的

@foreach($og as $seo_item)
{{ $seo_item->title }}
@endforeach

在其中,我参考了这张表,并选择了带有值标题的字段 - 服务。 但是我的数据没有显示在页面上。

底座本身是这样的

=======================================================
|ID|PAGES| TITLE|DESCRIPTION|URL|IMAGES|LOCAL|SITE_NAME|
=======================================================
| 1|service|TitlePage|DescPage Service|service|images.jpg|en|localhost|
| 2|contact|ContactPage|DescPage Contact|conatact|images.jpg|en|localhost|

我哪里做错了?

【问题讨论】:

  • 使用$og = Ogsett::wherePages('service')-&gt;firstOrFail();

标签: php mysql laravel select line


【解决方案1】:

如果你想遍历所有标题,你必须这样做

 $og = Ogsett::wherePages('service')->get();

然后在你的刀片中

@foreach($og as $seo_item)
{{ $seo_item->title }}
@endforeach

或者,如果您只想要标题而不需要其他数据库列,您也可以使用

$og = Ogsett::wherePages('service')->pluck('title');

然后你可以在刀片上

 @foreach($og as $seo_item)
{{ $seo_item }}
@endforeach

【讨论】:

    猜你喜欢
    • 2020-12-16
    • 1970-01-01
    • 2015-12-07
    • 2016-02-27
    • 2012-06-16
    • 2014-02-10
    • 1970-01-01
    • 1970-01-01
    • 2011-06-23
    相关资源
    最近更新 更多