Matt Rogers Matt Rogers - 27 days ago 11
Twig Question

Accessing eloquent BelongsToMany in twig?

I am having an issue with relationships in eloquent and loading them in twig templating. I have three tables which are Articles, Users and Authors. The first too are pretty straight forward they are my blog posts (articles) and users is my user model containing first names, last names, email ect. Authors is a pivot table which contains (users_id and articles_id) which corresponds to the id's in the previous two tables.

The issue i am having is how do i show the name of the author using the following array with twig markup. at the moment I have this but it just isn't working.

{% for article in articles %}
by {{ article.first_name }} {{ article.last_name }}
{% end for %}


Any help would be great, I have been trying to figure this out for days.

MY CODE

I have the following relationships

class Article extends Eloquent {

public function authors() {
return $this->belongsToMany('voicetank\User\User', 'ArticleAuth', 'Articles_id', 'users_id');
}
}

class User extends Eloquent {
public function articles() {
return $this->belongsToMany('voicetank\Articles\AddArticle', 'ArticleAuth', 'users_id', 'id');
}


Within my route I have the following code

$app->get('/home', function() use ($app) {

$articles = $app->article->where('articlepublish', true)->orderBy('created_at', 'desc')->with('authors')->get();

$user = $app->user->get();

$app->render('home/home.php', [
'articles' => $articles,
'user' => $user
]);


Using a var_dump i get the following array.

object(Illuminate\ Database\ Eloquent\ Collection) #199 (1) { ["items":protected]= >
array(1) {
[0] => object(voicetank\ Articles\ AddArticle) #194 (23) { ["table":protected]= >
string(8)
"Articles" ["fillable": protected] => array(5) {
[0] => string(5)
"title" [1] => string(4)
"body" [2] => string(14)
"featurepicture" [3] => string(14)
"articlepublish" [4] => string(8)
"category"
}["connection": protected] => NULL["primaryKey":
protected] => string(2)
"id" ["perPage": protected] => int(15)["incrementing"] =>
bool(true)["timestamps"] => bool(true)["attributes":
protected] => array(8) {
["id"] => int(35)["title"] => string(17)
"This is the Title" ["body"] => string(15)
"sdfasdfasdfsadf" ["category"] => string(8)
"Category" ["featurepicture"] => string(11)
"picturelink" ["articlepublish"] => int(1)[
"updated_at"] => string(19)
"2016-11-08 14:57:26" ["created_at"] => string(19)
"2016-11-08 14:57:26"
}["original": protected] => array(8) {
["id"] => int(35)["title"] => string(17)
"This is the Title" ["body"] => string(15)
"sdfasdfasdfsadf" ["category"] => string(8)
"Category" ["featurepicture"] => string(11)
"picturelink" ["articlepublish"] => int(1)[
"updated_at"] => string(19)
"2016-11-08 14:57:26" ["created_at"] => string(19)
"2016-11-08 14:57:26"
}["relations": protected] => array(1) {
["authors"] => object(Illuminate\ Database\ Eloquent\ Collection) #202 (1) { ["items":protected]= >
array(1) {
[0] => object(voicetank\ User\ User) #201 (23) { ["table":protected]= >
string(5)
"users" ["fillable": protected] => array(10) {
[0] => string(5)
"email" [1] => string(8)
"password" [2] => string(10)
"first_name" [3] => string(9)
"last_name" [4] => string(15)
"current_project" [5] => string(6)
"active" [6] => string(11)
"active_hash" [7] => string(12)
"recover_hash" [8] => string(19)
"remember_identifier" [9] => string(
14)
"remember_token"
}["connection": protected] => NULL[
"primaryKey": protected] => string(2)
"id" ["perPage": protected] => int(15)[
"incrementing"] => bool(true)[
"timestamps"] => bool(true)[
"attributes": protected] => array(13) {
["id"] => int(29)["email"] => string(
24)
"matt@website.com" [
"first_name"
] => string(7)
"Matthew" ["last_name"] => string(6)
"Rogers" ["password"] => string(60)
"xxxxxxxx" [
"current_project"
] => int(1)["active"] => int(1)[
"active_hash"] => NULL[
"recover_hash"] => NULL[
"remember_identifier"] =>
NULL["remember_token"] => NULL[
"created_at"] => string(19)
"2016-08-07 08:59:01" ["updated_at"] =>
int(2016)
}["original": protected] => array(15) {
["id"] => int(29)["email"] => string(
24)
"matt@website.com" [
"first_name"
] => string(7)
"Matthew" ["last_name"] => string(6)
"Rogers" ["password"] => string(60)
"xxxxxxxxxxxxxx" [
"current_project"
] => int(1)["active"] => int(1)[
"active_hash"] => NULL[
"recover_hash"] => NULL[
"remember_identifier"] =>
NULL["remember_token"] => NULL[
"created_at"] => string(19)
"2016-08-07 08:59:01" ["updated_at"] =>
int(2016)["pivot_Articles_id"] => int(
35)["pivot_users_id"] => int(
29)
}["relations": protected] => array(1) {
["pivot"] => object(Illuminate\ Database\ Eloquent\ Relations\ Pivot) #200 (26) { ["parent":protected]= >
object(voicetank\ Articles\ AddArticle) #191 (23) { ["table":protected]= >
string(8)
"Articles" ["fillable": protected] =>
array(5) {
[0] => string(5)
"title" [1] => string(4)
"body" [2] => string(14)
"featurepicture" [3] =>
string(14)
"articlepublish" [4] =>
string(8)
"category"
}["connection": protected] =>
NULL["primaryKey": protected] =>
string(2)
"id" ["perPage": protected] => int(15)[
"incrementing"] => bool(true)[
"timestamps"] => bool(true)[
"attributes": protected] =>
array(0) {}["original": protected] =>
array(0) {}["relations":
protected
] => array(0) {}["hidden":
protected
] => array(0) {}["visible":
protected
] => array(0) {}["appends":
protected
] => array(0) {}["guarded":
protected
] => array(1) {
[0] => string(1)
"*"
}["dates": protected] => array(0) {}
["dateFormat": protected] => NULL[
"casts": protected] => array(
0) {}["touches": protected] =>
array(0) {}["observables":
protected
] => array(0) {}["with":
protected
] => array(0) {}["morphClass":
protected
] => NULL["exists"] => bool(false)[
"wasRecentlyCreated"] => bool(
false)
}["foreignKey": protected] => string(11)
"Articles_id" ["otherKey": protected] =>
string(8)
"users_id" ["guarded": protected] => array(0) {}
["connection": protected] => NULL["table":
protected] => string(11)
"ArticleAuth" ["primaryKey": protected] =>
string(2)
"id" ["perPage": protected] => int(15)[
"incrementing"] => bool(true)[
"timestamps"] => bool(false)[
"attributes": protected] => array(2) {
["Articles_id"] => int(35)["users_id"] =>
int(29)
}["original": protected] => array(2) {
["Articles_id"] => int(35)["users_id"] =>
int(29)
}["relations": protected] => array(0) {}[
"hidden": protected
] => array(0) {}["visible": protected] =>
array(0) {}["appends": protected] =>
array(0) {}["fillable": protected] =>
array(0) {}["dates": protected] => array(
0) {}["dateFormat": protected] =>
NULL["casts": protected] => array(0) {}[
"touches": protected
] => array(0) {}["observables": protected] =>
array(0) {}["with": protected] => array(0) {}
["morphClass": protected] => NULL[
"exists"] => bool(true)[
"wasRecentlyCreated"] => bool(false)
}
}["hidden": protected] => array(0) {}["visible":
protected
] => array(0) {}["appends": protected] => array(0) {}[
"guarded": protected
] => array(1) {
[0] => string(1)
"*"
}["dates": protected] => array(0) {}["dateFormat":
protected
] => NULL["casts": protected] => array(0) {}["touches":
protected
] => array(0) {}["observables": protected] => array(0) {}
["with": protected] => array(0) {}["morphClass":
protected
] => NULL["exists"] => bool(true)["wasRecentlyCreated"] =>
bool(false)
}
}
}
}["hidden": protected] => array(0) {}["visible": protected] => array(
0) {}["appends": protected] => array(0) {}["guarded":
protected
] => array(1) {
[0] => string(1)
"*"
}["dates": protected] => array(0) {}["dateFormat": protected] =>
NULL["casts": protected] => array(0) {}["touches": protected] =>
array(0) {}["observables": protected] => array(0) {}["with":
protected
] => array(0) {}["morphClass": protected] => NULL["exists"] =>
bool(true)["wasRecentlyCreated"] => bool(false)
}
}
}

Answer

By defining authors as a belongsToMany of Article it's going to give you an array. Your query is retrieving the data you want but in your template if there is just one author you should be doing:

{% for article in articles %}
by {{ article.authors[0].first_name }} {{ article.authors[0].last_name }}
{% end for %}
Comments