mercredi 8 février 2017

Submit form and redirect to another view

I have 2 simple views: home and create. Here is the controller for them

public function index()
{
    $products = Product::all();
    return view('home', compact('products'));
}

public function createProduct() 
{
    return view('productCreate');
}
public function createProductSubmit( Request $request ) {


    $product = new Product;

    $product->productName = $request->name;
    $product->productPrice = $request->price;
    $product->quantity = strip_tags($request->quantity);
    $product->category_id = $request->category;

    $product->save();                              
    return view('home');
}

and the routes

Route::get('/home', 'HomeController@index');

Route::get('/home/product/create', 'HomeController@createProduct');
Route::post('/home/product/create', 'HomeController@createProductSubmit');

The problem is that when I go in /home/product/create to create new product and after I add everything and submit the form I've get the error

Undefined variable: products in home.blade.php

Why I get this error? In my index() function I have passing it?

Update

    <table class="table table-striped">
      <thead>
        <tr>
          <th>#</th>
          <th>Product Name</th>
          <th>Product Price</th>
          <th>Product Quantity</th>
          <th>Category</th>
          <th>Action</th>
        </tr>
      </thead>
      <tbody>
    @foreach( $products as $product )
        <tr>
          <th></th>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td><button class='btn btn-success'>Edit</button> <button class='btn btn-danger'>Delete</button></td>
        </tr>
    @endforeach
      </tbody>
    </table> 



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2ljEpph
via IFTTT

Aucun commentaire:

Enregistrer un commentaire