Kanchan186's avatar

{"_token":"aPxhuhWNZm2KBVJoWiuEvN2Xc4tI3UTORCGIlpMs","p_name":"dfgm","image":"86282.jpg","submit":null}

values not store to database. please tell me where i going wrong

0 likes
16 replies
tykus's avatar

Probably your model does not permit mass-assignment. Set a $fillable property on the model with the attributes you will allow to mass-assign:

protected $fillable = ['p_name', 'image']'

Assumes you do not want to persist the CSRF token and submit button value?

siangboon's avatar

how could you expect people to undertand what had you done and what exact error you facing and where you went wrong with this simple sentense????? it can have lot of possibilities... at least brieft describe what happening.... post some codes, or error message for people to easy understand the issue...

first try to output the request()->all() to ensure all the data is received as expected and then double check whether have the column define in $fillable of the model if you are mass assignment.

Kanchan186's avatar

addprod.blade.php

        <div class="content">
           
                <form method="post" action="{{url('/')}}/store">
                    {{csrf_field()}}

             <table>

            <tr>
            <label>Enter product name</label>
            <input type="text" class="form-group" name="p_name" >
            </tr>

            <tr>
            <input type="file" name="image" class="custom-file-input">  
            <label >choose file</label>  
            </tr>
                    
             <tr><input type="submit" name=""></tr> 
             </table>      
            </form>
                
        </div>
    </div>
</body>
tykus's avatar

Dude, again with the formatting...

Please wrap your code in triple backticks (```)

Kanchan186's avatar

sir, I switched on markdown option but still preview not proper.

tykus's avatar

The correct format is

```

your code

```

tykus's avatar

productController.php '''

Sweet Fuck.

Kanchan186's avatar

productController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\product;

class ProductController extends Controller
{
   public function addpd()
                {
                $product=product::get();
                      return view('addprod',compact('product'));
                  }


   public function store(Request $request)
         {
           //dd($request->all());
          
            $product=new product();

            $product->p_name=$request->input('p_name');


            if ($request->hasfile('image'))
          {
                    
            $file=$request->file('image');

            $extension=$file->getClientOriginalExtension();

            $filename=time().'.'.$extension;

            $file->move('uploads/product',$filename);

            $product->image=$filename;
          }

          else{

             return $request;
             $product->image='';
           }
        $product->save();
        //return view('addprod')->with('product',$product);
         return view('viewprod',compact('product'));


            }
  
   //return redirect('viewprod');
   }

Kanchan186's avatar

product.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class product extends Model
{
    //protected  $table = 'products';
   protected $fillable = ['p_name', 'image'];
    protected  $primaryKey = 'p_id';

}

tykus's avatar

In your product controller, you are returning without saving the new product instance.

return $request;
1 like
Kanchan186's avatar

web.php

<?php

Route::get('/', function () {
    return view('addprod');
});

Route::get('addpd','ProductController@addpd');
Route::post('store','ProductController@store');
//Route::get('viewprod','CountryController@show');

Kanchan186's avatar

when i made changes, again getting error

SQLSTATE[HY000]: General error: 1364 Field 'p_name' doesn't have a default value (SQL: insert into `products` (`updated_at`, `created_at`) values (2019-08-21 10:36:06, 2019-08-21 10:36:06))

Tray2's avatar

Please watch the free Laravel From Scratch series before you do anything else.

1 like

Please or to participate in this conversation.