1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

Having strange problem changing Boolean in Laravel

Discussão em 'Outras Linguagens' iniciado por Tiran100, Outubro 17, 2024 às 16:23.

  1. Tiran100

    Tiran100 Guest

    So having this strange problem in laravel. I am trying to update a boolean value in my database but i keep getting this strange error in my log:

    Missing required parameter for [Route: change.stock] [URI: product/stock/{product}/{turn}] [Missing parameter: turn]

    Anybody maybe knows what i am over seeing here.

    My route:

    Route::get('/product/stock/{product}/{turn}', [ProductController::class, 'changeStock'])->name('change.stock');


    My Controller:

    <?php

    namespace App\Http\Controllers;

    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\Gate;
    use App\Http\Requests\Product\{ImageRequest,OfferRequest,DeliveryRequest,InformationsRequest};
    use App\Models\{Product,Image,Offer,Delivery,Category,Favorite};
    use Illuminate\Support\Str;

    class ProductController extends Controller
    {
    /**
    * Change product stock setting
    *
    * @param $turn
    * @return \Illuminate\Http\RedirectResponse
    *
    */
    public function changeStock($turn): \Illuminate\Http\RedirectResponse
    {
    try{
    $this -> setStock($turn);
    session() -> flash('success', 'You have changed your product stock setting');
    }
    catch (RequestException $e){
    session() -> flash('errormessage', $e -> getMessage());
    }
    return redirect() -> back();

    }


    My User.php Model:

    <?php

    namespace App\Models;

    use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Illuminate\Foundation\Auth\User as Authenticatable;
    use Illuminate\Notifications\Notifiable;
    use Laravel\Sanctum\HasApiTokens;
    use Carbon\Carbon;
    use App\Traits\UUIDs;
    use App\Models\Conversation;

    class User extends Authenticatable
    {
    use HasApiTokens, HasFactory, Notifiable, UUIDs;

    /**
    * Sets the product stock status
    *
    * @param $turn
    * @throws RequestException
    */
    public function setStock($turn)
    {
    // sets stock value
    $this -> in_stock = $turn == 0;
    $this -> save();

    }



    The blade file:

    <div class="btn-group btn-group-sm w-100 my-1 mb-4" role="group" aria-label="Basic example">
    <a href="{{ route('change.stock', 1) }}" class="btn @if
    ($product -> in_stock == 1)
    btn-success btn-sm @else btn-outline-success btn-sm @endif">In Stock</a>
    <a href="{{ route('change.stock', 0) }}" class="btn @if
    ($product -> in_stock == 0)
    btn-danger btn-sm @else btn-outline-danger btn-sm @endif">Set Out of Stock</a>
    </div>

    Continue reading...

Compartilhe esta Página