duanmei1946 2017-09-16 17:39
浏览 56
已采纳

Laravel变量不通过

I'm an absolute beginner and I'm trying to do a CRUD in Laravel, but I can not figure it out why my variables aren't being passed over to the view so I can have a dynamic display of them in the table.

My "routes" work fine.

Then my controller

     public function getHome()
     {

     $results = Crud::index();
     return view('pages/home', ['results' => $results]);

     }

Calls my "Crud" model

class Crud extends Model
{
     public static function index()
     {

        return $results = DB::table('data')
                         ->whereNotNull('id')
                          ->get();
     }
}

And goes to my view as seen in the controller

@extends('main')

@section('title', '| Home')

@section('content')

 <div class="container">

     @include ('partials/_jumbotron')

     <table class="table table-inverse">
   <thead>
    <tr>
      <th>#</th>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Username</th>
    </tr>
 </thead>
 <tbody>


 @if(!empty($results))

  @foreach($results as $result)

  <tr>
    <td scope="row">$result->$id</th>
    <td>$result->$first_name</td>
    <td>$result->$last_name</td>
    <td>$result->$username</td>
  </tr>

@endforeach

 @endif

</tbody>
</table>
</div>

 @endsection

Thanks for the help in advance...I'm losing my mind And sorry If I wasn't specific enough..

Edit1: I did as you say, but I'm still not outputing data to my table? Is there anything else wrong in the stated code or is the bug somewhere deeper?

Edit2: Thanks everyone for the pointed out mistakes - blade {{ }} and object properties $object->propertie not $object->$propertie

When I fixed those mistakes which were obviously denying the output I remembered that I have an empty database.. Case Closed - Thanks everyone for the help

  • 写回答

3条回答 默认 最新

  • douwuying4709 2017-09-16 17:56
    关注

    You're not printing the variables. You're printing literals.

    If you want to print the variables (echo them) in blade files, you need to wrap them in curly brackets. Also there's not $ when you want to display an object attribute. Like that:

    @foreach($results as $result)
    
    <tr>
        <td scope="row">{{ $result->id }}</th>
        <td>{{ $result->first_name }}</td>
        <td>{{ $result->last_name }}</td>
        <td>{{ $result->username }}</td>
    </tr>
    
    @endforeach
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来