I’m working on a wordpress plugin using herbert. I have two controllers, the one (ClientController) which is called via panel and the second one(TimelineController) is an API controller for saving new data on the time line and is called via AJAX request using routes.
Here are my two controllers
ClientController
public function edit(Http $request)
{
$client = $this->clientsRep->find($input["id"]);
$timeline = $client->timeline;
return view('@AdminViews/edit.twig',compact('client', 'timeline'));
}
TimelineController
public function store(Http $request)
{
if($request->ajax())
{
if (is_user_logged_in())
{
$class = ucfirst($request->get('type'));
$c = 'Testing\HelpedClasses\TimelineLib'.$class;
$activity = new $c();
$activity = $activity->saveActivity($request);
if (is_array($activity))
{
$type = $activity["type"];
$activity = Activities::find($activity["activity_id"]);
$returnHTML = herbert('twig')->render('@AdminViews/edit/timeline_partials/timeline-row.twig', [
'activity' => $activity,
]);
return new JsonResponse(['success' => true, 'html' => $returnHTML, 'type' => $request->get('type'));
}
}
}
}
Here are the Models with their relations
class Client extends Model
{
public function timeline()
{
return $this->hasOne('TestModelsAdminTimeline', 'timeline_client_id', 'ID');
}
}
class Activities extends Model
{
public function clientNote()
{
return $this->hasOne('TestModelsAdminNotes', 'notes_timeline_activities_id', 'ID');
}
}
Here are the views
edit.twing
<div class="wrap">
<h1>Edit: {{client.clientMeta.name.value }} {{client.clientMeta.name.lastname }}</h1>
<div class="postbox ">
<ul class="nav nav-pills">
<li class="active"><a href="#1a" data-toggle="tab">Account</a></li>
<li><a href="#timeline" data-toggle="tab">Activity</a></li>
</ul>
<div class="inside">
<div class="tab-content clearfix">
<div class="tab-pane active" id="1a">
{% include '@AdminViews/edit/edit.twig' %}
</div>
<div class="tab-pane" id="timeline">
{% include '@AdminViews/edit/timeline.twig' %}
</div>
</div>
</div>
</div>
</div>
timeline.twig
<h3>Activity</h3>
<div class="timeline row">
<div class="col-sm-12 col-md-4 col-md-push-8">
<a href="#" data-type="note" class="btn btn-float note-btn">Add Note <i class="fa fa-sticky-note"></i></a>
</div>
<div class="col-sm-12 col-md-8 col-md-pull-4 timeline-section">
{% if timeline is not null %}
<ul class="timeline">
{% for activity in timeline %}
{% include '@AdminViews/edit/timeline_partials/timeline-row.twig' %}
{% else %}
<li class="empty-list">Looks like you have no addresses added.</li>
{% endfor %}
</ul>
{% else %}
<p class="empty-list">Looks like you don't have any timeline yet for this customer.</p>
{% endif %}
</div>
</div>
timeline-row.twig
<li>
<div class="direction-l"><span class="time-wrapper"><span class="time">{{activity.updated_at}}</span></span></div>
<div class="direction-r">
<div class="flag-wrapper">
<span class="flag">
{% include '@AdminViews/edit/timeline_partials/timeline-row-'~ activity.timeline_meta_key ~ '.twig' %}
</span>
</div>
</div>
</li>
and here is a sample of activity.timeline_meta_key value: timeline-row-note.twig
<span class="timeline-key"> <i class="fa fa-bell-o"></i>{{activity.timeline_meta_key}}</span>
<div class="clearfix"></div>
{% if activity.clientNote %}
<p class="note-subject">{{activity.clientNote.notes_subject}}</p>
{% endif %}
{% if activity.clientNote %}
<p class="note-desc">{{activity.clientNote.notes_description}}</p>
{% endif %}
Now if I call the ClientController it shows me fine the values {{activity.clientNote.notes_subject}} and {{activity.clientNote.notes_description}}
If now I call the TimelineController is not printing the variables that I already mentioned, I did a dd() in the twig file and it says that the variable is null, but if I do dd() before
$returnHTML = herbert('twig')->render('@AdminViews/edit/timeline_partials/timeline-row.twig', [
'activity' => $activity,
]);
and after Activities::find($activity[“activity_id”]); the two variables have the correct value. Does anyone knows why is not showing me the values of the relation activity.clientNote in the twig files if I’m do an ajax call?
Thank you