如何使用Laravel 5.3刀片显示(多维?)数组中的数据 [英] How to show data from (multidimensional?) arrays with Laravel 5.3 blade

查看:211
本文介绍了如何使用Laravel 5.3刀片显示(多维?)数组中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以这是一个问题,我希望能对其他新手有所帮助,因为我遇到了从Blade中的数组中提取信息的错误,并且我不熟悉正确调试的语法.

Ok, so this is a question I hope can help other newbies as I'm running into errors pulling information from arrays in Blade and I'm unfamiliar with it's syntax to properly debug.

我了解如何将数组数据正常发送到视图,例如:

I understand how to send array data to the view normally, for instance:

public function index() {
   $variable = DB::table('tablename')->where('ID', '535');
   return view('viewname', compact('variable'));
}

这会将附加到ID 535的所有内容发送到视图.然后可以像这样打印标题:

This will send everything attached to the ID of 535 to the view. The title can then be printed out like so:

@foreach ($variable as $foo)
  {{ $foo->title }}
@endforeach

或者如果您想打印所有内容(我认为这是对的吗?):

Or if you want to print everything (I Think this is right?):

@foreach ($variable as $foo)
  @foreach ($foo as $name)
    {{ $name }}
  @endforeach
@endforeach

我很了解这个过程.

但是我遇到的问题是模型.

But where I'm getting stuck is with models.

假设我设置了一些路线:

Let's say I set up some routes:

Route::get('User', 'UserEntryController@index');
route::get('User/{id}', 'UserEntryController@show');

然后在控制器中获取显示路线的控制器:

And in the controller one that grabs the show route:

   <?php

namespace App\Http\Controllers;
use App\UserEdit;
use Illuminate\Http\Request;
use App\Http\Requests;

class UserEntryController extends Controller
{

   public function show(UserEdit $id) {

       return $id;
   }

}

这将返回模型附带的所有内容:

This will return everything attached to the model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class UserEdit extends Model  {




    protected $table = 'users';


    protected $fillable = ['ID', various', 'pieces', 'of', 'table', 'data'];


    protected $hidden = [];


    protected $casts = [];


    protected $dates = [];

}

但是,如果我更改控制器上的一行:

However if I change a line on the controller:

<?php

namespace App\Http\Controllers;
use App\UserEdit;
use Illuminate\Http\Request;
use App\Http\Requests;

class UserEntryController extends Controller
{

   public function show(UserEdit $id) {

       return view('UserEdit', compact('id'));
       //return $id;

   }

}

上述刀片服务器代码将无法运行.实际上,我根本无法解析发送到视图的数组.当然,直接的{{id}}将为我提供数组的实际内容.

The aforementioned Blade code will not run. In fact, I can't parse the array sent out to the the view at all. Of course a straight {{ id }} will give me the actual contents of the array.

{"ID":535,"variable":"Del","pieces":"22","of":"32","table":"54","data":"John"}

{"ID":535,"various":"Del","pieces":"22","of":"32","table":"54","data":"John"}

所以我想我的问题是.如果我要获取的数据是这样的数组形式的.如何遍历它以应用格式,将其放在表格中或以表格形式等等?

So I guess my question is. If I'm getting data that's in the form of an array like this. How do iterate through it to apply formatting, put it in tables, or put it in a form, etc?

推荐答案

您的命名约定实际上使您感到困惑. 只要您要返回并使用对象,就可以将该$ id变量重命名为更明显的名称. 像:

You're naming convention actually confuses you. As long as you're returning and using a Object, rename that $id variable to something more obvious. like:

class UserEntryController extends Controller
{
    ...
    public function show(UserEdit $object) {
        return view('UserEdit', compact('object'));
    }
    ...
}

对于您的框架未设置为直接绑定到对象而您的 show 方法接收到$ id的情况,则应该通过查询数据库来返回对象.

For the case when your framework is not set to bind directly to the object and your show method receives the $id, than you should return the object by querying the DB.

class UserEntryController extends Controller
{
    ...
    public function show($id) {
        $object = UserEdit::findOrFail($id);
        return view('UserEdit', compact('object'));
    }
    ...
}

...,然后在视图刀片文件中使用nou,就像在上面的示例中一样.

... and nou in your view blade file you can use it like in your above examples.

@foreach ($object as $foo)
    @foreach ($foo as $name)
        {{ $name }}
    @endforeach
@endforeach`

这篇关于如何使用Laravel 5.3刀片显示(多维?)数组中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆