Laravel 登録機能・検索機能がうまくいかない
解決したいこと
laravelで商品管理ツールを作成しているのですが、登録機能、検索機能がうまく働きません。
エラーなども出ず、どこがおかしいのか把握できていない状況です。
発生している問題・エラー
・検索機能 検索フォームに値を入力して検索ボタンを押しても反映されない。
該当するソースコード
ソースコードを入力
views/product.blade.php
@extends('layouts.app')
@section('content')
<div class="search">
<form action="{{ route('product') }}" method="GET">
@csrf
<!-- 検索フォーム -->
<div class="product_name.search">
<label for="product_name">{{ __('商品名') }}</label>
<input type="text" name="keyword">
</div>
<div class="company_name.serch">
<label for="company_id">{{ __('メーカー') }}<span class="badge badge-danger ml-2">{{ __('必須') }}</span></label>
@foreach ($companies as $company)
<select class="form-control" name="company_id" id="company_id" value="{{ old('company_id')}}">
<option name="keyword">{{ $company->company_name }}</option>
</select>
@endforeach
</div>
<input type="submit" value="検索">
</form>
</div>
<h1>商品一覧</h1>
<table class="table table-striped">
<thead>
<tr>
<th></th>
<th>商品名</th>
<th>メーカー</th>
<th>価格</th>
<th>在庫数</th>
<th>商品説明</th>
<th>画像</th>
<th>作成日</th>
<th>更新日</th>
<th>詳細</th>
<th>削除</th>
</tr>
</thead>
<tbody>
@foreach ($products as $product)
<tr>
<td>{{ $product->id }}</td>
<td>{{ $product->product_name }}</td>
@foreach ($companies as $company)
<td>{{ $company->company_name }}</td>
@endforeach
<td>{{ $product->price }}</td>
<td>{{ $product->stock }}</td>
<td>{{ $product->comment }}</td>
<td>{{ $product->img_path }}</td>
<td>{{ $product->created_at }}</td>
<td>{{ $product->updated_at }}</td>
<td><a href="{{ route('product.detail', ['id'=>$product->id]) }}" class="btn btn-primary">詳細</a></td>
<td>
<form action="{{ route('product.destroy', ['id'=>$product->id]) }}" method="POST">
@csrf
<button type="submit" class="btn btn-danger">削除</button>
</form>
</td>
</tr>
@endforeach
<div>
</div>
<button type="button" onclick="location.href='{{ route('product_form') }}'"> 商品登録 </button>
</tbody>
</table>
@endsection
views/product_form.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<h1>商品登録画面</h1>
<form action="{{ route('submit') }}" method="POST">
@csrf
<div class="form-group">
<label for="product_name">{{ __('商品名') }}<span class="badge badge-danger ml-2">{{ __('必須') }}</span></label>
<input type="text" class="form-control" name="product_name" id="product_name" value="{{ old('product_name')}}">
@if($errors->has('product_name'))
<p>{{ $errors->first('product_name') }}</p>
@endif
</div>
<div class="form-group">
<label for="company_id">{{ __('メーカー') }}<span class="badge badge-danger ml-2">{{ __('必須') }}</span></label>
<select class="form-control" name="company_id" id="company_id" value="{{ old('company_id')}}">
@foreach ($companies as $company)
<option value='{{$company->id}}'>{{ $company->company_name }}</option>
@endforeach
</select>
@if($errors->has('company_id'))
<p>{{ $errors->first('company_id') }}</p>
@endif
</div>
<div class="form-group">
<label for="price">{{ __('価格') }}<span class="badge badge-danger ml-2">{{ __('必須') }}</span></label>
<input type="text" class="form-control" name="price" id="price" value="{{ old('price')}}">
@if($errors->has('price'))
<p>{{ $errors->first('price') }}</p>
@endif
</div>
<div class="form-group">
<label for="stock">{{ __('在庫数') }}<span class="badge badge-danger ml-2">{{ __('必須') }}</span></label>
<input type="text" class="form-control" name="stock" id="stock" value="{{ old('stock')}}">
@if($errors->has('stock'))
<p>{{ $errors->first('stock') }}</p>
@endif
</div>
<div class="form-group">
<label for="comment">{{ __('商品説明') }}<span class="badge badge-danger ml-2">{{ __('必須') }}</span></label>
<textarea class="form-control" name="comment" id="comment" value="{{ old('comment')}}"></textarea>
@if($errors->has('comment'))
<p>{{ $errors->first('comment') }}</p>
@endif
</div>
<button type="submit">登録</button>
</form>
<button type="button" onclick="location.href='{{ route('product') }}'">戻る</button>
</div>
@endsection
Controllers/Productcontroller.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Product;
use App\Models\Company;
use App\Http\Requests\ProductRequest;
use Illuminate\Support\Facades\DB;
class Productcontroller extends Controller
{
// public function company() {
// $model = new Product();
// $products = $model->getCompanyNameById();
// }
public function showList(Request $request)
{
// 商品一覧画面表示
$product = new Product();
$company = new Company();
$products = $product->getList();
$companies = $company->getListcompany();
return view('product', compact('products','companies'));
}
public function search(Request $request){
// 検索機能
$keyword = $request->input('keyword');
$product = new Product();
$company = new Company();
$products = $product->SearchList($keyword);
return view('product', compact('products'));
}
public function create(){
// 登録フォーム
$model = new Product();
$companies = $model ->getCompanyNameById();
return view('product_form', compact('companies'));
}
public function exeCreate(ProductRequest $request){
// 登録処理
DB::beginTransaction();
try {
$model = new Product();
$model->registProduct($request);
} catch (\Exception $e) {
DB::rollback();
return back();
}
return redirect(route('product'));
}
public function delete($id)
{
// 削除機能
$product = Product::find($id);
$product->delete();
return redirect()->route('product');
}
public function detail($id)
{
// 詳細画面表示
$product = Product::find($id);
$company = new Company();
$companies = $company->getCompanyNameById();
return view('detail', compact('product','companies'));
}
public function edit($id)
{
// 編集処理
$company = new Company();
$product = Product::find($id);
$companies = $company->getCompanyNameById($product->company_id);
return view('edit', compact('product','companies'));
}
public function update(ProductRequest $request, $id)
{
// 更新処理
$product = Product::find($id);
$product->updateProduct($request, $product);
$companies = $product ->getCompanyNameById();
return redirect()->route('product', compact('product','companies'));
}
}
Models/Product.php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class Product extends Model
{
use HasFactory;
protected $table = 'products';
protected $primaryKey = 'id';
protected $fillable = [
'product_name',
'price',
'stock',
'comment',
'img_path',
'company_id',
'created_at',
'updated_at'
];
public function getCompanyNameById() {
$products= DB::table('products')
->join('companies', 'products.company_id', '=', 'companies.id')
->get();
return $products;
}
public function getList(){
$products = DB::table('products')
->get();
return $products;
}
public function SearchList($keyword){
// 検索処理
// { $results = DB::table('products')
// ->join('companies', 'products.company_id', '=', 'companies.id')
// ->select('products.company_id', 'products.product_name')
// ->where('products.product_name', 'LIKE', "%{$keyword}%")
// ->orWhere('companies.company_name', 'LIKE', "%{$keyword}%")
// ->paginate(10)
// ->get();
// }
$products=DB::table('products')
->join('companies','company_id','=','companies.id')
->select('products.*','companies.campanies_name')
->where('products.product_name', 'LIKE', "%$keyword%")
->orwhere('companies.company_name', 'LIKE', "%$keyword%")
->get();
return $products;
}
public function registProduct($data) {
// 登録処理
DB::table('products') ->insert([
'product_name' => $data->product_name,
'price' => $data->price,
'stock' => $data->stock,
'comment' => $data->comment,
'img_path' => $data->img_path,
'company_id' => $data->company_id,
'created_at' => NOW(),
'updated_at' => NOW(),
]);
}
public function updateProduct($request, $product)
{
// 更新処理
$result = $product->fill([
'product_name' => $request->product_name,
'price' => $request->price,
'stock' => $request->stock,
'comment' => $request->comment,
'img_path' => $request->img_path,
'updated_at' => NOW(),
'company_id' => $request->company_id
])->save();
return $result;
}
}
Models/Company.php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class Company extends Model
{
use HasFactory;
protected $table = 'companies';
protected $primaryKey = 'id';
protected $fillable = [
'company_name',
'street_address',
'representative',
'created_at',
'updated_at'
];
public function getCompanyNameById(){
$companies = DB::table('companies')->get();
return $companies;
}
public function getListcompany(){
$companies = DB::table('companies')->get();
return $companies;
}
}
自分で試したこと
今回二つのテーブルを使うので、結合をして表示させたりしましたが、うまくいきません。
学習2ヶ月目で質問内容も抽象的でわかりずらいとは思いますが、ご教授いただけると幸いです。
よろしくお願いいたします。