2

I would like to create a form in Laravel (only) with 3 fields: title, price, quantity , the name of the page is named 'create.blade.php'. enter image description here

My first problem is that when I enter the 3 values, nothing happening! The page create.blade.php is stuck ! I don't have error message

enter image description here

my second problem is to get the amount total on my page 'index.blade.php'

In my table products I have this:

public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('title');
            $table->integer('quantity');
            $table->double('price');
            $table->double('total')->nullable();
            $table->timestamps();
        });
    }

In my productController I have this:

public function index()
    {
        $products = Product::oldest()->paginate(5);
        return view('admin.products.index', compact('products'))
                  ->with('i', (request()->input('page', 1)-1)*5);
    }


    public function create()
    {

        $products = Product::all();
        return view('admin.products.create', compact('products'));
    }


    public function store(Request $request)
    {
        $request->validate([
                'title' => 'required',
                'quantity' => 'required',
                'price' => 'required',
                'total' => 'required'
        ]);

        Product::create($request->all());

        return redirect()->route('products.index')
                    ->with('success', 'save');
    }

In my Model Product

 protected  $fillable = ['title', 'quantity', 'price', 'total']; 

    public function setTotalAttribute()
    {
        $this->attributes['total'] = $this->quantity * $this->price;
    }

    public function getTotalAttribute($value)
    {
        return $value;
    }

And in my create.blade.php I have this:

 <form class="panel-body" action="{{route('products.store')}}" method="POST" novalidate>
              @csrf
              <fieldset class="form-group {{ $errors->has('title') ? 'has-error' : '' }}">
                <label for="form-group-input-1">Title</label>
                <input type="text" name="title" id="title" class="form-control" value="{{ old('title')}}"/>
                {!! $errors->first('title', '<span class="help-block">:message</span>') !!}
              </fieldset>

              <fieldset class="form-group {{ $errors->has('quantity') ? 'has-error' : '' }}">
                <label for="form-group-input-1">Quantity</label>
                <input type="text" name="quantity" id="quantity" class="form-control" value="{{ old('quantity')}}"/>
                {!! $errors->first('quantity', '<span class="help-block">:message</span>') !!}
              </fieldset>

              <fieldset class="form-group {{ $errors->has('price') ? 'has-error' : '' }}">
                <label for="form-group-input-1">Price</label>
                <input type="text" name="price" id="price" class="form-control" value="{{ old('price')}}"/>
                {!! $errors->first('price', '<span class="help-block">:message</span>') !!}
              </fieldset>

              <a href="{{route('products.index')}}" class="btn btn-primary pull-right">Back</a>
              <button type="submit" class="btn btn-sm btn-primary">OK</button>

            </form>

Route

Route::resource('/products', 'ProductController');

File index.blade.php

@foreach($products as $product)
                <tr>
                   <td> {{$product->title}}</td>
                   <td> {{$product->quantity}}</td>
                   <td> {{$product->price}}</td>
                   <td> {{$product->total}}</td>
                   <td>
                     <form method="POST" action="{{ route('products.destroy', $product) }} ">
                    <a class="btn btn-sm btn-warning" href="{{route('products.edit',$product->id)}}">Editer</a>
                    @csrf
                    @method('DELETE')
                    <button type="submit" class="btn btn-sm btn-danger">Deleter</button>
                    </form>
                    </td>
                </tr>
                @endforeach

If I already know how to solve my first problem about the create.blade.php, I am happy.

Thank you a lot for your help.

Edit: enter image description here

I don't get the amount total...

2
  • 1
    Show us your routes, please! Commented Apr 12, 2019 at 17:22
  • @Lucas Piazzi: Hello Piazzi, ^^ I have edited my message. Commented Apr 12, 2019 at 17:32

1 Answer 1

3

First problem:

In your productController you are validating that 'total' => 'required' but in your create.blade.php file you are not submitting anything regarding total. As a result, the validation is returning error but you are not even showing it. So, you are thinking that the form is stuck though it's not. Actually it's redirecting back with validation error of total filed is required.

Second problem

I am not 100% sure about this solution but you can try the mutator below. model.

public function setTotalAttribute()
{
    $this->attributes['total'] = $this->attributes['quantity'] * $this->attributes['price'];
}

By the way you don't need the unnecessary getTotalAttribute method in your

From comments

Seems like you are struggling with Eloquent's mutator. So, let's remove the mutator setTotalAttribute method and accessor from model and in controller

replace following line:

Product::create($request->all());

by the following lines:

$data = $request->all(); 
$data['total'] = $request->price * $request->quantity; 
Product::create($data);

Now check if it works.

Sign up to request clarification or add additional context in comments.

13 Comments

thank you my first problem is solve. But now, how I can get the total please? My model Product is not correct ???
Updated the answer, try it.
@Imrar: Thank you Imran, in my index.blade.php I have put this <td> {{$product->setTotalAttribute->total}}</td> but I have an error message App\Product::setTotalAttribute must return a relationship instance. I am really stuck.... :-(
Seems like you are struggling with Eloquent's mutator. So, let's remove the setTotalAttribute method from model. and in controller $data = $request->all(); $data['total'] = $request->price * $request->quantity; Product::create($data);
Updated the answer containing the previous comment.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.