what do you mean by remote? is it a view file ?
@include will render a view template file. this does not include php files
you need to use php method include() or include_once()
you can assign the result of that file to a variable like this
$dataPoints = include('\path\to\file');
Thanks for the reply! :)
By remote i do mean a view file, the file is located in
resources/views/partials/dataPoints.blade.php
Ill give your method a whirl!
Thankyou!
ok, but why do you want to do that? view templates are supposed to render a view, they should't have executable code. The correct approach would be to pass this array from the controller on to the view.
Although you may define a static method on your model class or a utility class to get this array from the view.
So ive tried every possible variation of this :
<?php
$dataPoints = include('partials/dataPoints.php');
?>
To which i always get the same result, file not found....where in relation to the blade file is the directory?
For example i've tried
../
/
./
None of which worked!
astroanu said:
ok, but why do you want to do that? view templates are supposed to render a view, they should't have executable code. The correct approach would be to pass this array from the controller on to the view.
Although you may define a static method on your model class or a utility class to get this array from the view.
The datapoints file is just an array that im trying to include into some java, they're graph vectors, and dont want to clog up my template file including potentially 1000's of datapoints! :)
is the array being generated by a java server ? or did you mean "java script", array to be used in a graph back on the front end ?
Create a new class file on the app directory. let's call it Data
namespace App;
class Data{
public static function getPoints()
{
return [
// array data
];
}
}
on your view file you can call it to get the array easily
{{ \App\Data::getPoints() }}
Ok so this is what i done :
Created a new controller in app/controllers called "dataPoints.php"
In there i added :
<?php
namespace App;
class dataPoints{
public static function getPoints()
{
return [
// array data
array("y" => 6, "label" => "Apple"),
array("y" => 4, "label" => "Mango"),
array("y" => 5, "label" => "Orange"),
array("y" => 7, "label" => "Banana"),
array("y" => 4, "label" => "Pineapple"),
array("y" => 6, "label" => "Pears"),
array("y" => 7, "label" => "Grapes"),
array("y" => 5, "label" => "Lychee"),
array("y" => 4, "label" => "Jackfruit")
];
}
}
Then in my template file i added :
{{ \App\dataPoints::getPoints() }}
Returned this error :
Class 'App\dataPoints' not found
Thanks a lot for your help really appreciate it! :)
move your dataPoints.php to the app folder. project-folder\app\dataPoints.php
Ok so i got a new error now!
htmlentities() expects parameter 1 to be string, array given (View: C:\wamp\www\owtest\resources\views\dashboard\sr-graph.blade.php)
LOL What is?!
of course, change {{ \App\dataPoints::getPoints() }} to {{ dd(\App\dataPoints::getPoints()) }} this will print your array on the view
Excellent thanks for that! :)
So i now have something new....LOL Im sorry, again thanks for your help!
The data i need to post is this :
array("y" => 6, "label" => "Apple"),
array("y" => 4, "label" => "Mango"),
array("y" => 5, "label" => "Orange"),
array("y" => 7, "label" => "Banana"),
array("y" => 4, "label" => "Pineapple"),
array("y" => 6, "label" => "Pears"),
array("y" => 7, "label" => "Grapes"),
array("y" => 5, "label" => "Lychee"),
array("y" => 4, "label" => "Jackfruit")
In that format, whats coming through is this :
array:9 [▼
0 => array:2 [▼
"y" => 6
"label" => "Apple"
]
1 => array:2 [▼
"y" => 4
"label" => "Mango"
]
2 => array:2 [▼
"y" => 5
"label" => "Orange"
]
3 => array:2 [▼
"y" => 7
"label" => "Banana"
]
4 => array:2 [▼
"y" => 4
"label" => "Pineapple"
]
5 => array:2 [▶]
6 => array:2 [▶]
7 => array:2 [▶]
8 => array:2 [▶]
]
It just prints that on my template file/page
The javascript im using is this :
<script type="text/javascript">
$(function () {
var chart = new CanvasJS.Chart("chartContainer", {
theme: "theme2",
animationEnabled: true,
title: {
text: "Basic Column Chart using CanvasJS"
},
data: [
{
type: "column",
dataPoints: <?php echo json_encode($dataPoints, JSON_NUMERIC_CHECK); ?>
}
]
});
chart.render();
});
</script>
I was hoping that the data would be posted as $dataPoints...
yea, so on your javascript all you gotta do is replace :
dataPoints: <?php echo json_encode($dataPoints, JSON_NUMERIC_CHECK); ?>
with (if you're using blade)
dataPoints: {{ json_encode( \App\dataPoints::getPoints() , JSON_NUMERIC_CHECK) }}
or replace with this if you're using just php
dataPoints: <?php echo json_encode( \App\dataPoints::getPoints() , JSON_NUMERIC_CHECK); ?>
Perfect! :)
Thankyou, just one last thing...when viewing the HTML (as the graph didnt work) i noted the data as :
dataPoints: [{"y":6,"label":"Apple"},{"y":4,"label":"Mango"},{"y":5,"label":"Orange"},{"y":7,"label":"Banana"},{"y":4,"label":"Pineapple"},{"y":6,"label":"Pears"},{"y":7,"label":"Grapes"},{"y":5,"label":"Lychee"},{"y":4,"label":"Jackfruit"}]
Which obviously isnt how i want it to come through as, any suggestions? :)
Thanks a million!
i think laravel is trying to escape the json string to html
Replace {{ }} with {!! !!} in the view
A quick question a little off topic i guess...
I have a list of numbers (they're 4 digits) eg. 2245
The format they need to be entered is :
array("y" => 5),
Is it possible to use this list and prepend the suffix thats required? The numbers i have in plaintext, literally just a list, but i need the data as
array("y" => 5),
And i dont fancy adding that format to each number as there are potentially thousands!
Any tips/suggestions would be great!
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community