In Laravel 9, how to insert data into a database [closed]
Q
I am working with Laravel 9. I am trying to insert data into database. It is not user data, data will be inserted into database on form submit button.
My view:
<form action="{{ route('store.category') }}" method="POST">
@csrf
<div class="mb-3">
<label for="categoryName" class="form-label">Category Name</label>
<input type="text" class="form-control" id="category_name" placeholder="Enter Category Name">
@error('category_name')
<span class="text-danger">{{ $message }}</span>
@enderror
</div>
<button type="submit" style="float: right;" class="btn form-control btn-primary">Add</button>
</form>
My web.php:
Route::post('/category/add', [CategoryController::class, 'AddCat'])->name('store.category');
My model
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Category extends Model
{
protected $table = 'categories';
use SoftDeletes;
protected $fillable = [
'user_id',
'category_name'
];
} My table migration:
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->integer('user_id');
$table->string('category_name');
$table->timestamps();
$table->SoftDeletes();
});
}
My controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Category;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
class CategoryController extends Controller
{
public function AddCat(Request $request)
{
$validated = $request->validate([
'category_name' => 'required|unique:categories|max:255',
]);
First i tried this
$category = Category::create([
'user_id' => Auth::user()->id,
'category_name' => $request->category_name,
]);
$category->save();
Then this
$category = new Category;
$category->user_id = Auth::user()->id;
$category->category_name = $request->category_name;
$category->save();
Then this
$user_id = Auth::user()->id;
$category_name = $request->input('category_name');
$data=array("user_id"=>,"$user_id"=>$category_name);
DB::table('categories')->insert($data);
}
}
But the data not inserting into database. The command is showing the below every time the form is submitted
[Fri May 13 20:06:36 2022] 127.0.0.1:8821 Accepted
[Fri May 13 20:06:36 2022] 127.0.0.1:8821 Closing
[Fri May 13 20:06:36 2022] 127.0.0.1:8823 Accepted
[Fri May 13 20:06:37 2022] 127.0.0.1:8823 Closing
[Fri May 13 20:06:37 2022] 127.0.0.1:8825 Accepted
[Fri May 13 20:06:37 2022] 127.0.0.1:8826 Accepted
[Fri May 13 20:06:37 2022] 127.0.0.1:8827 Accepted
[Fri May 13 20:06:37 2022] 127.0.0.1:8825 [200]: GET /css/app.css
[Fri May 13 20:06:37 2022] 127.0.0.1:8826 [200]: GET /js/app.js
[Fri May 13 20:06:37 2022] 127.0.0.1:8825 Closing
[Fri May 13 20:06:37 2022] 127.0.0.1:8826 Closing
[Fri May 13 20:06:42 2022] 127.0.0.1:8827 Closed without sending a request; it was probably just an unused speculative preconnection
[Fri May 13 20:06:42 2022] 127.0.0.1:8827 Closing
Can someone help me with the solution
Found the answer. The issue created due to my stupidity!
<input type="text" class="form-control" id="category_name" placeholder="Enter Category Name">
Forgot to put name attribute in the input tag
<input type="text" class="form-control" name="category_name" id="category_name" placeholder="Enter Category Name">
0 comments:
Post a Comment
Thanks