My current uri: http://vue-dmenu.test/restaurants/Jn87fwrtZj?tag=oLin77gUPs
web.php file:
Route::resource('restaurants', RestaurantController::class);
Route::post('/items', [ItemController::class, 'store'])->name('items.store');
This is my Restaurant Controller:
public function index()
{
$restaurants = Restaurant::where('user_id', Auth::user()->id)->get()->sortByDesc('created_at');
return Inertia::render('Restaurant/Index', [
'restaurants' => $restaurants,
]);
}
public function create()
{
return Inertia::render('Restaurant/Index', [
'show' => true,
]);
}
public function store(Request $request)
{
$attributes = request()->validate([
'name' => 'required',
'table_amount' => 'required|numeric',
'bg_img_url' => 'required|image',
'logo_url' => 'required|image',
'khqr_url' => 'required|image',
'home_address' => 'required',
'street_address' => 'required',
'commune' => 'required',
'district' => 'required',
]);
$attributes['id'] = Str::random(10);
$attributes['user_id'] = Auth::user()->id;
$attributes['qr_code_url'] = 'https://chart.googleapis.com/chart?chs=300x300&cht=qr&choe=UTF-8&chl=' . $_SERVER['APP_URL'] . '/restaurants/' . $request->name;
$attributes['bg_img_url'] = $request->file('bg_img_url')->store('restaurants/' . $request->name . '/background');
$attributes['logo_url'] = $request->file('logo_url')->store('restaurants/' . $request->name. '/logo');
$attributes['khqr_url'] = $request->file('khqr_url')->store('restaurants/' . $request->name . '/khqr');
Restaurant::create($attributes);
}
public function show(Restaurant $restaurant)
{
$tags = $restaurant->tags;
$categories = Category::where('tag_id', request('tag'))->get();
$items = Item::where('category_id', request('category'))->get();
return Inertia::render('Category/Index', [
'restaurant' => $restaurant,
'tags' => $tags,
'categories' => $categories,
'items' => $items,
'showItem' => request()->has('category')
]);
}
This is my ItemController:
public function store()
{
dd(request('tag')); //return null
$attributes = request()->validate([
'name' => 'required',
'price' => 'required|numeric',
'description' => 'required',
'item_img_url' => 'required|image',
'is_available' => 'required|boolean',
'category_id' => 'required',
]);
$attributes['id'] = Str::random(10);
$attributes['item_img_url'] = request()->file('item_img_url')->store('items/' . $request->name);
Item::create($attributes);
}
In restaurants.show when I use request('tag') or request('category') it returns the value of the query. But in items.store it returns null. And when I use request()->fullUrl in items.store it returns http://vue-dmenu.test/items, which is different from my current url. I can figure out a little bit that maybe because of route conflict or something that the query only return the value in restaurant route but not in item, because I am using restaurant route now. Since I am still new to laravel is there a way to fix this, or is there a way to get the current uri instead of http://vue-dmenu.test/items when use request()->fullUrl in item controller?
Thanks!!