could someone help me with my code, when making a query of an existing table there are times that the invoices have several products, but my save button does not save the record that I want, oh how can I specify the record that I need when the "ID" is "INVOICE" then repeats several times, I have been told to use checkbox but I don't know how to implement it in this case, I hope you can help me
namespace App\Http\Livewire;
use Illuminate\Support\Facades\DB;
use App\Models\recepcionDes;
use App\Models\viewfacturaa;
use Livewire\Component;
class Descripcion extends Component
{
protected $listeners = ['openModalDes', 'editPedido','seleccionar'];
public $modal = false;
public $mensajePeido = false;
public $totalOCRestante;
public $codigo;
public $producto;
public $productoE = "";
public $pedido = null;
public $editMode = false;
public $recepcionDescripcion = [
'id' => null,
'des_id' => null,
'factura' => "",
'producto' => "",
'descripcion' => "",
'moneda' => "",
'tc' => null,
'precio_uni' => null,
'sub_total' => null,
'to_iva' => null,
'total' => null,
'diferencia' => null,
];
protected $rules = [
'recepcionDescripcion.des_id' => ['required', 'integer', 'max:120'],
'recepcionDescripcion.factura' => 'nullable',
'recepcionDescripcion.producto' => ['nullable', 'string'],
'recepcionDescripcion.descripcion' => ['nullable', 'string'],
'recepcionDescripcion.moneda' => ['nullable', 'string'],
'recepcionDescripcion.tc' => ['nullable'],
'recepcionDescripcion.precio_uni' => ['nullable'],
'recepcionDescripcion.sub_total' => ['nullable'],
'recepcionDescripcion.to_iva' => ['nullable'],
'recepcionDescripcion.total' => ['nullable'],
'recepcionDescripcion.diferencia' => ['nullable'],
];
public function render()
{
return view('livewire.descripcion');
}
public function eliminarPar($factura) {
recepcionDes::find($factura)->delete();
}
public function openModalDes($des_id)
{
$this->modal = true;
// $this->producto = $producto;
$this->des_id = $des_id;
$this->recepcionDescripcion["des_id"] = $des_id;
}
public function cerrarModalPedidos() {
if ($this->editMode) {
$this->editMode = false;
}
$this->modal = false;
$this->limpiarFormulario();
}
public function guardarPedido() {
$this->validate();
$pedidoG = recepcionDes::updateOrCreate(
['id' => $this->recepcionDescripcion["id"]],
$this->recepcionDescripcion
);
if ($pedidoG->id != null) {
session()->flash(
'message',
$this->recepcionDescripcion["id"] != null ? '¡Actualización exitosa!' : '¡Alta exitosa!'
);
if ($this->recepcionDescripcion["id"] != null) {
$this->limpiarFormulario();
$this->emit('render');
$this->editMode = false;
} else {
$this->cerrarModalPedidos();
}
}
}
public function seleccionar($fa, $pro, $des, $mo, $pre, $sub, $to ) {
$this->mensajePeido = false;
$this->recepcionDescripcion["factura"] = $fa;
$this->recepcionDescripcion["producto"] = $pro;
$this->recepcionDescripcion["descripcion"] = $des;
$this->recepcionDescripcion["moneda"] = $mo;
$this->recepcionDescripcion["precio_uni"] = $pre;
$this->recepcionDescripcion["sub_total"] = $sub;
$this->recepcionDescripcion["total"] = $to;
$this->pedido = null;
}
public function buscarPedido() {
$this->pedido = DB::connection('sqlsrv')
->select("select FACTURA, PRODUCTO, DESCRIPCION_FACTURA, MONEDA, TC, PRECIO_UNIDAD, SUBTOTAL, TOTAL_IVA from v_Vts_Factura_Ecabezado_Detalle where FACTURA = ? and PRODUCTO = ?", [intval($this->recepcionDescripcion['factura'])]);
if ($this->productoE != "") {
$this->pedido = DB::connection('sqlsrv')
->select("select PEDIDO, DENOMINACION_SOCIAL, CANTIDAD, FORMAT(FECHAENTREGA, 'yyyy-MM-dd') as FECHAENTREGA, NOMBRE_COMPLETO, PRODUCTO
from v_Vts_Factura_Ecabezado_Detalle where PEDIDO = ? and PRODUCTO = ?", [intval($this->orden_compra_pedido['factura']), $this->productoE]);
}
if ($this->pedido != null) {
$this->mensajePeido = false;
$this->recepcionDescripcion["factura"] = $this->pedido[0]->FACTURA;
$this->recepcionDescripcion["producto"] = $this->pedido[0]->PRODUCTO;
$this->recepcionDescripcion["descripcion"] = $this->pedido[0]->DESCRIPCION_FACTURA;
$this->recepcionDescripcion["moneda"] = $this->pedido[0]->MONEDA;
$this->recepcionDescripcion["tc"] = $this->pedido[0]->TC;
$this->recepcionDescripcion["precio_uni"] = $this->pedido[0]->PRECIO_UNIDAD;
$this->recepcionDescripcion["sub_total"] = $this->pedido[0]->SUBTOTAL;
$this->recepcionDescripcion["to_iva"] = $this->pedido[0]->TOTAL_IVA;
} else {
$this->mensajePeido = true;
}
}
public function sumar($pediosSum) {
$this->mensajePeido = false;
$this->recepcionDescripcion["factura"] = $pediosSum[0]["FACTURA"];
$this->recepcionDescripcion["producto"] = $pediosSum[0]["PRODUCTO"];
$this->recepcionDescripcion["descripcion"] = $pediosSum[0]["DESCRIPCION_FACTURA"];
$this->recepcionDescripcion["moneda"] = $pediosSum[0]["MONEDA"];
$this->recepcionDescripcion["precio_uni"] = $pediosSum[0]["PRECIO_UNIDAD"];
$this->recepcionDescripcion["sub_total"] = $pediosSum[0]["SUBTOTAL"];
// $this->recepcionDescripcion["total"]
$this->recepcionDescripcion["total"] = 0;
foreach ($pediosSum as $item) {
$this->recepcionDescripcion["total"] += $item["TOTAL_IVA"];
}
$this->pedido = null;
}
} ```
My view
```<div>
#if ($modal)
<div class="fixed z-10 inset-0 overflow-y-auto ease-out duration-400">
<div class="flex justify-center min-h-screen pt-4 px-4 pb-20 text-center sm:p-0">
<div class="fixed inset-0 transition-opacity">
<div class="absolute inset-0 bg-gray-500 opacity-75"></div>
</div>
<span class="hidden sm:inline-block sm:align-middle sm:h-screen"></span>
<div class="inline-block align-bottom bg-white rounded-lg text-left overflow-hidden shadow-xl transform transition-all sm:my-8 sm:align-middle sm:max-w-4xl sm:w-full"
role="dialog" aria-modal="true" aria-labelledby="modal-headline">
<div class="bg-white px-4 pt-5 pb-4 sm:p-6 sm:pb-4">
<div class="css-grid">
<div class="mb-4">
<label form="factura"
class="block text-gray-700 text-sm font-bold mb-2">Factura:</label>
<div class="inline-flex">
<input name="factura" type="text" placeholder="Ingrese factura"
class="form-control focus:outline-none focus:shadow-outline"
wire:model.lazy="recepcionDescripcion.factura" autocomplete="off">
<button wire:click="buscarPedido()" class="btn btn-primary">Buscar</button>
</div>
</div>
</div>
#error('recepcionDescripcion.factura')
<span class="error">{{ $message }}</span>
#enderror
#if ($pedido != null)
<table class="w-full">
<thead>
<tr>
<th class="border px-2 py-2 text-xs">Factura</th>
<th class="border px-2 py-2 text-xs">Producto</th>
<th class="border px-2 py-2 text-xs">Moneda</th>
<th class="border px-2 py-2 text-xs"></th>
</tr>
</thead>
<tbody>
#foreach ($pedido as $p)
<tr>
<td class="border px-2 py-2 text-xs">{{$p->FACTURA}}</td>
<td class="border px-2 py-2 text-xs">{{$p->PRODUCTO}}</td>
<td class="border px-2 py-2 text-xs">{{$p->MONEDA}}</td>
<td class="border px-2 py-2 text-xs text-center"><button class="btn btn-primary"
wire:click="seleccionar('{{$p->FACTURA}}', '{{$p->PRODUCTO}}', '{{$p->DESCRIPCION_FACTURA}}, '{{$p->MONEDA}}', '{{$p->PRECIO_UNIDAD}}, '{{$p->SUBTOTAL}}', '{{$p->TOTAL_IVA}})">Seleccionar</button>
<button wire:click="guardarPedido()"
class="inline-flex justify-center w-full rounded-md border border-transparent px-4 py-2 bg-green-600 text-base leading-6 font-medium text-white shadow-sm hover:bg-green-800 focus:outline-none focus:border-green-700 focus:shadow-outline-green transition ease-in-out duration-150 sm:text-sm sm:leading-5">Guardar</button>
</td>
</tr>
#endforeach
<tr>
<td class="border px-2 py-2 text-xs text-center" colspan="3">
<button class="btn btn-secondary" wire:click="sumar({{json_encode($pedido)}})">Sumar</button>
</td>
</tr>
</tbody>
</table>
#endif
#livewire('list-cambios', ["des_id" =>$recepcionDescripcion["des_id"]])
<label form="pedido" class="text-gray-700 text-sm font-bold">Factura:
{{ $recepcionDescripcion['factura'] }}</label><br>
<label form="pedido" class="text-gray-700 text-sm font-bold">Producto:
{{ $recepcionDescripcion['producto'] }}</label><br>
<label form="pedido" class="text-gray-700 text-sm font-bold">Moneda:
{{ $recepcionDescripcion['moneda'] }}</label><br>
<label form="pedido" class="text-gray-700 text-sm font-bold">Precio unitario:
{{ $recepcionDescripcion['precio_uni'] }}</label>
<div class="bg-gray-50 px-4 py-3 sm:px-6 sm:flex sm:flex-row-reverse">
<span
class="flex w-full rounded-md shadow-sm sm:ml-3 sm:w-auto">
<button wire:click="guardarPedido()"
class="inline-flex justify-center w-full rounded-md border border-transparent px-4 py-2 bg-green-600 text-base leading-6 font-medium text-white shadow-sm hover:bg-green-800 focus:outline-none focus:border-green-700 focus:shadow-outline-green transition ease-in-out duration-150 sm:text-sm sm:leading-5">Guardar</button>
</span>
<span class="flex w-full rounded-md shadow-sm sm:ml-3 sm:w-auto">
<button wire:click="cerrarModalDes()"
class="inline-flex justify-center w-full rounded-md border border-gray-300 px-4 py-2 bg-gray-200 text-base leading-6 font-medium text-gray-700 shadow-sm hover:text-gray-500 focus:outline-none focus:border-blue-300 focus:shadow-outline-blue transition ease-in-out duration-150 sm:text-sm sm:leading-5">Cerrar</button>
</span>
</div>
</div>
</div>
</div>
</div>
</div>
#endif
</div> ```
[enter image description here][1]
[1]: https://i.stack.imgur.com/5iB2P.jpg
Related
i have the same problem in this issue:
Laravel Livewire File Upload Not Validating and is returning a Livewire\TemporaryUploadedFile instance
i want upload my image path in database product, but i have some error
first in validate file image, missing field image or image required
same error in edit edit view,
but if i push second time the button il all ok and work
this is my controller:
public function store()
{
$data= $this->validate([
'name' => 'required',
// 'detail' => 'required',
//'giac' => 'required',
'classe' => 'required',
'photo' => 'required|image|mimes:jpg,jpeg,png,svg,gif|max:2024', // 1MB Max
]);
// $this->photo->store('documents');
$data['photo']= $this->photo->store('documents');
$filename = $this->photo->store('documents','public');
$data['photo'] = $filename;
$this->photo = $filename;
Product::updateOrCreate(['id' => $this->product_id], [
'name' => $this->name,
// 'detail' => $this->detail,
'giac' =>$this->giac,
'photo' =>$this->photo
]);
//$this->reset();
session()->flash('message',
$this->product_id ? 'Product Updated Successfully.' : 'Product Created Successfully.');
$this->closeModal();
$this->resetInputFields();
}
this is my blade file:
<div class="fixed z-10 inset-0 overflow-y-auto ease-out duration-400">
<div class="flex items-end justify-center min-h-screen pt-4 px-4 pb-20 text-center sm:block sm:p-0">
<div class="fixed inset-0 transition-opacity">
<div class="absolute inset-0 bg-gray-500 opacity-75"></div>
</div>
<!-- This element is to trick the browser into centering the modal contents. -->
<span class="hidden sm:inline-block sm:align-middle sm:h-screen"></span>?
<div class="inline-block align-bottom bg-white rounded-lg text-left overflow-hidden shadow-xl transform transition-all sm:my-8 sm:align-middle sm:max-w-lg sm:w-full" role="dialog" aria-modal="true" aria-labelledby="modal-headline">
<form>
<div class="bg-white px-4 pt-5 pb-4 sm:p-6 sm:pb-4">
<div class="">
<div class="mb-4">
<label for="exampleFormControlInput1" class="block text-gray-700 text-sm font-bold mb-2">Codice:</label>
<input type="text" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="exampleFormControlInput1" placeholder="Enter Name" wire:model="minsan">
#error('minsan') <span class="text-red-500">{{ $message }}</span>#enderror
</div>
<div class="mb-4">
<label for="exampleFormControlInput1" class="block text-gray-700 text-sm font-bold mb-2">Giacenza:</label>
<input type="text" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="exampleFormControlInput1" placeholder="Enter Name" wire:model="giac">
#error('giac') <span class="text-red-500">{{ $message }}</span>#enderror
</div>
<div class="mb-4">
<label for="exampleFormControlInput1" class="block text-gray-700 text-sm font-bold mb-2">Descrizione:</label>
<input type="text" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="exampleFormControlInput1" placeholder="Enter Name" wire:model="name">
#error('name') <span class="text-red-500">{{ $message }}</span>#enderror
</div>
<div class="mb-4">
<label for="exampleFormControlInput1" class="block text-gray-700 text-sm font-bold mb-2">Classe:</label>
<input type="text" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="exampleFormControlInput1" placeholder="Enter Name" wire:model="classe">
#error('classe') <span class="text-red-500">{{ $message }}</span>#enderror
</div>
<form wire:submit.prevent="uploadImage" enctype="multipart/form-data">
<input type="file" wire:model="photo">
#error('photo') <span class="text-red-500">{{ $message }}</span> #enderror
</form>
</div>
</div>
<div class="bg-gray-50 px-4 py-3 sm:px-6 sm:flex sm:flex-row-reverse">
<span class="flex w-full rounded-md shadow-sm sm:ml-3 sm:w-auto">
<button wire:click.prevent="store()" type="button" class="inline-flex justify-center w-full rounded-md border border-transparent px-4 py-2 bg-green-600 text-base leading-6 font-medium text-white shadow-sm hover:bg-green-500 focus:outline-none focus:border-green-700 focus:shadow-outline-green transition ease-in-out duration-150 sm:text-sm sm:leading-5">
Salva
</button>
</span>
<span class="mt-3 flex w-full rounded-md shadow-sm sm:mt-0 sm:w-auto">
<button wire:click="closeModal()" type="button" class="inline-flex justify-center w-full rounded-md border border-gray-300 px-4 py-2 bg-white text-base leading-6 font-medium text-gray-700 shadow-sm hover:text-gray-500 focus:outline-none focus:border-blue-300 focus:shadow-outline-blue transition ease-in-out duration-150 sm:text-sm sm:leading-5">
Cancel
</button>
</span>
</form>
</div>
</div>
</div>
</div>
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Livewire\Crud;
use App\Http\Livewire\Products;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/products', Products::class)->name('products.index');
Route::post('/products', Products::class)->name('products.index');
Want to ask, why is it that when I input data from a table, only 1 data appears in MySQL from the shared table input?
Here is a picture of the table on the website. And there are 3 student data for input. but later only 1 will appear which is given a red line in the image below, 3 data should appear in mysql.
But after inputting data from the table, only 1 data appears in MySQL, 3 data should appear in MySQL.
Blade
<div class="overflow-x-auto relative shadow-md sm:rounded-lg">
<form method="POST" action="{{route('add.teacher.attendance.detail')}}" enctype="multipart/form-data">
#csrf
<table class="w-full text-sm text-left text-gray-500 dark:text-gray-400">
<thead class="text-xs text-white uppercase bg-[#464867] dark:bg-[#464867]">
<tr>
<th scope="col" class="py-3 px-6">
NISN
</th>
<th scope="col" class="py-3 px-6">
Name
</th>
<th scope="col" class="py-3 px-6">
Attendance
</th>
<th scope="col" class="py-3 px-6">
Note
</th>
</tr>
</thead>
<tbody>
#foreach($dataAttendanceTa as $data)
<tr class="bg-white border-b dark:bg-gray-900 dark:border-gray-700">
<th scope="row" class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
{{$data->username}}
<input
type="hidden"
name="id_atte"
id="id_atte"
value="{{$data->id_atte}}"
>
<input
type="hidden"
name="id_student"
id="id_student"
value="{{$data->id}}"
>
</th>
<td class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
{{$data->name}}
</td>
<td class="flex py-4 px-6 w-10 font-medium text-gray-900 whitespace-nowrap dark:text-white">
<form class="flex">
<div class="flex items-center mb-4 ml-3">
<input id="present" type="radio" name="id_atte_type" value="2" class="w-4 h-4 border-gray-300 focus:ring-2 focus:ring-blue-300 dark:focus:ring-blue-600 dark:focus:bg-blue-600 dark:bg-gray-700 dark:border-gray-600" checked>
<label for="present" class="block ml-2 text-sm font-medium text-gray-900 dark:text-gray-300">
Present
</label>
</div>
<div class="flex items-center mb-4 ml-3">
<input id="sick" type="radio" name="id_atte_type" value="1" class="w-4 h-4 border-gray-300 focus:ring-2 focus:ring-blue-300 dark:focus:ring-blue-600 dark:focus:bg-blue-600 dark:bg-gray-700 dark:border-gray-600">
<label for="sick" class="block ml-2 text-sm font-medium text-gray-900 dark:text-gray-300">
Sick
</label>
</div>
<div class="flex items-center mb-4 ml-3">
<input id="absent" type="radio" name="id_atte_type" value="3" class="w-4 h-4 border-gray-300 focus:ring-2 focus:ring-blue-300 dark:focus:ring-blue-600 dark:bg-gray-700 dark:border-gray-600">
<label for="absent" class="block ml-2 text-sm font-medium text-gray-900 dark:text-gray-300">
Absent
</label>
</div>
</form>
</td>
<td class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
<input
type="text"
name="note"
id="note"
autocomplete="note"
value="{{ old('note') }}"
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-600 dark:border-gray-500 dark:placeholder-gray-400 dark:text-white"
>
</td>
</tr>
#endforeach
</tbody>
</table>
<input type="submit" class="mt-3 mb-3 focus:outline-none text-white bg-[#464867] hover:bg-[#464867] font-medium rounded-lg text-sm px-5 py-2.5 mb-2 " value="Done" >
</form>
</div>
Controllers
public function ViewAttendance($id){
$dataAttendance = Attendance::findOrFail($id);
$dataAttendanceTa = Attendance::join('subjects', 'attendances.id_subject', '=', 'subjects.id_sub')
->join('class_infos', 'subjects.id_class', '=', 'class_infos.id')
->join('class_details', 'class_infos.id', '=', 'class_details.id_class')
->join('users', 'class_details.id_user', '=', 'users.id')
->where('attendances.id_atte', '=', $id)
->get();
return view('teacher.attendance.data_attendance_detail', compact( 'dataAttendance', 'dataAttendanceTa'));
}
public function AddAttendanceDetail(Request $request) {
$addAttendanceDetail = new AttendanceDetail();
$request->validate([
'id_atte' => ['required'],
'id_student' => ['required'],
'id_atte_type' => ['required'],
]);
$addAttendanceDetail->id_atte = $request->input('id_atte');
$addAttendanceDetail->id_student = $request->input('id_student');
$addAttendanceDetail->id_atte_type = $request->input('id_atte_type');
$addAttendanceDetail->note = $request->input('note');
$addAttendanceDetail->save();
return redirect('teacher/data-attendance');
}
So for Controllers I join with other table data.
Model
class AttendanceDetail extends Model
{
use HasFactory;
protected $primaryKey = 'id_atte_detail';
protected $table = 'attendance_details';
protected $fillable = [
'id_atte_detail',
'id_atte',
'id_student',
'id_atte_type',
'note',
];
}
What is the solution so that when you click the DONE button, all the data in MySQL appears? and is there something wrong in my coding or in mysql?
I'm still new to Laravel but this might be able to help you since, as you press "DONE" you are saving data by multiple student ID's
Instead of doing it once like
$addAttendanceDetail->id_atte = $request->input('id_atte');
$addAttendanceDetail->id_student = $request->input('id_student');
$addAttendanceDetail->id_atte_type = $request->input('id_atte_type');
$addAttendanceDetail->note = $request->input('note');
$addAttendanceDetail->save();
You should use foreach
$data = $request->all();
$finalArray = [];
foreach ($data as $key => $value) {
array_push(
$finalArray,
[
'id_atte' => $value['id_atte'],
'id_student' => $value['id_student'],
'id_atte_type' => $value['id_atte_type'],
'note' => $value['note']
]
);
});
AttendanceDetail::insert($finalArray);
Still, not really sure, but might be helpful
/* i have one balde that i use for uploading the images "createEvent.blade.php" then i try to retrive it in 2 baldes "indexEvent.blade.php" and "events.blade.php" the images stored in "/storage/app/public/img" and i run "php artisan storage:link
" every thing is fine in my localhost the imges uploaded and displayed fine, but when i upload my website to the server it still uploade the imge but not displying it. i try to figur out that but nothing work. any one can help. */
the controller i use to store the imges and updated
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;
use App\Models\Event;
use App\Models\EventAttendies;
use URL;
use Session;
use Redirect;
use Input;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\EventAttendiesExport;
class EventController extends Controller
{
//admin saide
public function index()
{
$data['events'] = Event::orderBy('id','desc')->paginate(20);
return view('dashboard.admin.indexEvent', $data);
}
public function create()
{
return view('dashboard.admin.createEvent');
}
public function store(Request $request)
{
$request->validate([
'title' => 'required',
'description' => 'required',
'image' => 'required|image|mimes:jpg,png,jpeg,gif,svg|max:2048',
'date' => 'required',
'place' => 'required',
'event_type' => 'required',
'status' => 'required',
]);
$path = $request->file('image')->store('public/img');
$event = new Event;
$event->title = $request->title;
$event->description = $request->description;
$event->date = $request->date;
$event->place = $request->place;
$event->event_type = $request->event_type;
$event->image = $path;
$event->status = $request->status;
$event->save();
return redirect()->route('events.index')
->with('success','Event has been created successfully.');
}
public function show(Event $event)
{
return view('dashboard.admin.indexEvent',compact('event'));
}
public function edit(Event $event)
{
return view('dashboard.admin.eventEdit',compact('event'));
}
public function update(Request $request, $id)
{
$request->validate([
'title' => 'required',
'description' => 'required',
'date' => 'required',
'place' => 'required',
'event_type' => 'required',
'status' => 'required',
]);
$event = Event::find($id);
if($request->hasFile('image')){
$request->validate([
'image' => 'required|image|mimes:jpg,png,jpeg,gif,svg|max:2048',
]);
$path = $request->file('image')->store('public/img');
$event->image = $path;
}
$event->title = $request->title;
$event->description = $request->description;
$event->date = $request->date;
$event->place = $request->place;
$event->event_type = $request->event_type;
$event->status = $request->status;
$event->save();
return redirect()->route('events.index')
->with('success','Event updated successfully');
}
// Event Show "events.blade.php" user side
public function event()
{
$data['events'] = Event::orderBy('id','desc')->where('status','1')->get();
return view('events', $data);
}
}
here my from from admin side to uploade the images
<form action="{{ route('events.store') }}" method="POST" enctype="multipart/form-data">
#csrf
<div class="overflow-hidden sm:rounded-md">
<div class="px-4 py-5 sm:p-2">
<div class="grid grid-cols-6 gap-6">
<div class="col-span-6 sm:col-span-6">
<label for="title" class="block text-sm font-medium text-gray-700">Event Title</label>
<input type="text" name="title" id="title" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm sm:text-sm">
</div>
<div class="col-span-6 sm:col-span-6">
<label for="description" class="block text-sm font-medium text-gray-700">Event Description</label>
<input type="text" name="description" id="description" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm sm:text-sm">
</div>
<div class="col-span-6">
<label for="place" class="block text-sm font-medium text-gray-700">Event Place</label>
<input type="text" name="place" id="place" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm sm:text-sm">
</div>
<div class="col-span-6 sm:col-span-4">
<label for="date" class="block text-sm font-medium text-gray-700">Event Date</label>
<div class="flex absolute inset-y-0 left-0 items-center pl-3 pointer-events-none">
</div>
<input datepicker="" datepicker-autohide="" name="date" id="date" type="text" class=" border border-gray-300 text-gray-900 sm:text-sm rounded-lg block w-full pl-10 p-2.5 datepicker-input" placeholder="Select date">
</div>
<input class="hidden" name="event_type" value="Free" id="event_type">
<!--- <div class="col-span-6 sm:col-span-3">
<label for="event_type" class="block text-sm font-medium text-gray-700">Event Type</label>
<select id="event_type" name="event_type" class="mt-1 block w-full rounded-md border border-gray-300 bg-white py-2 px-3 shadow-sm focus:outline-none sm:text-sm">
<option value="free">Free</option>
<option value="paid">Paid</option>
</select>
</div> -->
<div class="col-span-6 sm:col-span-3">
<label for="status" class="block text-sm font-medium text-gray-700">Event Status</label>
<select id="status" name="status" class="mt-1 block w-full rounded-md border border-gray-300 bg-white py-2 px-3 shadow-sm focus:outline-none sm:text-sm">
<option value="1">Active</option>
<option value="0">Inactive</option>
</select>
</div>
<div class="col-span-6 sm:col-span-4">
<label for="date" class="block text-sm font-medium text-gray-700">Event Photo</label>
<input type="file" name="image" class="form-control" placeholder="Event Title">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="bg-gray-50 px-4 py-3 sm:flex sm:flex-row-reverse sm:px-6">
<button type="submit" class="inline-flex w-full justify-center rounded-md border border-transparent bg-red-600 px-4 py-2 text-base font-medium text-white shadow-sm hover:bg-red-700 focus:outline-none focus:ring-2 focus:ring-red-500 focus:ring-offset-2 sm:ml-3 sm:w-auto sm:text-sm">Add</button>
<button type="button" class="mt-3 inline-flex w-full justify-center rounded-md border border-gray-300 bg-white px-4 py-2 text-base font-medium text-gray-700 shadow-sm hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 sm:mt-0 sm:ml-3 sm:w-auto sm:text-sm" id="close-Addmodal">Cancel</button>
</form>
here i retrive the image in admin side
#foreach ($events as $event)
<tr class="border-b border-gray-200 hover:bg-gray-100">
<td class="py-3 px-6 text-left whitespace-nowrap">
<div class="flex items-center">
<span class="font-medium">{{ $event->title }}</span>
</div>
</td>
<td class="py-3 px-6 text-left">
<div class="flex items-center">
<span>{{ $event->description }}</span>
</div>
</td>
<td class="py-3 px-6 text-center">
<div class="flex items-center justify-center">
<span class="font-medium">{{ $event->date }}</span>
</div>
</td>
<td class="py-3 px-6 text-center">
<div class="flex items-center justify-center">
<img class="w-6 h-6 rounded-full border-gray-200 border -m-1 transform hover:scale-125" src="{{ Storage::url($event->image) }}" alt="{{ $event->place }}"/>
</div>
</td>
<td class="py-3 px-6 text-center">
#if(($event->status)==1)
<span class="bg-purple-200 text-purple-600 py-1 px-3 rounded-full text-xs">Active</span>
#else
<span class="bg-red-200 text-red-600 py-1 px-3 rounded-full text-xs">Pending</span>
#endif
<!--<span class="bg-green-200 text-green-600 py-1 px-3 rounded-full text-xs">Completed</span>-->
here i retrive the imge in user side
#foreach ($events as $event)
<div class="w-full #if($events->count()>=2) md:w-1/2 #endif px-4">
<div class="bg-white shadow-lg rounded-lg overflow-hidden mb-10">
<img class="eventsPic w-full" style="background-image:url({{ Storage::url($event->image) }});"/>
<div class="p-8 sm:p-9 md:p-7 xl:p-9 text-center">
<h3>
<a href="javascript:void(0)"
class=" font-semibold text-dark text-xl sm:text-[22px] md:text-xl lg:text-[22px] xl:text-xl 2xl:text-[22px] mb-2 block hover:text-primary " >
{{ $event->title }}
</a>
</h3>
<p class="text-base text-body-color leading-relaxed">
{{ $event->description }}
</p>
<p>{{ ucfirst($event->event_type) }}</p>
<p>{{ $event->place }}</p>
<p>{{ $event->date }}</p>
<a id="Regis" data-event-id="{{$event->id}}" data-title="{{ $event->title }}" data-event-type="{{ $event->event_type }}"
href="javascript:void(0)"
class="register-btn inline-block py-2 px-12 border rounded-full text-base text-body-color font-medium hover:border-red-700 hover:bg-red-700 hover:text-white transition">
{{__('sniperTrans.Register')}}
</a>
</div>
</div>
</div>
#endforeach
the problem was I deployed it via GitHub and i do not give the permission to write.
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
#What I want to do
I want to make a chats system using Laravel,Pusher,ajax.
If the login user is not the item user, a message is sent correctly.
But if I want to send a message to others on the item that made by the login user,
the message will be sent to login user.
I want to know how to fix this issue.But I have no idea...
Route
Route::get('/chats/{receive}/send/{itemId}', [ChatsController::class, 'sendChat'])->name('chats.users');
Route::post('chat/send' , [ChatsController::class, 'store'])->name('chatSend');
blade
<div class="px-4 bg-white text-right sm:px-6">
<a href="{{ route('chats.users',['receive' => $item->user->id ,'itemId'=> $item->id ] ) }}"
type="submit" >チャット </a></div>
Controller
public function sendChat(Request $request, $receive ,$itemId )
{
$loginId = Auth::id();
$param = [
'send' => $loginId,
'receive' => $receive,
];
$query = Message::where('send' , $loginId)->where('receive' , $receive);
$query->orWhere(function($query) use($loginId , $receive){
$query->where('send' , $receive);
$query->where('receive' , $loginId);
});
$messages = $query->get();
$user = User::where('id', $param['receive'])->get();
$item = Item::findOrFail($itemId);
return view('chats.sendChat' , compact('param' , 'messages','user','item'));
}
/**
* Save message
*/
public function store(Request $request)
{
$insertParam = [
'send' => $request->input('send'),
'receive' => $request->input('receive'),
'message' => $request->input('message'),
'user_id' => Auth::user()->id,
'item_id' => $request->input('item_id')
];
try{
Message::insert($insertParam);
}catch (\Exception $e){
return false;
}
return true;
}
messages table
$table->id();
$table->foreignId('user_id')->constrained();
$table->foreignId('item_id')->constrained();
$table->bigInteger('send')->comment('送信者');
$table->bigInteger('receive')->comment('受信者');
$table->text('message');
$table->tinyInteger('is_read')->default(0);
$table->timestamps();
chats.sendChat.blade.php
#extends('layouts.app')
#section('content')
<div class="mt-4">
<div class="m-auto px-4 py-5 mt-2 max-w-xl bg-white rounded-lg " >
<div class="row">
<div class="col-start-2 col-span-4 ">
</div>
</div>
<div id="room" class="justify-items-stretch">
<h1 class="text-lg mb-2">
<a class="no-underline hover:underline text-black" href="{{ route('users.items',[$user[0]->id]) }}">
{{ $user[0]->name }}</a>
{{ $item->title }}
</h1>
<hr>
#foreach($messages as $message)
{{-- Send message --}}
#if($message->send == \Illuminate\Support\Facades\Auth::id())
<div class="flex items-end justify-end">
<div class="send mr-2 bg-green-300 mx-1 my-1 px-1 rounded-lg" style="text-align: right">
<p>{{$message->message}}</p>
</div>
</div>
<p class="text-xs " style="text-align: right">{{ $message->user->name }}</p>
#endif
{{-- received message --}}
#if($message->receive == \Illuminate\Support\Facades\Auth::id())
<div class="flex items-start justify-start">
<div class="receive mr-2 bg-gray-300 mx-1 my-1 px-1 rounded-lg" style="text-align: left">
<p >{{$message->message}}</p>
</div>
</div>
<p class="text-xs ">{{ $message->user->name }}</p>
#endif
#endforeach
</div>
<form >
#csrf
<textarea name="message" style="width:100%" class="mt-1 p-1 focus:outline-none focus:ring focus:border-blue-300 block w-full shadow-sm sm:text-sm border border-indigo-600 rounded-xl"></textarea>
<button type="button" id="btn_send" class="inline-flex justify-center py-2 px-8 mt-1 border border-transparent shadow-sm text-sm font-medium rounded-xl text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 " style="float: right" >
<svg
class="w-4 h-4 transform rotate-45 -mt-px"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M12 19l9 2-9-18-9 18 9-2zm0 0v-8"
></path>
</svg></button>
</form>
<input type="hidden" name="send" value="{{$param['send']}}">
<input type="hidden" name="receive" value="{{$param['receive']}}">
<input type="hidden" name="login" value="{{\Illuminate\Support\Facades\Auth::id()}}">
<input type="hidden" name="item_id" value="{{$item->id}}">
</div>
</div>
#endsection
javascript
<script type="text/javascript">
var pusher = new Pusher('', {
cluster : '',
encrypted: true
});
var pusherChannel = pusher.subscribe('chat');
pusherChannel.bind('chat_event', function(data) {
let appendText;
let login = $('input[name="login"]').val();
if(data.send === login){
appendText = '<div class="flex items-end justify-end"> <div class="send bg-green-300 mx-1 my-1 p-1 rounded-lg " style="text-align:right"><p>' + data.message + '</p></div></div> ';
}else if(data.receive === login){
appendText = '<div class="receive bg-gray-300 w-3/4 mx-4 my-2 p-2 rounded-lg clearfix" style="text-align:left"><p>' + data.message + '</p></div> ';
}else{
return false;
}
$("#room").append(appendText);
if(data.receive === login){
Push.create("新着メッセージ",
{
body: data.message,
timeout: 8000,
onClick: function () {
window.focus();
this.close();
}
})
}
});
$.ajaxSetup({
headers : {
'X-CSRF-TOKEN' : $('meta[name="csrf-token"]').attr('content'),
}});
// メッセージ送信
$('#btn_send').on('click' , function(){
$.ajax({
type : 'POST',
url : '/chat/send',
data : {
message : $('textarea[name="message"]').val(),
send : $('input[name="send"]').val(),
receive : $('input[name="receive"]').val(),
item_id : $('input[name="item_id"]').val(),
}
}).done(function(result){
$('textarea[name="message"]').val('');
}).fail(function(result){
});
});
</script>