Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在laravel中使用with()函数从表关系打印数据_Php_Mysql_Laravel 5 - Fatal编程技术网

Php 如何在laravel中使用with()函数从表关系打印数据

Php 如何在laravel中使用with()函数从表关系打印数据,php,mysql,laravel-5,Php,Mysql,Laravel 5,以下是从表关系生成的数组: {"id":1,"shelf":{"id":1,"name":"Computer Related","description":"Lorem ipsum dolor somet.","created_at":"2017-01-15 00:00:00","updated_at":"2017-01-15 00:00:00","deleted_at":null},"book_no":"001","title":"Programming","author":"Rasmus L

以下是从表关系生成的数组:

{"id":1,"shelf":{"id":1,"name":"Computer Related","description":"Lorem ipsum dolor somet.","created_at":"2017-01-15 00:00:00","updated_at":"2017-01-15 00:00:00","deleted_at":null},"book_no":"001","title":"Programming","author":"Rasmus Lerdorf","publisher":"Micheal Holmes","description":"The planning, scheduling, or performing of a program.","book_img":null,"no_of_books":20,"created_at":"2017-01-11 00:00:00","updated_at":"2017-01-11 00:00:00","deleted_at":null}

我只是想知道如何从shelf数组值中获取name的值。

您的输出是json格式的。因此,请使用
json\u decode()
转换为数组。如下所示

$json = '{"id":1,"shelf":{"id":1,"name":"Computer Related","description":"Lorem ipsum dolor somet.","created_at":"2017-01-15 00:00:00","updated_at":"2017-01-15 00:00:00","deleted_at":null},"book_no":"001","title":"Programming","author":"Rasmus Lerdorf","publisher":"Micheal Holmes","description":"The planning, scheduling, or performing of a program.","book_img":null,"no_of_books":20,"created_at":"2017-01-11 00:00:00","updated_at":"2017-01-11 00:00:00","deleted_at":null}';

$array = json_decode($json,true);
print_r($array);
输出:

Array ( [id] => 1 [shelf] => Array ( [id] => 1 [name] => Computer Related [description] => Lorem ipsum dolor somet. [created_at] => 2017-01-15 00:00:00 [updated_at] => 2017-01-15 00:00:00 [deleted_at] => ) [book_no] => 001 [title] => Programming [author] => Rasmus Lerdorf [publisher] => Micheal Holmes [description] => The planning, scheduling, or performing of a program. [book_img] => [no_of_books] => 20 [created_at] => 2017-01-11 00:00:00 [updated_at] => 2017-01-11 00:00:00 [deleted_at] => )

使用echo$array['shelf']['name']获取名称的值因为你的数组是多维的。

你想在javascript或mysql中做什么?我想用laravel blade中的大括号{{}打印它。类似这样的:{{$variable->field}是的,我知道了。。谢谢