我之前的搜索方法在我实现了计算方法后运行得很好
**我收到错误未定义变量$公式
C:\xampp\htdocs\MedusaDashboard 资源 iews\superadmin\search.blade.php :103
这是我的控制器**
`<?php
namespace App\Http\Controllers;
use App\Models\DetailFormula;
use App\Models\Formula;
use App\Models\Produk;
use Illuminate\Http\Request;
class SearchController extends Controller
{
public function search(Request $request)
{
$query = $request->input('query');
$formulas = Formula::whereHas('warna', function($q) use ($query){
$q->where('kode_warna', 'like', "%$query%");
})
->orWhere('varian', 'like', "%$query%")
->orWhereHas('user', function($q) use ($query) {
$q->where('name', 'like', "%$query%");
})
->orWhereHas('warna', function($q) use ($query) {
$q->where('nama_warna', 'like', "%$query%");
})
->orWhereHas('mobil', function($q) use ($query) {
$q->where('merek', 'like', "%$query%");
})
->get();
return view('superadmin.search', compact('formulas'));
}
public function Detailformula($id)
{
// Mencari detail formula berdasarkan idformula yang cocok dengan $id
$detailFormula = DetailFormula::where('idformula', $id)->get();
// Memeriksa apakah detail formula ditemukan
if (!$detailFormula) {
abort(404, 'Detail formula not found');
}
// Tampilkan halaman formulaSA.blade.php dengan data yang diperlukan
return view('superadmin.formulaSA', compact('detailFormula'));
}
public function calculate(Request $request)
{
$amount = $request->input('amount');
$unit = $request->input('unit');
$idFormula = $request->input('idFormula'); // Ensure this input is passed from the form
// Ambil data detail formula berdasarkan idFormula
$detailFormula = DetailFormula::where('idformula', $idFormula)->get();
if ($unit == 'gram') {
$totalJumlah = $detailFormula->sum('jumlahdlmgr');
$x = $amount / $totalJumlah;
foreach ($detailFormula as $detail) {
$detail->jumlahdlmgr = $detail->jumlahdlmgr * $x;
}
} elseif ($unit == 'liter') {
$totalVolume = 0;
foreach ($detailFormula as $detail) {
$produk = Produk::where('kode_produk', $detail->kode_produk)->first();
if ($produk) {
$volume = ($detail->jumlahdlmgr / 1000) / $produk->massa_jenis;
$totalVolume += $volume;
}
}
$x = $amount / $totalVolume;
foreach ($detailFormula as $detail) {
$produk = Produk::where('kode_produk', $detail->kode_produk)->first();
if ($produk) {
$detail->jumlahdlmgr = $detail->jumlahdlmgr * $x;
}
}
}
// Tampilkan halaman formulaSA.blade.php dengan data yang diperbarui
return view('superadmin.formulaSA', compact('detailFormula'));
}
}`
这是我的搜索。刀片
`@if(request()->has('query') && !empty(request()->input('query')))
<h2>Search Results for Formulas</h2>
<br></br>
@if ($formulas->isEmpty())
<p>No formulas found.</p>
@else
<div class="row gy-4" id="search-results">
@foreach($formulas as $formula)
<a class="search-item" href="{{ route('formula.open', ['id' => $formula->id]) }}">
<!--<p>Pembuat: {{ $formula->user->name }}</p>-->
<p>{{ $formula->warna->kode_warna }} / {{ $formula->warna->nama_warna }}</p>
<p>{{ $formula->mobil->merek }} {{ $formula->mobil->model }}</p>
<p>{{ $formula->varian }}</p>
</a>
@endforeach
</div>
@endif
@endif`
formulaSA.blade(实现计算方法)
`<table class="styled-table">
<thead>
<tr>
<th>Komponen</th>
<th>Jumlah (gram)</th>
</tr>
</thead>
<tbody>
@foreach($detailFormula as $detail)
<tr>
<td>{{ $detail->produk->kode_produk }} ({{ optional($detail->coat)->jeniscoat }})</td>
<td>{{ $detail->jumlahdlmgr }} gr</td>
</tr>
@endforeach
</tbody>
</table>
<!-- Form untuk melakukan perhitungan -->
<form method="POST" action="{{ route('calculate') }}">
@csrf
<input type="hidden" name="idFormula" value="{{ $detailFormula->first()->idformula }}">
<div class="input-group">
<label for="amount">Masukkan jumlah yang diinginkan</label>
<input type="number" id="amount" name="amount" placeholder="Masukkan jumlah" required>
<select id="unit" name="unit" required>
<option value="" disabled selected>Pilih satuan</option>
<option value="liter">Liter</option>
<option value="gram">Gram</option>
</select>
<button type="submit">Hitung</button>
</div>
</form>`
我尝试更改变量并再次检查变量,我认为这只是一个小拼写错误,我希望方法搜索和计算可以一起运行
解决了兄弟感谢您查看并回答我的问题
错误出现在我的 web.php 和控制器中
这是新的路线和控制器
Route::get('/formula', [SearchController::class, 'showSearchForm'])->name('formula.search');
Route::get('/formula/search', [SearchController::class, 'search'])->name('formula.search.results');
Route::get('/formula/{id}', [SearchController::class, 'handleFormula'])->name('formula.open');
类 SearchController 扩展了 Controller { 公共函数搜索(请求$request) { $query = $request->input('query');
// Simpan query dalam session
$request->session()->put('search_query', $query);
// Lakukan pencarian seperti sebelumnya
$formulas = Formula::whereHas('warna', function($q) use ($query) {
$q->where('kode_warna', 'like', "%$query%");
})
->orWhere('varian', 'like', "%$query%")
->orWhereHas('user', function($q) use ($query) {
$q->where('name', 'like', "%$query%");
})
->orWhereHas('warna', function($q) use ($query) {
$q->where('nama_warna', 'like', "%$query%");
})
->orWhereHas('mobil', function($q) use ($query) {
$q->where('merek', 'like', "%$query%");
})
->get();
return view('superadmin.search', compact('formulas', 'query'));
}
public function showSearchForm(Request $request)
{
// Ambil query pencarian dari session
$query = $request->session()->get('search_query', '');
// Tampilkan halaman pencarian dengan query dari session
return view('superadmin.search', compact('query'));
}
我只是添加新路线和新方法