How can i display the element of a selected Id in vuejs? - php

I have a page called Home.vue i have a router link which is id once users click it it takes them to the page, how can i display the data elements of the clicked id?
<li class="ta-track-card column col-2 flex-column" v-for="faq in faqs">
<div class="inner">
<div class="artwork" role="link">
<router-link :to="`/album/${faq.id}`"><span :style="`background-image: url('http://localhost/mymusic/${faq.artworkPath}');`"></span></router-link>
</div>
<div class="info">
<div class="title white-primary-hover"><router-link :to="`/album/${faq.id}`">{{ faq.title }}</router-link></div>
<div class="username light-white-hover">
<span>by </span>
<router-link :to="`/artist/${faq.artistId}`">{{ faq.artist }}</router-link>
</div>
<div class='released'>Released On {{ faq.albumdate }}</div>
</div>
</div>
</li>
Below is the Album.vue Page where i display the data
<div class="entityInfo">
<div class="leftSection" v-for="faq in faqs">
<img :src="/""faq.artworkPath">
</div>
<div class="rightSection" v-for="faq in faqs" :key="faq.id">
<h2 class="_good">{{ faq.title }}</h2>
<p class="_me">By {{ faq.artist }}</p>
<p class="_me">1 songs</p>
<p class="_me">{{ faq.albumdate }}</p>
</div>
Below is Script i use:
<script>
import axios from 'axios';
export default {
name: 'album',
data: () =>({
faqs: [],
songs: [],
errors: []
}),
created() {
axios.get('http://localhost/mymusic/rest/api/album/read')
.then(response => {
this.faqs = response.data.data;
})
.catch(e => {
console.log(e),
this.errors.push(e)
})
}
}
</script>
Below is the Sample API:
{
"data": [
{
"id": "41",
"artistId": "38",
"title": "Red Handed",
"artist": "Peruzzi & Mayorkun",
"artworkPath": "assets/images/artwork/red.jpg",
"albumdate": "2018-09-24"
},
{
"id": "40",
"artistId": "37",
"title": "FEFE (feat. Nicki Minaj & Murda Beatz)",
"artist": "6ix9ine",
"artworkPath": "assets/images/artwork/fefe.jpg",
"albumdate": "2018-07-22"
},
}
Please how can i display the element of the Id i click from the router

As i can see you are passing the id in the route you can retrieve it using id = this.$route.params.id and store it in a temporary variable.And in created just retrieve the data by passing id in post parameter and changing your query accordingly.Or the other option is to loop over through your retrieved data and filter them from the selected id.
Or rather then filter create the data object as id->value pairs to directly access them through id and you will have to make single api call.

Related

Laravel ajax passing data to php

I have a special problem.
I am making an Ajax request to get data from the db without updating.
The response I get should be outputted in a advanced way like this:
#foreach ($tasks as $task)
<div class="pr-1">
<div onclick="openComponent('modal-task', {{ $task->id }});setGlobalIndex({{ $task->id }});" title="{{ $task->title }}" class="calendar-task hover:opacity-75 cursor-pointer" style="background-color: #{{ $task->color }};">#{{ $task->id }} {{ ucfirst(substr($task->title, 0, 23)) }}</div>
</div>
<div id="modal-task_{{ $task->id }}" class="hidden">
<div class="display">
<div class="top">
<div class="title">Opgave #{{ $task->id }} <div class="inline-block text-blue-600 underline text-sm">Se hele opgaven</div>
</div>
<div onclick="closeComponent('modal-task', {{ $task->id }})" class="close-button"><i class="fas fa-times-circle text-gray-700"></i>
</div>
</div>
#include('component.task')
</div>
#endforeach
Right now I get the correct response from the Ajax request.
color: "e53e3e"
company_id: 1
created_date: "2020-11-13"
created_time: "00:00:00"
description: null
id: 8
status_1: null
status_2: null
status_3: null
title: "Overskrift"
But how do I output it like that, with the include also and it should appear many times (its a calendar system that gets tasks from each days)
The Ajax request POST 2 dates (from and to) till a laravel controller which returns the tasks that have the date in between.
<script>
var datePeriodRow = "<?php echo $datePeriodRow; ?>";
var _token = $('input[name=_token]').val();
$.ajax({
url: "/task/get",
type:"POST",
data:{
datePeriodRow:datePeriodRow,
_token: _token
},
success:function(response){
if(response) {
console.log(response);
}
},
});
</script>
What you can do in your "/task/get" endpoint method is to return a view that returns a rendered html. then set the rendered response with javascript in a div element instead of that foreach loop.
So place the foreach code part in an include/partial like partial/tasks.blade.php and in your endpoint method:
/** route: /task/get **/
public function yourTasksMethod(){
//your code that retrieves your tasks
return view('partial.tasks')->with('tasks', $tasks)->render();
}
Keep in mind to clear/empty the div before you make another request to the task/get endpoint.

Displaying data retrieved from json in Laravel

I am beginner webdeveloper. I use in my project Laravel 8 and Vue.
I have this code:
<template>
<div class="sm:bg-transparent pt-6 md:mt-16 w-full px-4 md:px-0">
<h1 class="text-dark text-4xl sm:text-3xl font-medium sm:text-center mb-6">{{ __('Archiwum') }}</h1>
<preloader
v-if="isLoadingInProgress"
class="py-8"
:class="preloaderClass"
/>
<div class="text-center" v-if="gasmeters.length == 0 && !isLoadingInProgress">
{{ __('Nie posiadasz jeszcze żadnych odczytów') }}
</div>
<div v-if="gasmeters.length">
<div class="bg-white rounded border border-stroke flex items-start p-6"
v-for="(gasmeter, index) in gasmeters"
v-bind:key="`gasmeter-${index}`">
<div class="mr-6">
<img class="mb-2" :src="gasmeters.img"/> {{ gasmeters }}
</div>
<div class="w-1/5">
<span class="font-bold text-xl text-grey">Lipiec 2020 (#123)</span>
</div>
<div class="w-1/5">
<span class="font-bold text-xl text-grey">Stan:</span> <span>2120</span>
</div>
<div class="w-1/5">
<span class="font-bold text-xl text-grey">Data:</span> <span>2020.01.12</span>
</div>
<div class="w-1/5">
<span class="text-xl">Zgłoszone przez aplikację</span>
</div>
</div>
</div>
</div>
</template>
<script>
import axios from "axios";
import Preloader from "#/common/components/Preloader";
export default {
components: {
Preloader
},
mounted() {
this.loadGasmeters();
},
data() {
return {
gasmeters: [],
isLoadingInProgress: false,
}
},
computed: {
preloaderClass() {
return this.gasmeters.length > 0
? ""
: "loading--transparent loading--block";
}
},
methods: {
loadGasmeters() {
this.isLoadingInProgress = true;
axios
.get(route('api.archive.list'))
.then(response => {
this.gasmeters = _.get(response, "data.data", []);
console.log(this.gasmeters);
})
.catch(error => {
console.log(error);
})
.then(() => {
this.isLoadingInProgress = false;
});
},
}
}
</script>
My php return me:
{
"data": [
[
{
"id": 1,
"value": "99.00",
"settlement_date": "2020-10-20T22:00:00.000000Z",
"loaded_from": "Stan pobrany z systemu BAŁTYKGAZ",
"img": "img/icons/crm.svg",
"created_at": "2020-10-21T15:09:22.000000Z"
},
{
"id": 2,
"value": "109.00",
"settlement_date": "2020-10-20T22:00:00.000000Z",
"loaded_from": "Zgłoszone przez aplikację",
"img": "img/icons/cms.svg",
"created_at": "2020-10-21T17:02:18.000000Z"
}
]
]
}
I have problem with my part this code:
<div v-if="gasmeters.length">
<div class="bg-white rounded border border-stroke flex items-start p-6"
v-for="(gasmeter, index) in gasmeters"
v-bind:key="`gasmeter-${index}`">
<div class="mr-6">
<img class="mb-2" :src="gasmeters.img"/> {{ gasmeters }}
</div>
<div class="w-1/5">
<span class="font-bold text-xl text-grey">Lipiec 2020 (#123)</span>
</div>
<div class="w-1/5">
<span class="font-bold text-xl text-grey">Stan:</span> <span>2120</span>
</div>
<div class="w-1/5">
<span class="font-bold text-xl text-grey">Data:</span> <span>2020.01.12</span>
</div>
<div class="w-1/5">
<span class="text-xl">Zgłoszone przez aplikację</span>
</div>
</div>
</div>
I want replace my img by img from json, "Lipiec 2020 (#123)" => gasmeters.settlement_date (# gasmeters.id), "Stan: " 2120 => Stan: gasmeters.value
How can I make it?
This: {{ gasmeters }} return me:
[ [ { "id": 1, "value": "99.00", "settlement_date": "2020-10-20T22:00:00.000000Z", "loaded_from": "Stan pobrany z systemu BAŁTYKGAZ", "img": "img/icons/crm.svg", "created_at": "2020-10-21T15:09:22.000000Z" }, { "id": 2, "value": "109.00", "settlement_date": "2020-10-20T22:00:00.000000Z", "loaded_from": "Zgłoszone przez aplikację", "img": "img/icons/cms.svg", "created_at": "2020-10-21T17:02:18.000000Z" } ] ]
Please help me
I saw Your problem. Because your response data from service API Laravel is the array in array. If you want to get your right data, you can do in 2 way:
You change the front end data when get from service API, like this:
axios.get(route('api.archive.list'))
.then(response => {
this.gasmeters = _.get(response, "data.data[0]", []);
console.log(this.gasmeters);
})
.catch(error => {
console.log(error);
})
.then(() => {
this.isLoadingInProgress = false;
});
Or you can change the Laravel API like this:
{
"data": [{
"id": 1,
"value": "99.00",
"settlement_date": "2020-10-20T22:00:00.000000Z",
"loaded_from": "Stan pobrany z systemu BAŁTYKGAZ",
"img": "img/icons/crm.svg",
"created_at": "2020-10-21T15:09:22.000000Z"
},
{
"id": 2,
"value": "109.00",
"settlement_date": "2020-10-20T22:00:00.000000Z",
"loaded_from": "Zgłoszone przez aplikację",
"img": "img/icons/cms.svg",
"created_at": "2020-10-21T17:02:18.000000Z"
}
]
}

Vue2 Laravel component in list

I am building a Laravel app and trying to use vue.js (without much success!). I'm not understanding the way components work with ajax data. Almost all examples I've found showing this functionality define the data for the component at the app level, not the component level.
I'm trying to dynamically define my data in the component itself, and always get the error that Property or method tasks is not defined on the instance but referenced during render. Here's the component, which is meant to just call out to an endpoint to pull basic "to do" tasks:
Vue.component('tasks', {
data: function() {
return {
tasks: []
}
},
mounted() {
this.getTasks();
},
methods: {
getTasks() {
axios.get('/tasks').then(function (response) {
this.tasks = response.data;
console.dir(this.tasks);
})
.catch(function (error) {
console.log(error);
});
}
},
template: `
<div class="card">
<div class="card-title">{{ task.name }}</div>
<div class="card-body">
<div class="service-desc">{{ task.description }}</div>
<div class="task-notes"><input class="form-control" v-model="task.notes" placeholder="Notes"></div>
<div class="task-active"><input type="checkbox" checked data-toggle="toggle" data-size="sm" v-model="task.active" v-on:click="$emit('disable')"></div>
</div>
</div>
`
});
the component is called from within the blade template using:
<tasks v-for="task in tasks" :key="task.id"></tasks>
tasks is declared in the data function, so I'm not sure why vue is telling me it's not defined?
When you define a data property on a component it's only available within that component and its template. Your v-for directive is in the parent scope (i.e outside of the component where tasks is defined).
The simplest solution here is probably to move the container element inside the component, and iterate over the tasks there:
<div>
<div class="card" v-for="task in tasks" :key="task.id">
<div class="card-title">{{ task.name }}</div>
<div class="card-body">
<div class="service-desc">{{ task.description }}</div>
<div class="task-notes"><input class="form-control" v-model="task.notes" placeholder="Notes"></div>
<div class="task-active"><input type="checkbox" checked data-toggle="toggle" data-size="sm" v-model="task.active" v-on:click="$emit('disable')"></div>
</div>
</div>
</div>
Note: you can't use v-for a template's root element, which is why you'd move the container element into the template.
An alternative is break this into two components (e.g. TaskList and TaskItem) where the parent component is responsible for fetching the tasks from the API. The child component can just receive a single task as a prop and render it to the UI.
TaskList
Vue.component('task-list', {
data: function() {
return {
tasks: []
}
},
mounted() {
this.getTasks();
},
methods: {
getTasks() {
axios.get('/tasks').then(response => {
this.tasks = response.data;
console.dir(this.tasks);
})
.catch(error => {
console.log(error);
});
}
},
template: `
<div class="container">
<task-item
v-for="task in tasks"
:key="task.id"
:task="task"
/>
</div>
`
});
TaskItem
Vue.component('tasks', {
props: {
task: {
required: true
}
},
template: `
<div class="card">
<div class="card-title">{{ task.name }}</div>
<div class="card-body">
<div class="service-desc">{{ task.description }}</div>
<div class="task-notes"><input class="form-control" v-model="task.notes" placeholder="Notes"></div>
<div class="task-active"><input type="checkbox" checked data-toggle="toggle" data-size="sm" v-model="task.active" v-on:click="$emit('disable')"></div>
</div>
</div>
`
});
The advantage of this is that it separates the responsibility of the components a little better. You could add logic to the TaskList component to handle displaying a loading spinner and/or error messages for the API call, while TaskItem only has to concern itself with displaying a single task.

Laravel foreach issues

I get stuck on a foreach loop inside my blade template after hours of trial and failure I need some help.
My Controller
public function menue() {
$restaurants = User::with('articles')->get();;
return view('pages.menues')->withRestaurants($restaurants);
}
My foreach
#foreach($restaurants as $restaurant)
<div class="panel panel-default">
<div class="panel-heading">
#foreach($restaurant->articles as $article)
{{$article->title}}
<span class="float-right">{{$article->published_at}}</span>
#endforeach
</div>
<div class="panel-body">
#foreach($restaurant->articles as $article)
{{$article->body}}
#endforeach
{{$restaurant->name}}
</div>
</div>
#endforeach
This is what I'm trying to loop trough:
{
"id":1,
"name":"Sam",
"email":"sam#me.com",
"created_at":"2016-07-26 15:03:51",
"updated_at":"2016-07-27 15:39:55",
"articles":[
{
"id":1,
"user_id":1,
"title":"Monday Afternoon",
"body":"got it",
"created_at":"2016-07-27 15:31:05",
"published_at":"2016-07-27 15:30:00",
"excerpt":null,
"updated_at":"2016-07-27 15:31:05"
},
{
"id":3,
"user_id":1,
"title":"Good Morning Wednesday",
"body":"lorem ipsum",
"created_at":"2016-07-27 11:38:37",
"published_at":"2016-07-27 11:38:00",
"excerpt":null,
"updated_at":"2016-07-27 11:38:37"
},
{
"id":4,
"user_id":1,
"title":"Good Morning Thursday",
"body":"lorem ipsum ",
"created_at":"2016-07-27 11:39:14",
"published_at":"2016-07-28 14:38:00",
"excerpt":null,
"updated_at":"2016-07-27 11:39:14"
},
{
"id":5,
"user_id":1,
"title":"Wednesday Afternoon",
"body":"Hallo Welt",
"created_at":"2016-07-27 14:55:00",
"published_at":"2016-07-27 14:54:00",
"excerpt":null,
"updated_at":"2016-07-27 14:55:00"
}
]
}
Output of my blade template
The result is instead of four posts I get only 2 posts. Each of them contains to other posts. How can I display all 4 posts individually in my view?
Try This
#foreach($restaurants as $restaurant)
#foreach($restaurant->articles as $article)
<div class="panel panel-default">
<div class="panel-heading">
{{$article->title}}
<span class="float-right">{{$article->published_at}}</span>
</div>
<div class="panel-body">{{$article->body}}</div>
</div>
#endforeach
#endforeach

Multiple Jplayer audio players from Laravel DB

I'm trying to create multiple instances of Jplayers that each play a different audio file from my Laravel Database (table called archives) and i'm stuck at the point the jQuery code meets the Laravel code.
So far the audio players play but they play the same file.
also i have trouble when i navigate deeper into the website. ex: with mydomain.com/segments/ the audio player adds "segments" to the url instead of just the public directory. I know a similar issue can be solved with the URL:asset in Laravel, but i'm not sure how to with this since it's the jquery that's getting the file.
This is where i try to create my multiple jplayer instances using a laravel Forloop (i need to start at 2 because player 1 is being used elsewhere)I'm confused about how to mix the Laravel and Jquery code.
This is all in my footer.blade.php
<!--{{{$counter = 2}}}-->
#foreach ($archives as $archive)
<script>
var counter = "{{$counter}}";
var file = "archiveAudio/{{$archive->audioFile}}";
$(document).ready(function(){
$("#jquery_jplayer_"+ counter).jPlayer({
ready: function () {
$(this).jPlayer("setMedia", {
title: "Hidden",
mp3: file,
oga: ""
});
},
play: function() { // To avoid multiple jPlayers playing together.
$(this).jPlayer("pauseOthers");
},
swfPath: "../../js",
supplied: "mp3, oga",
cssSelectorAncestor: "#jp_container_" + counter,
wmode: "window",
globalVolume: true,
useStateClassSkin: true,
autoBlur: false,
smoothPlayBar: true,
keyEnabled: true
});
counter ++;
});
</script>
#endforeach
Home page Blade View (needs to start with #2 player add 1 to the player ID for each player it creates)
<div id="latest">
<!-- {{ $count = 2}} -->
#foreach ($archives as $archive)
<div class="singleLatest">
<img src = "/images/segments/{{$archive->segment->image}}" width="100px;"/>
<div class="playerAndInfo">
<h3> {{ $archive->archiveTitle}}<br>{{$archive->segment->name}}</h3>
<div id="jquery_jplayer_{{$count}}" class="jp-jplayer"></div>
<div id="jp_container_{{$count}}" class="jp-audio" role="application" aria-label="media player">
<div class="jp-type-single">
<div class="jp-gui jp-interface">
<div class="jp-controls">
<button class="jp-play" role="button" tabindex="0">play</button>
</div>
<div class="jp-progress">
<div class="jp-seek-bar">
<div class="jp-play-bar"></div>
</div>
</div>
<div class="jp-time-holder">
<div class="jp-current-time" role="timer" aria-label="time"> </div>
<div class="jp-duration" role="timer" aria-label="duration"> </div>
</div>
<div class="jp-no-solution">
<span>Update Required</span>
To play the media you will need to either update your browser to a recent version or update your Flash plugin.
</div>
</div>
</div>
</div>
</div><!--end playerAndInfo-->
</div> <!-- end single latest -->
<!-- {{ $count = $count +1}} -->
#endforeach
</div><!-- End latest -->
Thanks!

Categories