What am i not doing right? i'm woking on a livewire project that needs to get the grade_id's to show in show-component.blade.php, from Modules table which is related to a Grades table. My challenge is that, it only show a particular id, like the one in the mount section, so if i change it to 2 it will only show id's that are 2's, though thats really what i wanted but i can be change it manually.
use App\Models\Grade;
use App\Models\Module;
use Livewire\Component;
class ShowComponent extends Component
{
public $modules;
public $grades;
public function mount()
{
$this->modules = Module::all()->where("grade_id", "1");
}
public function render()
{
return view('livewire.show-component');
}
}
=========================================================================
show-component.blade.php
#foreach($modules as $module)
<div class="flex flex-col grid-cols-12 md:grid text-gray-50">
<div class="flex md:contents">
<div class="relative col-start-2 col-end-4 mr-10 md:mx-auto">
<div class="flex items-center justify-center w-6 h-full">
<div class="w-1 h-full bg-green-500 pointer-events-none"></div>
</div>
<div class="absolute w-6 h-6 -mt-3 text-center bg-green-500 rounded-full shadow top-1/2">
<i class="text-white fas fa-check-circle"></i>
</div>
</div>
<div class="w-full col-start-4 col-end-12 p-4 my-4 mr-auto bg-green-500 shadow-md rounded-xl">
<h2 class="mb-1 text-lg font-semibold">
{{ $module->name }}
</h2>
<p class="w-full leading-tight text-justify">
<a href="{{ url('storage/videos'.$module->video) }}">
<svg xmlns="http://www.w3.org/2000/svg" class="w-5 h-5" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM9.555 7.168A1 1 0 008 8v4a1 1 0 001.555.832l3-2a1 1 0 000-1.664l-3-2z" clip-rule="evenodd" />
</svg>
</a>
</p>
</div>
</div>
</div>
#endforeach
==================================================================
Module's Model
public function grade()
{
return $this->belongsTo(Grade::class, 'grade_id', 'id');
}
===============================================================
Grade's Model
public function curricula()
{
return $this->belongsTo(Curriculum::class, 'curricula_id', 'id');
}
public function module()
{
return $this->hasMany(Module::class);
}
If you want all Module records stored in your database table, you want to remove the where condition which is limiting the return value.
$this->modules = Module::all();
If you want just the grade_ids from your modules table, use pluck:
$grade_ids = Module::pluck('grade_id');
I think what you want is ..
Add
public $grade_id;
Then in mount
$this->modules = Module::where("grade_id", $this->grade_id)->get();
When you load your component you can pass grade_id to it
Related
I'm working on a Review module in laravel that can sort from All Ratings, Like, and Dislike. However, I'd also like to display the count of All Reviews, likes, and dislikes. Below is my code for the dropdown as well as the model.
Review Dropdown
#props(['routeName', 'username'])
<div class="flex items-center">
<span class="text-sm text-gray-700 uppercase m-1">{{ __('Review:') }}</span>
<div class="relative" x-data="{ open: false }" #click.outside="open = false" #close.stop="open = false">
<div #click="open = ! open">
<button class="flex items-center text-sm font-medium text-gray-500 hover:text-gray-700 hover:border-gray-300 focus:outline-none focus:text-gray-700 focus:border-gray-300 transition duration-150 ease-in-out">
<div>{{ App\Models\Review::RATING[request()->rating] ?? __('All') }}</div>
<div class="ml-1">
<svg class="fill-current h-4 w-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path fill-rule="evenodd" d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z" clip-rule="evenodd" /></svg>
</div>
</button>
</div>
<div x-show="open" class="absolute z-50 mt-2 w-20 rounded-md shadow-lg" style="display: none;" #click="open = false"> {{-- origin-top-right right-0 --}}
<div class="rounded-md ring-1 ring-black ring-opacity-5 py-1 bg-white overflow-y-auto py-1 h-30">
<x-dropdown-link href="{{ route($routeName, [$username]) }}?{{ http_build_query(request()->except('rating', 'page')) }}">
{{ __('All') }}
</x-dropdown-link>
#foreach(App\Models\Review::RATING as $value => $label)
<x-dropdown-link href="{{ route($routeName, [$username]) }}?rating={{ $value }}&{{ http_build_query(request()->except('rating', 'page')) }}">
{{ __($label) }}
</x-dropdown-link>
#endforeach
</div>
</div>
</div>
</div>
ReviewModel
class Review extends Model
{
use HasFactory;
protected $table = 'reviews';
protected $fillable = [
'customer_username',
'customer_id',
'business_id',
'rating',
'feedback'
];
const RATING = [
'1' => 'Dislike',
'2' => 'Like'
];
public function scopeFilter($query, $rating){
$query->when($rating ?? false, function ($query, $rating) {
$query->where(function ($query) use ($rating) {
$query->where('rating', $rating);
});
});
}
}
This is what I intend it to look like:
I did try adding ::count() and ->count() on App\Models\Review::RATING after the foreach in the dropdown but it broke the application.
I’ve made a very simple profile card with two views once is showing me the profile id,name and email and the second view just show the input fields for this three attributes.
This is working but it is “very” slow. When i’m hitting the button Edit on the show view, it takes more than 650ms(in best scenario, sometimes it takes more than 1.2sec) to load the edit view and vice versa.
How can I make this a little bit faster ?
Profile Component:
namespace App\Http\Livewire\User;
use App\User;
use Illuminate\Validation\Rule;
use Livewire\Component;
class Profile extends Component
{
public $user, $user_id, $name, $email;
public $updateMode = false;
public function mount(User $user)
{
$this->user = $user;
$this->user_id = $user->id;
$this->name = $user->name;
$this->email = $user->email;
}
public function render()
{
return view('livewire.user.profile.resource');
}
public function edit()
{
$this->updateMode = true;
}
public function cancel()
{
$this->updateMode = false;
}
public function submit()
{
$attributes = $this->validate([
'name' => 'required|min:6',
'email' => ['required', 'email', Rule::unique('users')->ignore($this->user->id)],
]);
$this->user->update($attributes);
$this->updateMode = false;
}
}
And this are the views:
resource.blade.php:
<div class="p-3">
#includeWhen(!$updateMode,'livewire.user.profile.show')
#includeWhen($updateMode,'livewire.user.profile.edit')
</div>
show.blade.php:
<div>
<div class="flex items-center py-2">
<div class="w-1/4">
<span class="text-gray-800">Id:</span>
</div>
<div class="w-3/4" >
<span class="text-gray-700 font-semibold">{{ $user_id }}</span>
</div>
</div>
<div class="flex items-center py-2">
<div class="w-1/4">
<span class="text-gray-800">Name:</span>
</div>
<div class="w-3/4" >
<span class="text-gray-700 font-semibold">{{ $name }}</span>
</div>
</div>
<div class="flex items-center py-2">
<div class="w-1/4">
<span class="text-gray-800">Email:</span>
</div>
<div class="w-3/4" >
<span class="text-gray-700 font-semibold">{{ $email }}</span>
</div>
</div>
<!-- Editing Buttons -->
<div class="pt-3">
<button type="button" wire:click.prevent="edit" class="py-1 px-2 rounded bg-blue-500 text-white font-semibold">Edit</button>
</div>
</div>
edit.blade.php:
<form wire:submit.prevent="submit">
<div class="flex items-center py-2">
<div class="w-1/4">
<span class="text-gray-800">Id:</span>
</div>
<div class="w-3/4" >
<span class="text-gray-700 font-semibold">{{ $user_id }}</span>
</div>
</div>
<div class="flex items-center py-2">
<div class="w-1/4">
<span class="text-gray-800">Name:</span>
</div>
<div class="w-3/4" >
<input type="text" class="w-1/2 border appearance-none py-1 px-2 rounded shadow focus:outline-none" wire:model="name">
</div>
</div>
<div class="flex items-center py-2">
<div class="w-1/4">
<span class="text-gray-800">Email:</span>
</div>
<div class="w-3/4" >
<input type="text" class="w-1/2 border appearance-none py-1 px-2 rounded shadow focus:outline-none" wire:model="email">
</div>
</div>
<!-- Editing Buttons -->
<div class="pt-3">
<button type="submit" class="py-1 px-2 rounded bg-blue-500 text-white font-semibold" >Save</button>
Cancel
</div>
</form>
I ran into the same issue, with the same implementation approach. I also noticed that it was a bit slow when Livewire would change the state to show/hide the form, so what I did was use Alpine.js for determining whether to show the form or not.
<div x-data="{ mode: 'view' }">
<div x-show="mode === 'edit'">
<div>
<!-- display form here -->
</div>
<button wire:click="update">
Save
</button>
<button #click.prevent="mode = 'view'">
Cancel
</button>
</div>
<div x-show="mode !== 'edit'">
<div>
<!-- profile displayed here -->
</div>
<button #click.prevent="mode = 'edit'">
Edit
</button>
</div>
</div>
Doing it this way resolved the issue with displaying the form, which feels really snappy and quick. I have run into a different issue since that I’m not sure how to address.
If I begin to type in the form to update the name, and quickly hit the save button before the requests to update the component properties complete, then what ever the properties were at the time that I hit the save button are what gets saved to the database, creating a race condition.
I’m still relatively new to the Livewire paradigm so I don’t have a good answer for this yet.
I am running into a very strange issue where I have a chatroom that should update whenever a new message is posted. Instead when I add a new message the x-data is getting updated and I can see the message there but the x-for doesnt seem to run and it never appears on the frontend. Then when I enter another message the previous message shows up on the frontend but the latest message does not show even though the x-data has been updated to reflect it.
Here is the code below:
<div
x-data="{{ json_encode(['messages' => $messages, 'messageBody' => '']) }}"
x-init="
Echo.join('chat.2')
.listen('ChatSent', (e) => {
#this.call('incomingMessage', e)
console.log(e)
})
">
<div
class="wow fadeInUp mb-10 rounded-md bg-light-bg dark:bg-dark"
data-wow-delay="0s"
>
<h3
class="border-b border-body-color border-opacity-10 py-4 px-8 text-lg font-semibold text-black dark:border-white dark:border-opacity-10 dark:text-white"
>
Chat
</h3>
<div id="chat-window" class="relative w-full p-3 overflow-y-auto h-[14rem]">
<ul class="space-y-2">
<template
x-for="message in messages"
:key="message.id"
>
<div>
<template x-if="message.user_id == {{ Auth::user()->id }}">
<li class="flex justify-end">
<div class="relative max-w-xl px-4 py-2 text-black dark:text-white bg-gray-100 dark:bg-black rounded shadow">
<span class="block" x-text="message.body"></span>
</div>
</li>
</template>
<template x-if="message.user_id != {{ Auth::user()->id }}">
<li class="flex justify-start">
<div class="relative max-w-xl px-4 py-2 text-black dark:text-white bg-blue-100 dark:bg-[#1e2a78] rounded shadow">
<span class="block" x-text="message.body"></span>
</div>
</li>
</template>
</div>
</template>
</ul>
</div>
<div class="flex items-center justify-between w-full p-3 border-t border-gray-300">
<input
#keydown.enter="
#this.call('sendMessage', messageBody)
messageBody = ''
"
x-model="messageBody"
type="text"
placeholder="Message"
class="block w-full py-2 pl-4 mx-3 bg-gray-100 dark:bg-black rounded-full outline-none focus:text-gray-700 dark:focus:text-white dark:text-white"
name="message" required />
<button
#click="
#this.call('sendMessage', messageBody)
messageBody = ''
">
<svg class="w-5 h-5 text-black dark:text-white origin-center transform rotate-90" xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20" fill="currentColor">
<path
d="M10.894 2.553a1 1 0 00-1.788 0l-7 14a1 1 0 001.169 1.409l5-1.429A1 1 0 009 15.571V11a1 1 0 112 0v4.571a1 1 0 00.725.962l5 1.428a1 1 0 001.17-1.408l-7-14z" />
</svg>
</button>
</div>
#error('messageBody')
<div class="w-full p-2 text-center">
<span class="text-red-500">Message is required!</span>
</div>
#enderror
</div>
<div
class="wow fadeInUp mb-10 rounded-md bg-light-bg dark:bg-dark"
data-wow-delay="0s">
<h3
class="border-b border-body-color border-opacity-10 py-4 px-8 text-lg font-semibold text-black dark:border-white dark:border-opacity-10 dark:text-white"
>
Active Players
</h3>
<ul class="flex flex-wrap py-6 px-8">
#forelse($here as $authData)
<li>
<a
href="javascript:void(0)"
class="text-body-color-3 mr-3 mb-3 inline-flex items-center justify-center rounded-full border-[.5px] border-body-color bg-body-color bg-opacity-10 py-2 px-4 hover:border-primary hover:bg-primary hover:text-white dark:border-[#363D68] dark:bg-[#272E5C] dark:text-white dark:hover:border-primary dark:hover:bg-primary dark:hover:text-white"
>
{{ $authData['name'] }}
</a>
</li>
#empty
#endforelse
</ul>
</div>
</div>
I figure it has to be something simple I am missing and hopefully someone can point me in the right direction.
EDIT to include Livewire code:
<?php
namespace App\Http\Livewire;
use App\Events\ChatSent;
use App\Models\Chat;
use App\Models\Games;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;
class Chatbox extends Component
{
public $game_id, $game, $chats;
public $here = [];
public $messages = [];
public function getListeners()
{
$game_id = $this->game_id;
return [
"echo-presence:chat.{$game_id},here" => 'here',
"echo-presence:chat.{$game_id},joining" => 'joining',
"echo-presence:chat.{$game_id},leaving" => 'leaving',
];
}
public function render()
{
return view('games.components.chatbox');
}
public function mount()
{
$this->game = Games::find($this->game_id);
$this->messages = Chat::
where('games_id', $this->game_id)
->with('user')
->latest()
->limit(30)
->get()
->reverse()
->values()
->toArray();
}
public function sendMessage($body)
{
if (! $body) {
$this->addError('messageBody', 'Message body is required.');
return;
}
$message = Auth::user()->chats()->create([
'body' => $body,
'games_id' => $this->game_id,
]);
$message->load('user');
broadcast(new ChatSent($message, $this->game))->toOthers();
$myMessage = $message->toArray();
$this->dispatchBrowserEvent('update-chat');
array_push($this->messages, $myMessage);
}
/**
* #param $message
*/
public function incomingMessage($message)
{
// get the hydrated model from incoming json/array.
$message = Chat::with('user')->find($message['id']);
array_push($this->messages, $message);
$this->dispatchBrowserEvent('update-chat');
}
/**
* #param $data
*/
public function here($data)
{
$this->here = $data;
}
/**
* #param $data
*/
public function leaving($data)
{
$here = collect($this->here);
$firstIndex = $here->search(function ($authData) use ($data) {
return $authData['id'] == $data['id'];
});
$here->splice($firstIndex, 1);
$this->here = $here->toArray();
}
/**
* #param $data
*/
public function joining($data)
{
$this->here[] = $data;
}
}
im trying to take 3 most viewed post but its just getting only two I don't know why I have tried limit () method it's not showing anything what am I doing wrong here ,
<?php
namespace App\Http\Livewire;
use App\Models\Movie;
use Livewire\Component;
class MovieBox extends Component
{
public function render()
{
return view('livewire.movie-box',[
'movies' => Movie::withCount('read_count')->orderBy('reads', 'desc')->take(3)->get(),
]);
}
}
movie box
<div>
<div class="relative grid grid-cols-3 gap-2 mt-3 user_story md:grid-cols-5">
#foreach ( $movies as $movie )
<a href="#create-post" uk-toggle="target: body ; cls: story-">
<div class="single_story">
<img src="assets/images/avatars/avatar-lg-1.jpg" alt="">
<div class="story-avatar"> <img src="assets/images/avatars/avatar-1.jpg" alt=""></div>
<div class="story-content">
<h4> {{ $movie->title }}</h4>
</div>
</div>
</a>
#endforeach
<span
class="absolute z-10 items-center justify-center hidden p-2 -mr-4 text-xl bg-white rounded-full shadow-md lg:flex w-9 uk-position-center-right"
uk-toggle="target: body ; cls: story-">
<i class="icon-feather-chevron-right"></i></span>
</div>
Change your render method like this.
public function render()
{
return view('livewire.movie-box',[
'movies' => Movie::withCount('read_count')->orderBy('reads', 'desc')->limit(3)->get(),
]);
}
Basically I'm using the #entangle to share the state between my Livewire component and my Alpine component.
<div x-data="{ open:#entangle('showModal') }" #keydown.escape="showModal=false">
<button type="button" class="block p-2 btn-light-green" #click="showModal=true">Añadir Mezcla</button>
<!--Overlay-->
<div class="overflow-auto" style="background-color: rgba(0,0,0,0.5)" x-show="showModal" :class="{ 'absolute inset-0 z-10 flex items-center justify-center': showModal }">
<!--Dialog-->
<div class="bg-white w-11/12 md:max-w-md mx-auto rounded shadow-lg py-4 text-left px-6"
x-show="showModal"
#click.away="showModal=false"
x-transition:enter="ease-out duration-300"
x-transition:enter-start="opacity-0"
x-transition:enter-end="opacity-100"
x-transition:leave="ease-in duration-300"
x-transition:leave-start="opacity-100"
x-transition:leave-end="opacity-0">
<!-- Title -->
<div class="flex justify-between items-center mb-3">
<p class="text-xl font-bold">Añadir una mezcla</p>
<div class="cursor-pointer z-50" #click="showModal=false">
<svg class="fill-current text-black" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 18 18">
<path d="M14.53 4.53l-1.06-1.06L9 7.94 4.53 3.47 3.47 4.53 7.94 9l-4.47 4.47 1.06 1.06L9 10.06l4.47 4.47 1.06-1.06L10.06 9z"></path>
</svg>
</div>
</div>
<!-- Content -->
<livewire:mix.user.create-mix-form/>
</div>
<!-- /Dialog -->
</div>
<!-- /Overlay -->
</div>
This is my Livewire Component:
<?php
namespace App\Http\Livewire\Mix\User;
use App\Models\Mix;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;
use Livewire\WithPagination;
class ShowUserMixes extends Component
{
use WithPagination;
protected $listeners = [
'deletedMix' => 'render',
'addedMix' => 'render'
];
public $showModal = false;
public function closeModal()
{
$this->showModal = false;
}
public function deleteUserMix($mix_id, $user_id)
{
if (Auth::user()->id !== $user_id) {
return false;
}
$mix = Mix::find($mix_id);
$mix->delete();
$this->emitSelf('deletedMix');
}
public function render()
{
return view('livewire.mix.user.show-user-mixes', [
'mixes' => Auth::user()->mixes()->where('active', 1)->paginate(10)
]);
}
}
This is the error I'm getting at the console:
Uncaught ReferenceError: showModal is not defined
at eval (eval at saferEval (alpine.js?df24:1701), :3:36)
at saferEval (alpine.js?df24:112)
at Component.evaluateReturnExpression (alpine.js?df24:1581)
at eval (alpine.js?df24:1557)
at Array.forEach ()
at Component.resolveBoundAttributes (alpine.js?df24:1530)
at Component.initializeElement (alpine.js?df24:1446)
at eval (alpine.js?df24:1430)
at eval (alpine.js?df24:1420)
at walk (alpine.js?df24:84)
I don't know what might me happening here, I'm using the #entangle properties as the Livewire docs do.
https://laravel-livewire.com/docs/2.x/alpine-js#extracting-blade-components
When you #entangle a property in AlpineJS, you're making a reference form your Alpine Property to another property in your Livewire Component.
Your issue is at the top of your modal definition, in your x-data declaration.
If you use x-data="{ open: #entangle('showModal') }", your showModal property will be bound inside alpine with the name open.
You only need to change the name of your definition:
<div
x-data="{ showModal: #entangle('showModal') }"
<!-- Other attributes here -->
>
<!-- Your modal goes here -->
</div>
You are trying to set and evaluate your livewire variable showModal when you should be setting and evaluating the property you set with Alpine which is open.
Try:
<div x-data="{ open:#entangle('showModal') }" #keydown.escape="open=false">
<button type="button" class="block p-2 btn-light-green" #click="open=true">Añadir Mezcla</button>
<!--Overlay-->
<div class="overflow-auto" style="background-color: rgba(0,0,0,0.5)" x-show="open" :class="{ 'absolute inset-0 z-10 flex items-center justify-center': open }">
<!--Dialog-->
<div class="bg-white w-11/12 md:max-w-md mx-auto rounded shadow-lg py-4 text-left px-6"
x-show="open"
#click.away="open=false"
x-transition:enter="ease-out duration-300"
x-transition:enter-start="opacity-0"
x-transition:enter-end="opacity-100"
x-transition:leave="ease-in duration-300"
x-transition:leave-start="opacity-100"
x-transition:leave-end="opacity-0">
<!-- Title -->
<div class="flex justify-between items-center mb-3">
<p class="text-xl font-bold">Añadir una mezcla</p>
<div class="cursor-pointer z-50" #click="open=false">
<svg class="fill-current text-black" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 18 18">
<path d="M14.53 4.53l-1.06-1.06L9 7.94 4.53 3.47 3.47 4.53 7.94 9l-4.47 4.47 1.06 1.06L9 10.06l4.47 4.47 1.06-1.06L10.06 9z"></path>
</svg>
</div>
</div>
<!-- Content -->
<livewire:mix.user.create-mix-form/>
</div>
<!-- /Dialog -->
</div>
<!-- /Overlay -->
</div>