How can we do that?
{"_token":"aPxhuhWNZm2KBVJoWiuEvN2Xc4tI3UTORCGIlpMs","p_name":"dfgm","image":"86282.jpg","submit":null}
values not store to database. please tell me where i going wrong
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?
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.
productController.php
product.php
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>
Dude, again with the formatting...
Please wrap your code in triple backticks (```)
sir, I switched on markdown option but still preview not proper.
The correct format is
```
your code
```
productController.php '''
Sweet Fuck.
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');
}
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';
}
In your product controller, you are returning without saving the new product instance.
return $request;
web.php
<?php
Route::get('/', function () {
return view('addprod');
});
Route::get('addpd','ProductController@addpd');
Route::post('store','ProductController@store');
//Route::get('viewprod','CountryController@show');
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))
Please watch the free Laravel From Scratch series before you do anything else.
Please or to participate in this conversation.