Vaneath's avatar

Laravel route request()->fullUrl

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!!

0 likes
3 replies
kokoshneta's avatar

If you’re expecting the tag and category fields to be available inside the body of the request that you’re posting to your items.store route, then you need to make sure that they’re actually being set on the page where the data for the new Item object is being prepared (presumably your items.create route). You’ve shown us a lot of code, but not that page, which is the actual relevant bit (all the restaurant resource stuff isn’t relevant here, as far as I can tell – and your “current URL” is for the restaurant stuff, not the items page which is where you say the problem is).

In the form where you enter the data to use to create the new item, you have to make sure that there are fields called tag and category. If there aren’t, they won’t be part of the request to the store route.

1 like
Vaneath's avatar

@kokoshneta thanks, but tag and category is not part of the form, it is the query from the url and I want to get the value of that query. In this case http://vue-dmenu.test/restaurants/Jn87fwrtZj?tag=oLin77gUPs in restaurants.show when I use request('tag') or request()->input('tag') or request()->query('tag') it returns the value of tag(oLin77gUPs) but when I do it in items.store, it returns null. And when I request()->fullUrl in restaurants.show it returns my full url http://vue-dmenu.test/restaurants/Jn87fwrtZj?tag=oLin77gUPs. But it returns http://vue-dmenu.test/items in items.store, even current url is http://vue-dmenu.test/restaurants/Jn87fwrtZj?tag=oLin77gUPs

kokoshneta's avatar

@Vaneath I don’t understand what you mean at all. If your URL is .../restaurants/xyz?tag=123, then you’re not in your items.store route – you’re in one of the restaurant.xyz routes.

Going to any endpoint starting in /restaurants/ will land you in the Restaurant controller.

So what is your URL when you access the items.store route?

Please or to participate in this conversation.