I have a chart, I am adding data through a controller. How do I add data via an event?
I have a database, through the controller I get the data, then I transfer it to the view and using the diagrams I display the data. I need to pass data through an event, how can this be done?
Chart:
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: [],
datasets: [{
label: 'data',
data: [],
borderWidth: 1
}]
},
options: {
scales: {
xAxes: [],
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
var updateChart = function() {
$.ajax({
url: "{{ route('chart') }}",
type: 'GET',
dataType: 'json',
success: function(data) {
myChart.data.labels = data.labels;
myChart.data.datasets[0].data = data.data;
myChart.update();
},
error: function(data){
console.log(data);
}
});
}
updateChart();
Echo.channel('events')
.listen('RealTimeMessage', (e) => {
updateChart();
});
Event:
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Queue\SerializesModels;
class RealTimeMessage implements ShouldBroadcast
{
use SerializesModels;
public string $message;
public function __construct(string $message)
{
$this->message = $message;
}
public function broadcastOn(): Channel
{
return new Channel('events');
}
}
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Data;
Use App\Events\RealTimeMessage;
class ChartController extends Controller
{
public function index() {
$infos = Data::latest()->take(30)->get()->sortBy('id');
$labels = $infos->pluck('id');
$data = $infos->pluck('data');
// dd($labels);
// return view('welcome', compact('labels', 'data'));
return response()->json(compact('labels', 'data'));
}
public function add(Request $request) {
return view('add');
}
public function store(Request $request) {
Data::create($request->data);
event(new RealTimeMessage('Done!'));
return view('add');
}
}
Change the event like so: (all public properties will be broadcasted)
public string $message;
public array $data
public function __construct(string $message, array $data)
{
$this->message = $message;
$this->data = $data;
}
Then in your controller:
...
event(new RealTimeMessage('Done!', $data));
....
Related
I'm new on Laravel development and started to build a small application for uploading images via form and drag and drop.
For some reason, when I try to upload an image using the POST route and the Controller's store method, the app only returns ERROR 405 - Method Not Allowed. I don't know why it's happening.
web.php
Route::resource('colaboradores', ColaboradoresController::class, ['only' => ['index', 'store', 'update', 'destroy']]);
My dropzone:
<form enctype="multipart/form-data" action="{{ route('administrativo.secoes.colaboradores.store') }}" id="image-upload-form">
<input type="file" name="file" id="image-input" class="dropzone-input" onchange="get_images(this)" multiple hidden>
<div class="drop-zone" id="image-dropzone" ondragover="dragOverHandler(event);" ondrop="dropHandler(event);">
<div class="inner-elements">
<label for="image-input">
<h3>Drag your pictures here</h3>
<span class="note needsclick">Or click here to manually select them.</span>
</label>
</div>
</div>
</form>
My JavaScript file with HTTP request through AJAX:
function dragOverHandler(ev) {
document.getElementById("image-dropzone").style.border = "3px solid #00f752";
ev.preventDefault();
}
function dropHandler(ev) {
document.getElementById("image-dropzone").style.border = "3px dashed #0087f7";
ev.preventDefault();
if (ev.dataTransfer.items) {
\[...ev.dataTransfer.items\].forEach((item, i) =\> {
if (item.type.includes("image")) {
const file = item.getAsFile();
upload_image(file);
}
});
}
}
function get_images() {
var total_images = document.getElementById("image-input").files.length;
if (total_images \> 0) {
for (var index = 0; index \< total_imagens; index++) {
var file = document.getElementById("image-input").files\[index\];
upload_image(file);
}
}
}
function upload_image(file) {
var form_data = new FormData();
form_data.append("file", file);
form_data.append("test", "test");
$.ajaxSetup({
headers: {
"X-CSRF-TOKEN": $("meta\[name='csrf-token'\]").attr("content")
}
});
$.ajax({
type: "PUT",
url: \`${window.location.pathname}/store/\`,
data: form_data,
cache: false,
contentType: false,
processData: false,
success: function (data) {
console.log(data);
},
error: function (data) {
console.log(data);
}
});
}
SobreController.php
<?php
namespace App\Http\Controllers\Administrativo;
use App\Http\Controllers\Controller;
use App\Models\SectionCollaborators;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
class ColaboradoresController extends Controller
{
public function index()
{
//
}
public function create()
{
//
}
public function store(Request $request)
{
try {
$novo_nome = self::upload_imagem($request);
return response()->json(["imagem" => "$novo_nome", "mensagem" => "Imagem salva com sucesso."] 200);
} catch (Exception $e) {
return response()->json(["mensagem" => "Ocorreu um erro ao salvar a imagem. Tente novamente.", 500]);
}
}
private function upload_imagem(Request $request)
{
if ($request->hasFile('file') && $request->file('file')->isValid()) {
$request->validate([
'file' => 'required|image|mimes:jpeg,png,jpg,webm,svg|max:2048',
]);
$img = $request->file;
$novo_nome = Storage::disk('local')->put('public/img/colaboradores/cards', $img, 'public');
return basename($novo_nome);
}
}
public function update(Request $request)
{
//
}
private function update_text($object, $nome, $valor)
{
//
}
public function destroy(Request $request)
{
//
}
public function setId(int $value)
{
//
}
public function getId()
{
//
}
}
I am using FullCalendar in laravel 8 project and I use it to display all the events from the database.
This is the code:
My controller
public function index(Request $request, $id) {
$patient = [];
$listOfPatients = [];
$appointments = [];
//apply permission constraits
$patient = Patient::find($id);
$listOfPatients = Patient::all();
$appointments = appointments::all();
$this->authorize('view', $patient);
// $appointments = $patient->remarks()->get()->sortBy([['id', 'desc']]);
if($request->ajax()) {
$data = appointments::whereDate('start', '>=', $request->start)
->whereDate('end', '<=', $request->end)
->get(['id', 'title', 'description', 'start', 'end']);
return response()->json($data);
}
return view('patient.appointments', [ "appointments" => $appointments, "patient" => $patient, "listOfPatients" => $listOfPatients]);
}
My js
$(document).ready(function () {
var SITEURL = "{{ url('/') }}";
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
var calendar = $('#full_calendar_events').fullCalendar({
editable: false,
editable: false,
eventSources: [
{
url: '/{id}/appointments',
color: '#65a9d7', // an option!
textColor: '#3c3d3d' // an option!
}
],
});
});
I also got this error:
jquery.min.js:4 GET http://127.0.0.1:8000/%7Bid%7D/appointments?start=2022-02-27&end=2022-04-10&_=1648373948124 404 (Not Found)
I'm trying to follow this example code https://laravel.com/docs/7.x/broadcasting#using-example-application
I already made a controller with this code:
public function send()
{
$orderId = 5;
broadcast(new TestEvento($orderId));
return view('test.send');
}
public function receive()
{
$orderId = 5;
return view('test.receive', compact('orderId'));
}
An event with this code:
public $orderId;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct($orderId)
{
$this->orderId = $orderId;
Log::debug('Construct orderId: '.$orderId);
}
/**
* Get the channels the event should broadcast on.
*
* #return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
Log::debug('broadcastOn: '.$this->orderId);
return new PrivateChannel('canaltest.'.$this->orderId);
}
A route:
Broadcast::channel('canaltest.{var}', function ($user, $var) {
return true; //temp workaround
return $user->id === Order::findOrNew($var)->user_id;
});
A view with this:
<div id="app">
<test :orderId="{{$orderId}}"></test>
</div>
Added this line to app.js:
Vue.component('test', require('./components/Test.vue').default);
And a .vue with
<script>
export default {
mounted() {
console.log('Component mounted.');
}
}
Echo.private('canaltest.${orderId}')
.listen('TestEvento', (e) => {
console.log(e);
});
</script>
The problem is I just can't find a way to get the js to get the orderId var and there's nowhere in vue documentation that says where should I place the .props array when requiring a component in app.js. So how can I get my test.vue component to read the orderId var?
I believe what you're looking for as below for test.vue
<script>
export default {
props: {
orderId: {
type: [Number, String],
required: true
},
},
mounted() {
console.log('Component mounted.');
}
}
</script>
I'm having difficulty passing the value from my personal js to the controller and recovering it on the personal tpl page.
This module will serve to customize product after some selections and fields to fill out.
The selections pass from tabs to tabs.
The problem is that I can't get the value {$ var}
I have:
JS in root->modules->modulename->views->js->namejsfile.js
CONTROLLER in root->modules->modulename->controllers->front->controllername.php
VIEW in root->modules->modulename->views->templates->front->filename.tpl
in JS
$('#send').click(function(){
var ciao = 'cioaa';
var myUrl = prestashop.urls.base_url + 'index.php?fc=module&module=configuratore';
$.ajax({
type: 'get',
cache:false,
url: myUrl,
data: {
ajax: true,
datas:ciao,
action: 'fromAjax',
},
})
.done(function() {
console.log('Success!');
})
.fail(function() {
console.log('error');
});
});
in PHP
class ConfiguratoreTaskModuleFrontController extends ModuleFrontController
{
public function __construct()
{
parent::__construct();
}
public function init()
{
parent::init();
}
public function initContent()
{
parent::initContent();
$this->setTemplate('module:configuratore/views/templates/front/task.tpl');
}
$this->fromAjax();
}
public function fromAjax()
{
$mVar = Tools::getValue('datas');
return $this->context->smarty->assign(array('var'=>$mVar));
}
in TPL
{$var}
<?
public function fromAjax()
{
$mVar = Tools::getValue('datas');
$this->context->smarty->assign(array('var'=>$mVar));
$templateFile = 'module:configuratore/views/templates/front/task.tpl';
$html = $this->fetch($templateFile);
die($html); // pass to JS
}
in JS:
.done(function(html) {
console.log(html);
})
I get 500 internal server error if try get data from another Model(Product) in Cart model. using ajax. But if I only comment out the $this->Product->findBy(id) is working fine.
$('form#add_form').on('submit', function(e){
var thisForm = $(this);
$.ajax({
url: thisForm.attr('action'),
type: 'POST',
//dataType: 'json',
data: thisForm.serialize(),
success: function(count) {
var total_items = $('p#total-items');
total_items.html('Total items: ' + count);
console.log(count);
}
});
e.preventDefault();
});
this is my CartsController
class CartsController extends AppController {
public $uses = array('Product', 'Cart');
public function add() {
$this->autoRender = false;
$itemsInCart = $this->Session->read();
if ($this->request->is('POST')) {
$item = $this->Product->findBy($this->request->data['Cart']['product_id']);
}
echo $this->request->data['Cart']['product_id'];
}
}
there is no findBy method
You must change your controller code like this:
class CartsController extends AppController {
public $uses = array('Product', 'Cart');
public function add() {
$this->autoRender = false;
$itemsInCart = $this->Session->read();
if ($this->request->is('POST')) {
$productID = $this->request->data['Cart']['product_id'];
$item = $this->Product->findById($productID);
}
echo json_encode($this->request->data['Cart']['product_id']);
}
}