Jeric
Jeric

Reputation: 57

how to print data from table relation using with() funtion in laravel

Here is the generated array from table relation:

{"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}

Just want to know how can I get value of name from shelf array value.

Upvotes: 0

Views: 294

Answers (1)

Hikmat Sijapati
Hikmat Sijapati

Reputation: 6994

Your output is in json format.so use json_decode() to convert into array.As below.

$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);

Output:

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] => )

Get value of name using echo $array['shelf']['name']; because your array is multidimensional.

Upvotes: 1

Related Questions