My live search using livewire is not working - php

I am doing a live search on livewire, my search term is already binded with the livewire controller, but when I add it to the where clause in render function, it still displays all the data on the table not the one i am searching. I tried replacing the searchterm with a string as keyword in LIKE and it works. It seems that i can't use the input in searchterm as a keyword in the where clause.
here is my controller;
<?php
namespace App\Http\Livewire;
use Livewire\Component;
use App\Models\Item;
use Livewire\WithPagination;
class Items extends Component
{
use WithPagination;
public $searchTerm;
public function render()
{
$select = Item::where('itemName', 'LIKE', '%' . $this->searchTerm . '%')->paginate(6);
return view('livewire.items', ['items' => $select]);
}
}
this is my view
<div class="row browse mt-6">
<div class="col float-left category ">
<h4 class="float-left font-weight-bold">All Categories</h4>
<h4 class="float-left font-weight-bold">Men</h4>
<h4 class="float-left font-weight-bold">Women</h4>
</div>
<div class="col search-cat float-right">
<input type="text" id="search" placeholder="Browse Products" wire:model="searchTerm"/>
</div>
{{$searchTerm}}
</div>
<hr style="border-top: dotted 1px;" />
<div class="row w-100 p-1">
<table class="table-center">
<tbody>
<div>
#foreach($items as $item)
<tr class="mx-auto">
<div class="d-inline w-30 item-desc mx-auto m-1">
<h4 class="pt-1">{{ $item->itemName }}</h4>
<div class="">
<img src="items/{{ $item->itemPic }}.png" alt="" class="item-pic">
</div>
<div class="row">
<div class="col d-inline float-left">
Price: US ${{ $item->itemPrice }}
</div>
<div class="col d-inline float-right">
{{ $item->itemCOD }}</td>
</div>
</div>
</div>
</tr>
</tbody>
#endforeach
</div>
<hr style="border-top: dotted 1px;" />
<div class="pagination-content">
{{ $items->links('pagination::pagination-custom') }}
</div>
<hr style="border-top: dotted 1px;" />
</table>
</div>

There's two things here that could potentially cause problems,
Your tr element does not have wire:key, the value to wire:key must be unique across the entire page
Your view consists of multiple root elements (if this is your entire view), these are the three root-elements in your view - and in Livewire, its very important that the view only has one root element.
<div class="row browse mt-6">
<hr style="border-top: dotted 1px;" />
<div class="row w-100 p-1">
Also, as a sidenote, your loop also closes the tbody tag, which is odd, so I moved that out of the loop. It also doesn't quite make sense to put divs and hrs inside the table tag, those should probably be outside of the tabletag.
Here's the updated view, with wire:key on the tr and with the whole view wrapped in one div,
<div>
<div class="row browse mt-6">
<div class="col float-left category ">
<h4 class="float-left font-weight-bold">All Categories</h4>
<h4 class="float-left font-weight-bold">Men</h4>
<h4 class="float-left font-weight-bold">Women</h4>
</div>
<div class="col search-cat float-right">
<input type="text" id="search" placeholder="Browse Products" wire:model="searchTerm"/>
</div>
{{$searchTerm}}
</div>
<hr style="border-top: dotted 1px;" />
<div class="row w-100 p-1">
<table class="table-center">
<tbody>
<div>
#foreach($items as $item)
<tr class="mx-auto" wire:key="item-{{ $item->id }}">
<div class="d-inline w-30 item-desc mx-auto m-1">
<h4 class="pt-1">{{ $item->itemName }}</h4>
<div class="">
<img src="items/{{ $item->itemPic }}.png" alt="" class="item-pic">
</div>
<div class="row">
<div class="col d-inline float-left">
Price: US ${{ $item->itemPrice }}
</div>
<div class="col d-inline float-right">
{{ $item->itemCOD }}</td>
</div>
</div>
</div>
</tr>
#endforeach
</div>
</tbody>
<hr style="border-top: dotted 1px;" />
<div class="pagination-content">
{{ $items->links('pagination::pagination-custom') }}
</div>
<hr style="border-top: dotted 1px;" />
</table>
</div>
</div>
Have a look at this resource from the official Livewire docs,
https://laravel-livewire.com/docs/2.x/troubleshooting#dom-diffing-issues

Maybe because you not render component
Try add <livewire:name-component /> in parent view like this
<div class>
<livewire:name-component />
</div>

Related

Form keeps reloading page after pressing Submit in Laravel 8

The caption explains it best. When I press the Submit button - the form should redirect to the Controller method "store" and execute the logic written. But instead, it just reloads the page. How do I know that the Controller isn't accessed? I have tried using dd() in the store() method and it never pops up.
Following is the HTML/PHP:
#extends('admin.layout')
#section('content')
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<link rel="stylesheet" href="{{url('js/datatables/dataTables.bootstrap4.css')}}">
<div class="intro-y col-span-12 mt-4">
<!-- BEGIN: Vertical Form -->
<div class="intro-y box">
<div class="flex flex-col sm:flex-row items-center p-5 border-b border-slate-200/60 dark:border-darkmode-400">
<h2 class="font-medium text-base mr-auto">
Course Cover
</h2>
</div>
<form action="{{ route('course.store') }}" method="post" enctype="multipart/form-data">
#csrf
#method('POST')
<div id="vertical-form" class="p-5">
<div class="preview">
<div class="avatar-upload" style="margin: 0 !important; max-width: 485px;">
<div class="avatar-edit" style="top:-20px !important; /* position: relative !important; */">
<input type='file' name="cover" id="imageUpload" accept=".png, .jpg, .jpeg" />
<label for="imageUpload"><i data-lucide="pen-tool" style="color: #232d45 !important;padding: 3px;left: 4px;top: 4px;position: relative;"></i></label>
</div>
<div id="imagePreview" style="background-size: cover; background-position: center; box-shadow: rgb(0 0 0 / 10%) 0px 2px 4px 0px; border: 1px solid rgb(141 146 157); margin: 0; border-radius: 11px; width: 452.66px !important; height: 224px; background-image: url(https://www.ysm.ca/wp-content/uploads/2020/02/default-avatar.jpg);">
</div>
</div>
</div>
</div>
</div>
</div>
<div class="intro-y col-span-12 mt-4">
<!-- BEGIN: Vertical Form -->
<div class="intro-y box">
<div class="flex flex-col sm:flex-row items-center p-5 border-b border-slate-200/60 dark:border-darkmode-400">
<h2 class="font-medium text-base mr-auto">
Course Details
</h2>
</div>
<div id="vertical-form" class="p-5">
<div class="grid grid-cols-12 gap-2 mt-4 mb-4">
<label for="vertical-form-1" class="form-label col-span-6">Course Title</label>
<label for="vertical-form-1" class="form-label col-span-6">Course Difficulty</label>
<input type="text" name="name" placeholder="Name.." class="form-control col-span-6" placeholder="Input inline 1" aria-label="default input inline 1">
<select name="difficulty" class="form-select form-select col-span-6" aria-label="Select Course Difficulty">
<option value="Beginner">Beginner</option>
<option value="Intermediate">Intermediate</option>
<option value="Expert">Expert</option>
</select>
</div>
<div class="mt-4 mb-4">
<label for="vertical-form-1" class="form-label">Categories</label>
<select name="categories[]" data-placeholder="Choose appropriate categories for this course.." class="tom-select w-full" multiple>
#foreach ($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
</select>
</div>
</div>
</div>
</div>
<div class="intro-y col-span-12 mt-4">
<!-- BEGIN: Vertical Form -->
<div class="intro-y box">
<div class="flex flex-col sm:flex-row items-center p-5 border-b border-slate-200/60 dark:border-darkmode-400">
<h2 class="font-medium text-base mr-auto">
Course Description
</h2>
</div>
<div id="vertical-form" class="p-5">
<div class="mt-4 mb-4">
<textarea class="dark:border-darkmode-400"" cols="80" id="editor2" name="description" rows="10" data-sample-short>Type a detailed description of the course i.e its prerequisites, requirements, learning outcome and any other details that may be needed.</textarea>
</div>
</div>
</div>
</div>
<div class="intro-y col-span-12 mt-4">
<!-- BEGIN: Vertical Form -->
<div class="intro-y box">
<div class="flex flex-col sm:flex-row items-center p-5 border-b border-slate-200/60 dark:border-darkmode-400">
<h2 class="font-medium text-base mr-auto">
Choose Instructor
</h2>
</div>
<div id="vertical-form" class="p-5">
<table class="table table-responsive table-striped table-vcenter js-dataTable-full">
<thead>
<tr>
<th class="text-center">PICK</th>
<th>ID</th>
<th>AVATAR</th>
<th class="d-none d-sm-table-cell">NAME</th>
<th class="d-none d-sm-table-cell" style="width: 15%;">EMAIL</th>
<th class="text-center" style="width: 15%;">Profile</th>
</tr>
</thead>
<tbody>
#foreach ($teachers as $teacher)
<tr>
<td class="w-10">
<input class="form-check-input" type="radio" name="teacher_id" value="{{ $teacher->id }}">
</td>
<td class="w-10">{{ $teacher->id }}</td>
<td class="w-20">
<div class="w-10 h-10 image-fit zoom-in ml-2">
<img class="rounded-full" src="{{ url('images/'.$teacher->avatar) }}" alt="">
</div>
</td>
<td class="d-none d-sm-table-cell">{{ $teacher->name }}</td>
<td class="d-none d-sm-table-cell">
<span class="badge badge-danger">{{ $teacher->email }}</span>
</td>
<td class="text-center">
<button type="button" class="btn btn-sm btn-secondary" data-toggle="tooltip" title="View Customer">
<i class="fa fa-user"></i>
</button>
</td>
</tr>
#endforeach
</tbody>
</table>
<button type="submit" class="btn btn-primary mt-5">Create Course</button>
</form>
</div>
</div>
</div>
<script src="https://cdn.ckeditor.com/4.19.0/standard-all/ckeditor.js"></script>
<script defer src="https://use.fontawesome.com/releases/v5.15.4/js/all.js" integrity="sha384-rOA1PnstxnOBLzCLMcre8ybwbTmemjzdNlILg8O7z1lUkLXozs4DHonlDtnE7fpc" crossorigin="anonymous"></script>
<script>
CKEDITOR.replace('editor2', {
height: 260,
/* Default CKEditor 4 styles are included as well to avoid copying default styles. */
contentsCss: [
'http://cdn.ckeditor.com/4.19.0/full-all/contents.css',
'https://ckeditor.com/docs/ckeditor4/4.19.0/examples/assets/css/classic.css'
],
removeButtons: 'PasteFromWord',
});
</script>
<script>
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$('#imagePreview').css('background-image', 'url('+e.target.result +')');
$('#imagePreview').hide();
$('#imagePreview').fadeIn(650);
}
reader.readAsDataURL(input.files[0]);
}
}
$("#imageUpload").change(function() {
readURL(this);
});
</script>
<script src="{{url('js/datatables/jquery.dataTables.min.js')}}"></script>
<script src="{{url('js/datatables/dataTables.bootstrap4.min.js')}}"></script>
<script src="{{url('js/be_tables_datatables.min.js')}}"></script>
#endsection
What am I doing wrong here? Why is the form not taking me to course.store method in the CourseController? And the controller is a resource controller. So the routing is very straight-forward, and not an issue in this case either.
I think you need to check your closing div tags, you must not close a div that is the parent of the form before closing the form tag,
May be you Forgot to remove Last Div tage and also you should write at last #endsection
#extends('admin.layout')
#section('content')
//here you can write your form as well as other HTML
#endsection
I had the same issue, in my case the problem was registered the same Route
Route::post('/users', [UserController::class, 'store']);
Route::post('/users', [UserController::class, 'storeDashboard']);
I wanted to fire store method but storeDashboard was firing instead

Images I upload in Laravel don't show after I have tried many answers online. All the images I upload store alright but they don't display

I have this in view. It is able to upload alright but it won't show. Is there anything more I should add?
<div class="col-md-4">
<img class="img img-fluid img-thumbnail" src="/storage/profile_images/{{$profile->profile_image}}"/> </div>
here is my show.blade.php file
<div class="col-md-4">
<img class="img img-fluid img-thumbnail" src="/storage/profile_images/{{$profile->profile_image}}"/></div>
Here is the player.blade.php file:
#if(count($players) > 0)
<?php $index = 0; ?>
#foreach($players as $player)
<div class="col-md-4">
<div class="card mb-1" style="width:300px">
<div class="card-body">
<img class="img img-fluid img-thumbnail" style="height: 170px; width: 350px" src="/storage/profile_images/{{$player->profile_image}}">
</div>
<div class="card-footer">
<div class="row justify-content-center">
{{$names[$index]}}<br>
</div>
<div class="row">
<div class="col-10">
<span class="h6">Team: {{$player->current_fc}}</span><br>
<span class="h6">Position: {{$player->position}}</span><br>
</div>
</div>
</div>
</div>
</div>
<?php $index++; ?>
#endforeach
<div class="col-12 mt-3">
{{$players->appends(request()->input())->links()}}
</div>
#else
<p>No Players available</p>
#endif
Follow above your image issue will be resolved:
Step 1:
Delete storage folder from public
Step 2:
Inside your project run this command
player.blade.php
php artisan storage:link
Step 3:
Replace or change as per this:
show.blade.php
<div class="col-md-4">
<img class="img img-fluid img-thumbnail" src="{{ url('storage/profile_images/.$profile->profile_image) }}" /></div>
Step 4:
Replace or change as per you required:
#if(count($players) > 0)
<?php $index = 0; ?>
#foreach($players as $player)
<div class="col-md-4">
<div class="card mb-1" style="width:300px">
<div class="card-body">
<img class="img img-fluid img-thumbnail" style="height: 170px; width: 350px" src="{{ url('storage/profile_images/.$profile->profile_image) }}">
</div>
<div class="card-footer">
<div class="row justify-content-center">
{{$names[$index]}}<br>
</div>
<div class="row">
<div class="col-10">
<span class="h6">Team: {{$player->current_fc}}</span><br>
<span class="h6">Position: {{$player->position}}</span><br>
</div>
</div>
</div>
</div>
</div>
<?php $index++; ?>
#endforeach
<div class="col-12 mt-3">
{{$players->appends(request()->input())->links()}}
</div>
#else
<p>No Players available</p>
#endif
You can use the asset function like in the example below:
#php
$imgUrl = '/storage/profile_images/'.$profile->profile_image;
#endphp
<img class="img img-fluid img-thumbnail" src="{{ asset($imgUrl)}}"/>
Also make sure that the storage is linked using the following command:
php artisan storage:link

Update loop records via Laravel and livewire

Hi, I would like some help on this.
This loop comes from my products table, using pure laravel I load each product in inputs, so if I make any changes and click on an update button it is redirected to a route to update the changed product.
How could I do this same idea using livewire?
How to display the updated data in this loop without having to go to another route.
My blade:
#forelse($categoryProduct->products as $key => $product)
<li>
<div class="mt-3 card">
<div class="card-body">
<div class="row">
<div class="col-1">
<x-inputs.group class="">
<div
x-data="imageViewer('{{ $product->img ? \Storage::url($product->img) : '' }}')">
<x-inputs.partials.label name="img" label="Foto">
</x-inputs.partials.label><br />
<!-- Show the image -->
<template x-if="imageUrl">
<img :src="imageUrl"
class="object-cover border border-gray-200 rounded"
style="width: 100px; height: 100px;" />
</template>
<!-- Show the gray box when image is not available -->
<template x-if="!imageUrl">
<div class="bg-gray-100 border border-gray-200 rounded"
style="width: 140px; height: 140px;"></div>
</template>
<div class="mt-2">
<input wire:model="img" type="file" name="img" id="img"
#change="fileChosen" />
</div>
#error('img')
#include('components.inputs.partials.error')
#enderror
</div>
</x-inputs.group>
</div>
<div class="col-11">
<div class="row">
<div class="col-10">
<input type="text" class="form-control" placeholder="Name"
value="{{ $product->name }}">
</div>
<div class="col-2">
<button
onclick="confirm('Tem certeza que deseja deletar esse produto?') || event.stopImmediatePropagation()"
wire:click.prevent="deleteProduct({{ $product->id }})"
class="btn btn-user btn-danger">
×
</button>
</div>
<div class="mt-3 col-10">
<textarea class="form-control"
placeholder="Description">{{ $product->description }}</textarea>
</div>
<div class="mt-3 col-2">
<input type="number" class="form-control"
placeholder="Price" value="{{ $product->price }}">
</div>
</div>
</div>
</div>
</div>
</li>
#empty
This is the way I'm feeding the loop:
public function render()
{
$this->shop = Shop::with([
'categoryProducts',
'categoryProducts.products',
'districts'
])->where('user_id', Auth::user()->id)->first();
return view('app.menu-controller')
->extends('layouts.app')
->section('content');
}
You can create a Livewire ProductComponent and in blade have the div card once this loop the same elements
https://www.laravel-livewire.com/docs/2.x/making-components
https://www.laravel-livewire.com/docs/2.x/rendering-components
<div>
<div class="mt-3 card">
<div class="card-body">
<div class="row">
<div class="col-1">
<x-inputs.group class="">
<div
x-data="imageViewer('{{ $product->img ? \Storage::url($product->img) : '' }}')">
<x-inputs.partials.label name="img" label="Foto">
</x-inputs.partials.label><br />
//......................
</div>
</div>
Use model binding in nested component for the loop
https://www.laravel-livewire.com/docs/2.x/nesting-components
#forelse($categoryProduct->products as $key => $product)
<li>
#livewire('product-component',['product' => $product], key($user->id))
</li>
and in component
public Product $product;
public function render()
{
return view('livewire.product-component')
->extends('layouts.app')
->section('content');
}
The rest in in Livewire documentation, when you get on there could come here with issues or questions. Greetings

change the value of an amount

In an html page, I would like to recover the value of an amount and can change the value only in my admin panel.
In the file navbar.blade.php I have 250 which is the amount to edit for the example.
<div class="header-widgets hidden-xs" style="padding:0px;padding-top: 60px;">
<div id="text-3" class="widget widget_text">
<div class="textwidget">
<div class="info-icon">
<img src="/img/time.png">
<span style="font-size: 22px;color: #0d3863;font-weight: bold;"> 250</span>
</div>
</div>
</div>
</div>
I just seek to edit the amount (250) ...
In my Controller named tarrifcontroller I have that.
public function edit(Tariff $tariff)
{
return view('admin.tariffs.edit', compact('tariff'));
}
public function update(Request $request, Tariff $tariff)
{
$tariff->valeur = strip_tags($request->input('amount'));
$tariff->save();
return redirect('/tariffs');
}
In my edit.blade.php I have that
#section('content')
<div class="px-content">
<div class="page-header">
<div class="row">
<div class="col-md-4 text-xs-center text-md-left text-nowrap">
<h1><i class="px-nav-icon ion-android-apps"></i>Tarif {{$tariff->id}} </h1>
</div>
<hr class="page-wide-block visible-xs visible-sm">
<!-- Spacer -->
<div class="m-b-2 visible-xs visible-sm clearfix"></div>
</div>
</div>
<div class="row">
<div class="panel">
<div class="panel-body">
<div class="table-responsive">
<form class="panel-body" action="/tariff/edit/{{$tariff->id}}" method="POST">
#csrf
<fieldset class="form-group">
<label for="form-group-input-1">Amount</label>
<input type="text" name="amount" class="form-control" id="form-group-input-1" value="{{$tariff->amount}}">
</fieldset>
<button type="submit" class="btn btn-primary pull-right">MAJ</button>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
My problem is now in navbar.blade.php how should I do that 250 can interact with my edit / update function?
<span style="font-size: 22px;color: #0d3863;font-weight: bold;"> 250</span>
Thank you
I don't have experience with laravel but i guess like any other php framework you can echo variable from controller to view
<span style="font-size: 22px;color: #0d3863;font-weight: bold;"> <?php echo $variable ?></span>
after some googling: You can pass data to the view using the with method.
return View::make('blog')->with('posts', $posts);

How do I loop through grid properly?

I'm currently working on a photo gallery index.
How can I make it so that my loop iterates all 4 columns and then proceeds making a new row?
This is my code:
<div class="row">
#foreach($photoGalleries as $photoGallery)
<div class="col-md-3">
<a href="{{ url('/photogalleries/' . $photoGallery->id) }}">
<img class="img-fluid" style="height: 200px; width: 200px;" src="/images/{{ $photoGallery->cover_image }}">
</a>
<h3>{{ $photoGallery->name }}</h3>
</div>
#endforeach
</div>
Now as you can imagine, it'll appear like this:
Alternative solution:
Add margin-top after the col-md-3 attribute, like so:
<div class="col-md-3" style="margin-top: 20px;">
</div>
$collectionOne = $photoGalleries->chunk(4);
#foreach($collectionOne as $collectionTwo)
<div class "row" >
#foreach($collectionTwo as $photoGallery)
<div class="col-md-3">
......
</div>
#endforeah
</div>
#endforeach

Categories