Reputation: 13
NewsController.php
This is the contoller and i use the create function to add record to the database but when i add all records will be empty
public function insertNews() {
$attribute = [
'Ntitle' => 'News Title',
'Ndesc' => 'News Description',
'Naddedby' => 'News Added By',
'Ncontent' => 'News Content',
'Nstatus' => 'News Status'
];
$data = $this->validate(request(), [
'Ntitle' => 'required',
'Ndesc' => 'required',
'Naddedby' => 'required',
'Ncontent' => 'required',
'Nstatus' => 'required'
], [], $attribute);
News::create($data);
return redirect('all/news');
}
and i use dd() to show the record and it will show with the token and all other values
dd(request()->all())
News.php and this is the model file
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class News extends Model
{
use SoftDeletes;
protected $primaryKey = 'News_ID';
protected $date = ['delete_at'];
protected $fillable = ['News_Title', 'News_Descrption', 'News_Content', 'Added_By', 'News_Status'];
}
web.php this is the web file (routes)
Route::post('insert/news', 'NewsController@insertNews');
news.blade.php
and this is the blade file that contain the form that i sent to the controller
<form action="{{ url('insert/news') }}" method="POST">
{{ csrf_field() }}
<input type="text" name="Ntitle" value="{{ old('Ntitle') }}" placeholder="News Title"><br />
<input type="text" name="Ndesc" value="{{ old('Ndesc') }}" placeholder="News Descrption"><br />
<input type="number" name="Naddedby" value="{{ old('Naddedby') }}" placeholder="News Added By"><br />
<textarea name="Ncontent" value="{{ old('Ncontent') }}" placeholder="News Content"></textarea><br />
<select name="Nstatus">
<option value="Active" {{ old('Nstatus') == 'Active' ? 'selected' : '' }}>Active</option>
<option value="Pending" {{ old('Nstatus') == 'Pending' ? 'selected' : '' }}>Pending</option>
<option value="Disabled" {{ old('Nstatus') == 'Disabled' ? 'selected' : '' }}>Disabled</option>
</select><br />
<input type="submit" value="Add News">
</form>
Upvotes: 1
Views: 5001
Reputation: 351
There are number of reasons for your code to not work:
public function insertNews() {
$attribute = [
'Ntitle' => 'News Title',
'Ndesc' => 'News Description',
'Naddedby' => 'News Added By',
'Ncontent' => 'News Content',
'Nstatus' => 'News Status'
];
$this->validate(request(), [
'Ntitle' => 'required',
'Ndesc' => 'required',
'Naddedby' => 'required',
'Ncontent' => 'required',
'Nstatus' => 'required'
], [], $attribute);
News::create($attribute);
return redirect('all/news');
}
Make sure that you have all the fields added to protected $fillable = []; in your model like this:
class News extends Model {
protected $fillable = [
'Ntitle', 'Ndesc', 'Naddedby', 'Ncontent', 'Nstatus'
];
Upvotes: 2
Reputation: 45
Can i have your web route? so i can tell that what's the mistake,
i think your route should be
Route::namespace('your_Controller_Foldername')->group(function () {
Route::resource('insert/news', 'NewsController');
});
Upvotes: 0
Reputation: 2540
You need to pass the value to the create() method.
//Passing the request to create method
News::create($request->all());
return redirect('all/news');
It will now store if you have added inputs name in the $fillable variable in the News model.
//News.php (Model)
protected $fillable = ['Ntitle', 'Ndesc', 'Naddedby', 'Ncontent', 'Nstatus'];
I prefer to use
//News.php (Model)
protected $guarded = [];
Upvotes: 0