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}是的,我知道了。。谢谢