I have a component using Laravel livewire which disappear after I submit the form inside of it.
Here is the blade content:
<div class="w-full flex flex-col justify-start items-start px-5 wow delay-100 fadeInUp">
<h3 class="w-full flex flex-row justif-start items-center text-center text-2xl font-medium text-gray-700 mt-5 mb-0">
{{ $survey->name }}
</h3>
<div class="w-full mt-5">
<div class="prose w-full max-w-full">
{!! $survey->description !!}
</div>
</div>
<form wire:submit.prevent="submit" class="w-full mt-10">
#foreach($survey->fields as $field)
<div class="w-full flex flex-col justify-start items-start gap-2 mb-10" wire:key="field{{ $field->id }}">
<span class="font-medium" #class([
'text-gray-700',
'text-danger-700' => $errors_list && $errors_list->has($field->slug)
])>
{{ $field->name }}
#if($field->is_required)
<sup class="font-medium text-danger-700">*</sup>
#endif
</span>
<div class="prose w-full max-w-full">
{!! $field->description !!}
</div>
<div class="w-full">
#switch($field->type)
#case('text')
<div class="w-full flex flex-row justify-start items-center gap-2">
{{ html()->text($field->slug)->class($field_class)->attribute('wire:model.defer', 'model.' . $field->slug) }}
</div>
#break;
#case('textarea')
<div class="w-full flex flex-row justify-start items-center gap-2">
{{ html()->textarea($field->slug)->class($field_class)->attribute('wire:model.defer', 'model.' . $field->slug) }}
</div>
#break;
#case('select')
<div class="w-full flex flex-row justify-start items-center gap-2">
{{ html()->select($field->slug, explode(',', $field->values))->class($field_class)->attribute('wire:model.defer', 'model.' . $field->slug) }}
</div>
#break;
#case('multiselect')
<div class="w-full flex flex-row justify-start items-center gap-2">
{{ html()->multiselect($field->slug, explode(',', $field->values))->class($field_class)->attribute('wire:model.defer', 'model.' . $field->slug) }}
</div>
#break;
#case('file')
<div class="w-full flex flex-row justify-start items-center gap-2">
{{ html()->file($field->slug) }}
</div>
#break;
#case('checkbox')
#foreach(explode(',', $field->values) as $value)
<div class="w-full flex flex-row justify-start items-center gap-2" wire:key="{{ $field->slug . '_' . $value }}">
{{ html()->checkbox($field->slug, false, $value)->id($field->slug . '_' . $value)->attribute('wire:model.defer', 'model.' . $field->slug) }}
{{ html()->label($value, $field->slug . '_' . $loop->index)->class('p-0') }}
</div>
#endforeach
#break;
#case('radio')
#foreach(explode(',', $field->values) as $value)
<div class="w-full flex flex-row justify-start items-center gap-2" wire:key="{{ $field->slug . '_' . $value }}">
{{ html()->radio($field->slug, false, $value)->id($field->slug . '_' . $value)->attribute('wire:model.defer', 'model.' . $field->slug) }}
{{ html()->label($value, $field->slug . '_' . $loop->index)->class('p-0') }}
</div>
#endforeach
#break;
#case('date')
<div class="w-full flex flex-row justify-start items-center gap-2">
{{ html()->date($field->slug, null, __('Y-m-d'))->class($field_class)->attribute('wire:model.defer', 'model.' . $field->slug) }}
</div>
#break;
#case('datetime')
<div class="w-full flex flex-row justify-start items-center gap-2">
{{ html()->datetime($field->slug, null, __('Y-m-d g:i A'))->class($field_class)->attribute('wire:model.defer', 'model.' . $field->slug) }}
</div>
#break;
#case('time')
<div class="w-full flex flex-row justify-start items-center gap-2">
{{ html()->time($field->slug, null, __('g:i A'))->class($field_class)->attribute('wire:model.defer', 'model.' . $field->slug) }}
</div>
#break;
#endswitch
</div>
</div>
#endforeach
<div class="w-full flex flex-row justify-start items-center gap-2">
<button type="submit" class="px-3 py-1 text-white bg-fprimary hover:text-fprimary hover:bg-white border border-fprimary rounded">
{{ __('Submit') }}
</button>
</div>
</form>
</div>
And the submit function, is only checking the errors:
protected function rules(): array
{
return $this->survey->form_rules;
}
public function submit()
{
$this->validate();
}
All works well before the line $this->validate(), after I call this function the component disappear from the rendered view.
But if the form is valid and no errors are fired when form is validated, all works well.
FYI, I am rendering the livewire component in a blade page:
<livewire:survey :survey="$survey"></livewire:survey>
I know this issue is already discussed in other questions, but I tried all the solutions without getting it work.
I found the solution which is simply to remove the <div> element that contains all the other elements and leave the <form> element as the root.
So now I have the following code in my blade file:
<form wire:submit.prevent="submit" class="w-full mt-10 w-full flex flex-col justify-start items-start">
<h3 class="w-full flex flex-row justif-start items-center text-center text-2xl font-medium text-gray-700 mt-5 mb-0">
{{ $survey->name }}
</h3>
<div class="w-full mt-5 mb-10">
<div class="prose w-full max-w-full">
{!! $survey->description !!}
</div>
</div>
#foreach($survey->fields as $field)
<div class="w-full flex flex-col justify-start items-start gap-2 mb-10" wire:key="field{{ $field->id }}">
<span class="font-medium #if($errors->has('model.' . $field->slug)) text-danger-700 #else text-gray-700 #endif">
{{ $field->name }}
#if($field->is_required)
<sup class="font-medium text-danger-700">*</sup>
#endif
</span>
<div class="prose w-full max-w-full">
{!! $field->description !!}
</div>
<div class="w-full">
#switch($field->type)
#case('text')
<div class="w-full flex flex-row justify-start items-center gap-2">
{{ html()->text($field->slug)->class($field_class)->attribute('wire:model.defer', 'model.' . $field->slug) }}
</div>
#break;
#case('textarea')
<div class="w-full flex flex-row justify-start items-center gap-2">
{{ html()->textarea($field->slug)->class($field_class)->attribute('wire:model.defer', 'model.' . $field->slug) }}
</div>
#break;
#case('select')
<div class="w-full flex flex-row justify-start items-center gap-2">
{{ html()->select($field->slug, explode(',', $field->values))->class($field_class)->attribute('wire:model.defer', 'model.' . $field->slug) }}
</div>
#break;
#case('multiselect')
<div class="w-full flex flex-row justify-start items-center gap-2">
{{ html()->multiselect($field->slug, explode(',', $field->values))->class($field_class)->attribute('wire:model.defer', 'model.' . $field->slug) }}
</div>
#break;
#case('file')
<div class="w-full flex flex-row justify-start items-center gap-2">
{{ html()->file($field->slug) }}
</div>
#break;
#case('checkbox')
#foreach(explode(',', $field->values) as $value)
<div class="w-full flex flex-row justify-start items-center gap-2" wire:key="{{ $field->slug . '_' . $value }}">
{{ html()->checkbox($field->slug, false, $value)->id($field->slug . '_' . $value)->attribute('wire:model.defer', 'model.' . $field->slug) }}
{{ html()->label($value, $field->slug . '_' . $loop->index)->class('p-0') }}
</div>
#endforeach
#break;
#case('radio')
#foreach(explode(',', $field->values) as $value)
<div class="w-full flex flex-row justify-start items-center gap-2" wire:key="{{ $field->slug . '_' . $value }}">
{{ html()->radio($field->slug, false, $value)->id($field->slug . '_' . $value)->attribute('wire:model.defer', 'model.' . $field->slug) }}
{{ html()->label($value, $field->slug . '_' . $loop->index)->class('p-0') }}
</div>
#endforeach
#break;
#case('date')
<div class="w-full flex flex-row justify-start items-center gap-2">
{{ html()->date($field->slug, null, __('Y-m-d'))->class($field_class)->attribute('wire:model.defer', 'model.' . $field->slug) }}
</div>
#break;
#case('datetime')
<div class="w-full flex flex-row justify-start items-center gap-2">
{{ html()->datetime($field->slug, null, __('Y-m-d g:i A'))->class($field_class)->attribute('wire:model.defer', 'model.' . $field->slug) }}
</div>
#break;
#case('time')
<div class="w-full flex flex-row justify-start items-center gap-2">
{{ html()->time($field->slug, null, __('g:i A'))->class($field_class)->attribute('wire:model.defer', 'model.' . $field->slug) }}
</div>
#break;
#endswitch
</div>
#error('model.' . $field->slug)
<span class="text-sm font-medium text-danger-700">{{ $message }}</span>
#enderror
</div>
#endforeach
<div class="w-full flex flex-row justify-start items-center gap-2">
<button type="submit" class="px-3 py-1 text-white bg-fprimary hover:text-fprimary hover:bg-white border border-fprimary rounded">
{{ __('Submit') }}
</button>
</div>
</form>
If anyone can give me some information about why this change made it works it will be great, because I don't understand why having a <div> element as root element didn't work, and <form> works.
FYI, I have other pages containing <div> root element and all is working just fine.
Related
Relevant configuration : Php version 8 - laravel 8.
I have been trying to use laravel json translation without success and I don't understand how to make it work. The normal way using shorkeys works just fine as shown in the output below (pictures).
dashboard.blade.php :
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
{{ __('Dashboard') }}
</h2>
</x-slot>
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
<div class="p-6 bg-white border-b border-gray-200">
You're logged in! {{ app()->getLocale() ." : " . __('30 Days') }}
</div>
<div class="p-6 bg-white border-b border-gray-200">
You're logged in! {{ app()->getLocale() ." : " . trans('30 Days') }}
</div>
<div class="p-6 bg-white border-b border-gray-200">
You're logged in! {{ app()->getLocale() ." : " . Lang::get('30 Days') }}
</div>
<div class="p-6 bg-white border-b border-gray-200">
You're logged in! {{ app()->getLocale() ." : " . __('auth.failed') }}
</div>
<div class="p-6 bg-white border-b border-gray-200">
You're logged in! {{ app()->getLocale() ." : " . trans('auth.failed') }}
</div>
<div class="p-6 bg-white border-b border-gray-200">
You're logged in! {{ app()->getLocale() ." : " . Lang::get('auth.failed') }}
</div>
</div>
</div>
</div>
auth.php :
return [
'failed' => 'Diese Kombination aus Zugangsdaten wurde nicht in unserer Datenbank gefunden.',
'password' => 'Das eingegebene Passwort ist nicht korrekt.',
'throttle' => 'Zu viele Loginversuche. Versuchen Sie es bitte in :seconds Sekunden nochmal.',
];
de.json :
{
"30 Days": "30 Tage",
"60 Days": "60 Tage",
"90 Days": "90 Tage",
":amount Total": ":amount Gesamt",
...rest of the translations,
}
I'm trying to change the navigation box a bit so that instead of nav-link it's a dropdown and a dropdown-link. I seem to be able to do this, but I can't turn on the section highlight in the navigation if it's selected. This is how I changed the navigation-links:
<!-- Navigation Links -->
<div class="hidden sm:flex sm:items-center sm:ml-6">
<x-dropdown :href="route('dashboard')" :active="request()->routeIs('dashboard')">
<x-slot name="trigger">
<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">
{{ __('Dashboard') }}
<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>
</x-slot>
<x-slot name="content">
<x-dropdown-link :href="route('dashboard')">
{{ __('Dashboard') }}
</x-dropdown-link>
<x-dropdown-link :href="route('dashboard')">
{{ __('Dashboard') }}
</x-dropdown-link>
<x-dropdown-link :href="route('dashboard')">
{{ __('Dashboard') }}
</x-dropdown-link>
</x-slot>
</x-dropdown>
</div>
This is how I am trying to change the dropdown.blade.php file. Just trying to drag enable enable from nav-link.blade.php file.
#props(['align' => 'right', 'width' => '48', 'contentClasses' => 'py-1 bg-white', 'active'])
#php
switch ($align) {
case 'left':
$alignmentClasses = 'origin-top-left left-0';
break;
case 'top':
$alignmentClasses = 'origin-top';
break;
case 'right':
default:
$alignmentClasses = 'origin-top-right right-0';
break;
}
switch ($width) {
case '48':
$width = 'w-48';
break;
}
$classes = ($active ?? false)
? 'inline-flex items-center px-1 pt-1 border-b-2 border-indigo-400 text-sm font-medium leading-5 text-gray-900 focus:outline-none focus:border-indigo-700 transition duration-150 ease-in-out'
: 'inline-flex items-center px-1 pt-1 border-b-2 border-transparent text-sm font-medium leading-5 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';
#endphp
<div class="relative" x-data="{ open: false }" #click.outside="open = false" #close.stop="open = false" {{ $attributes->merge(['class' => $classes]) }} >
<div #click="open = ! open">
{{ $trigger }}
</div>
<div x-show="open"
x-transition:enter="transition ease-out duration-200"
x-transition:enter-start="transform opacity-0 scale-95"
x-transition:enter-end="transform opacity-100 scale-100"
x-transition:leave="transition ease-in duration-75"
x-transition:leave-start="transform opacity-100 scale-100"
x-transition:leave-end="transform opacity-0 scale-95"
class="absolute z-50 mt-2 {{ $width }} rounded-md shadow-lg {{ $alignmentClasses }}"
style="display: none;"
#click="open = false">
<div class="rounded-md ring-1 ring-black ring-opacity-5 {{ $contentClasses }}">
{{ $content }}
</div>
</div>
</div>
As I see it, according to the developer tool in the browser, in this line
<div class="relative" x-data="{ open: false }" #click.outside="open = false" #close.stop="open = false" {{ $attributes->merge(['class' => $classes]) }} >
I do not apply
{{ $attributes->merge(['class' => $classes]) }}
Rather, it is applied, but somehow strange, it just adds an href link to the div properties.
<div class="relative" x-data="{ open: false }" #click.outside="open = false" #close.stop="open = false" href="http://localhost/dashboard">
Please tell me what I'm doing wrong.
I have an application where an user can change his name via a pop-up.
This is the method that handles the name change:
public function changeVorname(Request $request) {
$this->validate($request, [
'vorname' => 'required|max:255',
]);
Portal::query()->where('email', $request->email)->update(['vorname' => $request->neuer_vorname]);
return redirect()->back()->with('message', 'Vorname erfolgreich geändert');
}
This is the blade file with the modal:
<div class="w-1/2 mb-4 pb-3 text-lg">
<p>Vorname:</p>
<input type="text" name="vorname" id="vorname"
value="{{ \Illuminate\Support\Facades\Auth::guard('portal')->user()->vorname }}"
class="flex text-center bg-gray-100 border-gray-500 shadow-2xl
border-opacity-50 border-2 w-full p-4 rounded-lg #error('vorname') border-red-500 #enderror"
readonly>
#error('vorname')
<div class="text-red-500 mt-2 text-sm">
{{ 'Der Vorname darf keine Zahlen enthalten und nicht leer sein' }}
</div>
#enderror
<div class="text-right">
<button id="change_vorname" class="md:pl-2" name="change_vorname">
Vorname bearbeiten
</button>
</div>
</div>
<div id="change_vorname_modal"
class="modal fixed ml-96 top-20 mx-auto p-5 border w-96 shadow-lg rounded-md bg-white hidden">
<div class="mt-3 text-center text-xl">
Neuer Vorname
</div>
<div class="items-center px-4 py-3">
<label for="neuer_vorname" class="sr-only">Neuer Vorname</label>
<form
action="{{ route('change_vorname', \Illuminate\Support\Facades\Auth::guard('portal')->user()->email) }}"
method="post">
#csrf
<input type="text" name="neuer_vorname" id="neuer_vorname"
placeholder="Neuer Vorname" value=""
class="flex text-center text-sm mb-2 bg-gray-100 border-gray-500 shadow-2xl border-opacity-50 border-2 w-full p-4 rounded-lg">
<button type="submit" id="ok_btn" class="mb-4 pb-3 w-full text-white px-4 py-3 rounded text-base font-medium
bg-gradient-to-r from-green-400 to-blue-500 float-right shadow transition
duration-500 ease-in-out transform hover:-translate-y-1 hover:scale-100
shadow-2xl border-2 w-full p-4 rounded-lg">
Vorname ändern
</button>
</form>
</div>
<div class="items-center px-4 py-3">
<button id="back" class="mb-4 pb-3 w-full text-white px-4 py-3 rounded text-base font-medium
bg-gradient-to-r from-green-400 to-blue-500 float-right shadow transition
duration-500 ease-in-out transform hover:-translate-y-1 hover:scale-100
shadow-2xl border-2 w-full p-4 rounded-lg">
zurück
</button>
</div>
</div>
#if(session()->has('message'))
<div class="alert alert-success">
{{ session()->get('message') }}
</div>
#endif
<script>
var vorname_modal = document.getElementById("change_vorname_modal");
var vorname_btn = document.getElementById("change_vorname");
var back_btn = document.getElementById("back");
vorname_btn.onclick = function () {
vorname_modal.style.display = "block";
}
back_btn.onclick = function () {
vorname_modal.style.display = "none";
}
window.onclick = function (event) {
if (event.target == modal) {
vorname_modal.style.display = "none";
}
}
</script>
The issue is when I have the required in the request validation I always get the error, when I delete the required everything works perfectly. I need it because a user should not be able to rename his name to an empty one.
You have a mistake on name of new name input. its not same as validators key
change your validator like this
$this->validate($request, [
'neuer_vorname' => 'required|max:255',
]);
it will work
Good afternoon, how to display post data by category in laravel livewire? here I want to try to display post by category data in the following way but it still doesn't work:
web.php
Route::get('category/{category:slug}',[FrontController::class, 'category'])->name('category');
FrontController.php
public $search;
public function category(Category $category)
{
$categories = Category::all();
$general = General::find(1);
$locale = App::currentLocale();
$category_id = $category->id;
$search = request("search");
$posts = Post::where([
['lang',$locale],
['category_id',$category_id],
['status','PUBLISH'],
])->latest()->paginate(12);
if ($this->search !== null) {
$posts = Post::where([
['title','like', '%' . $this->search . '%'],
['lang',$locale],
['category_id',$category_id],
['status','PUBLISH'],
])->latest()->paginate(12);
}
// dd($category_id);
$tags = Tag::all();
$top = Post::where('status','PUBLISH')->orderBy('views','desc')->limit(5)->get();
return view ('front.category',compact('categories','category_id','general','locale','posts','tags','top'));
}
category.blade.php
#extends('layouts.front')
#section('content')
<main id="main">
<section class="post-category">
<div class="container-fluid">
<div class="row mt-3">
<div class="col-lg-3 col-md-12 col-sm-12 d-none d-lg-block">
<div class="sticky-top" style="top: 90px;">
<div class="card mb-3 rounded-3">
<div class="card-body">
<a href="#" target="_blank" rel="noreferrer">
<img src="{{ asset('front/img/ads.png') }}" alt="..." height="300" width="279" class="card-img-top" />
</a>
</div>
</div>
<div class="d-flex flex-column mb-3 bg-light shadow bg-body rounded">
<div class="card-header bg-primary bg-gradient text-white fw-bold fs-5">
{{ __('sentence.category') }}
</div>
<ul class="list-group list-group-flush">
#foreach ($categories as $category)
<li class="list-group-item d-flex justify-content-between align-items-center">
{{ $category->name }}
</li>
#endforeach
</ul>
</div>
<div class="d-flex flex-column bg-light bg-body shadow-lg rounded-3">
<div class="card-header bg-primary bg-gradient text-white fw-bold fs-5">
Tags
</div>
<div class="p-3 overflow-auto" style="max-height: 42vh">
<div class="nav tag-cloud">
#foreach ($tags as $tag)
{{ $tag->name }}
#endforeach
</div>
</div>
</div>
</div>
</div>
<input type="hidden" name="category_id" value="{{ $category_id }}">
<livewire:category-index>
<div class="col-lg-3 col-md-12 col-sm-12">
<div class="sticky-top" style="top: 90px;">
<div class="card rounded-3 shadow-lg mb-3">
<div class="card-body">
<img src="{{ asset('front/img/ads1.png') }}" height="117" width="279" class="card-img-top" alt="...">
</div>
</div>
<div class="bg-light shadow bg-body rounded-3 mb-3">
<div class="card-header bg-primary bg-gradient text-white fw-bold fs-5">
{{ __('sentence.top_article') }}
</div>
<ul class="list-group list-group-flush mb-2">
#foreach ($top as $top)
<li class="list-group-item">
{{ $top->title }}
<div class="d-flex justify-content-between mt-3">
<small class="text-muted">{{ Carbon\Carbon::parse($top->created_at)->format("d F, Y") }}</small>
<small class="text-muted">{{ $top->views }} views </small>
</div>
</li>
#endforeach
</ul>
</div>
<div class="d-flex flex-column mb-3 bg-light shadow bg-body rounded d-lg-none d-xl-none">
<div class="card-header bg-primary bg-gradient text-white fw-bold fs-5">
{{ __('sentence.category') }}
</div>
<ul class="list-group list-group-flush">
#foreach ($categories as $category)
<li class="list-group-item d-flex justify-content-between align-items-center">
{{ $category->name }}
</li>
#endforeach
</ul>
</div>
<div class="d-flex flex-column bg-light bg-body shadow-lg rounded-3 d-lg-none d-xl-none">
<div class="card-header bg-primary bg-gradient text-white fw-bold fs-5">
{{ __('sentence.tag') }}
</div>
<div class="p-3 overflow-auto" style="max-height: 42vh">
<div class="nav tag-cloud">
#foreach ($tags as $tag)
{{ $tag->name }}
#endforeach
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
</main>
#endsection
#push('scripts')
#livewireScripts
<script type="text/javascript">
window.onscroll = function (ev) {
if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
window.livewire.emit('category-index');
}
};
</script>
<script>
document.getElementById('load-more').onclick = function() {
window.livewire.emit('category-index');
};
</script>
#endpush
livewire\CategoryIndex.php
<?php
namespace App\Http\Livewire;
use Livewire\Component;
use App\Models\{Category, Post};
use Illuminate\Support\Facades\App;
class CategoryIndex extends Component
{
public $limitPerPage = 10;
public $search;
protected $listeners = [
'category-index' => 'CategoryIndex'
];
protected $updatesQueryString = [
['search' => ['except' => '']],
];
public function CategoryIndex()
{
$this->limitPerPage = $this->limitPerPage + 6;
}
public function render()
{
$locale = App::currentLocale();
$category_id = request('category_id');
$posts = Post::where([
['lang',$locale],
['category_id',$category_id],
['status','PUBLISH'],
])->latest()->paginate($this->limitPerPage);
if ($this->search !== null) {
$posts = Post::where([
['title','like', '%' . $this->search . '%'],
['lang',$locale],
['category_id',$category_id],
['status','PUBLISH'],
])->latest()->paginate($this->limitPerPage);
}
$this->emit('postStore');
dd($category_id);
return view('livewire.category-index', ['posts' => $posts]);
}
}
livewire\category-index.blade.php
<div class="col-lg-6 col-md-12 col-sm-12">
<div id="section-title" class="section-title p-1 pt-3">
<h2 class="text-center fw-bold">{{ trans('sentence.recent_posts')}}</h2>
</div>
<div class="form-group has-search mb-3">
<span class="bi bi-search form-control-feedback"></span>
<input type="text" wire:model="search" class="form-control" placeholder="{{ __('sentence.search_form') }}">
</div>
#foreach ($posts as $data)
<div class="card bg-light shadow bg-body rounded-3 mb-2">
<div class="card-header bg-primary text-white d-flex justify-content-between">
<small>by {{$data->admin->name}}</small>
<small>{{ Carbon\Carbon::parse($data->created_at)->format("d F, Y") }}</small>
</div>
<div class="card-body">
<h2 class="card-title">
{{ $data->title }}
</h2>
<div class="card-footer bg-body d-flex justify-content-between align-items-center pb-0 px-0">
<div class="d-flex my-1">
#foreach ($data->tags as $tag)
{{ $tag->name }}
#endforeach
</div>
</div>
</div>
</div>
#endforeach
#if ($posts->count() == 0)
<div class="alert alert-danger" role="alert">
Data not found!
</div>
#endif
#if($posts->count() >= 10)
<div class="text-center d-md-none d-lg-none d-xl-none">
<button id="load-more" class="btn btn-primary my-3">
Load More
</button>
</div>
#endif
</div>
If I add($category_id); in FrontController.php the id of that category appears, but if I try to dd in livewire\CategoryIndex.php it shows null. what is the correct way to display posts by category? thank you
Livewire components work slightly differently to blade views, they do not automatically inherit variables from their parents.
What you need to do is pass your category_id to the Livewire component.
class CategoryIndex extends Component
{
public $categoryId;
}
<livewire:category-index :categoryId="$category_id" />
I am using a blade template and I was wanting to know within my foreach what the best way is for me to have it so that my first .panel-heading has another class added to it and then the second one has another etc
Example:
.red
.blue
.red
.blue
.red
Code:
#foreach ($dealsDB as $deal)
<div class="col-md-4">
<div class="panel panel-default">
<div class="panel-heading">
<h4><i class="fa fa-fw fa-gift"></i>{{ $deal->title }}</h4>
</div>
<div class="panel-body">
<p>{{ $deal->content }}</p>
</div>
</div>
</div>
#endforeach
try this
$k=0;
#foreach ($dealsDB as $deal)
<div class="col-md-4">
<div class="panel panel-default">
<div class="panel-heading #if($k%2==0) red #else blue #endif">
<h4><i class="fa fa-fw fa-gift"></i>{{ $deal->title }}</h4>
</div>
<div class="panel-body">
<p>{{ $deal->content }}</p>
</div>
</div>
</div>
$k++;
#endforeach
You can use #switch inside your #foreach to assign different class on every iteration.
#foreach (...)
<div class="
#switch($loop->iteration)
#case(1)
red
#break
#case(2)
green
#break
#case(3)
blue
#break
#default
yellow
#endswitch
">
#endforeach
In Laravel 5.8+, the $loop->odd or $loop->even variables can be used for this.
Furthermore, in Laravel 8.0 the #class directive was added. This is the perfect use case example for it.
In Laravel 5.3+ there is the $loop->iteration which returns the current loop iteration (starts at 1).
Docs:
https://laravel.com/docs/5.8/blade#the-loop-variable
https://laravel.com/docs/8.x/blade#conditional-classes
The following examples will return the exact same result.
Laravel 8+ version
#foreach ($dealsDB as $deal)
<div class="col-md-4">
<div #class([
'panel panel-default',
'red-class' => $loop->odd,
'blue-class' => $loop->even
])>
<div class="panel-heading">
<h4><i class="fa fa-fw fa-gift"></i>{{ $deal->title }}</h4>
</div>
<div class="panel-body">
<p>{{ $deal->content }}</p>
</div>
</div>
</div>
#endforeach
Laravel 5.8+ Version
#foreach ($dealsDB as $deal)
<div class="col-md-4">
<div class="panel panel-default
#if($loop->odd) red-class #else blue-class #endif">
<div class="panel-heading">
<h4><i class="fa fa-fw fa-gift"></i>{{ $deal->title }}</h4>
</div>
<div class="panel-body">
<p>{{ $deal->content }}</p>
</div>
</div>
</div>
#endforeach
Laravel 5.3+ Version
#foreach ($dealsDB as $deal)
<div class="col-md-4">
<div class="panel panel-default
#if($loop->iteration % 2 == 0) blue-class #else red-class #endif">
<div class="panel-heading">
<h4><i class="fa fa-fw fa-gift"></i>{{ $deal->title }}</h4>
</div>
<div class="panel-body">
<p>{{ $deal->content }}</p>
</div>
</div>
</div>
#endforeach