Rawa Hamid
Rawa Hamid

Reputation: 13

Laravel how to add records with create function in controller?

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>

enter image description here

Upvotes: 1

Views: 5001

Answers (3)

Inderpreet Singh
Inderpreet Singh

Reputation: 351

There are number of reasons for your code to not work:

  1. In your case, you have incorrectly equated the $data to the validation object. You should use the $attribute (the key value array of data, not the validation object) in the create function instead of the $data.
  2. Secondly the keys passed in the array of the create data should be exactly same to the fields name of the table that you are using for data insertion. I see that your code shows Ntitle, Ndesc etc being used for the validation and in the $attribute array, while the fillable protected $fillable has the different field names! So, please use the correct field names in the data that you are passing and in the $fillable in the model. I am writing the below code assuming that Ntitle, Ndesc, Naddedby, Ncontent, Nstatus are the field names of your table. As per that please refer to the below code!

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

Karan Khimani
Karan Khimani

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

Lijesh Shakya
Lijesh Shakya

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

Related Questions