CoderFunda
  • Home
  • About us
    • Contact Us
    • Disclaimer
    • Privacy Policy
    • About us
  • Home
  • Php
  • HTML
  • CSS
  • JavaScript
    • JavaScript
    • Jquery
    • JqueryUI
    • Stock
  • SQL
  • Vue.Js
  • Python
  • Wordpress
  • C++
    • C++
    • C
  • Laravel
    • Laravel
      • Overview
      • Namespaces
      • Middleware
      • Routing
      • Configuration
      • Application Structure
      • Installation
    • Overview
  • DBMS
    • DBMS
      • PL/SQL
      • SQLite
      • MongoDB
      • Cassandra
      • MySQL
      • Oracle
      • CouchDB
      • Neo4j
      • DB2
      • Quiz
    • Overview
  • Entertainment
    • TV Series Update
    • Movie Review
    • Movie Review
  • More
    • Vue. Js
    • Php Question
    • Php Interview Question
    • Laravel Interview Question
    • SQL Interview Question
    • IAS Interview Question
    • PCS Interview Question
    • Technology
    • Other

17 May, 2022

In Laravel 9, how to insert data into a database [closed]

 Programing Coderfunda     May 17, 2022     Laravel, php     No comments   

 

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">
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Can't send mail with Lumen framework and no error appears

 Programing Coderfunda     May 17, 2022     Laravel, php     No comments   

 0

I have a problem with the Exceptions handler. By following this answer (which consists in adding a dd in the render function of Handler.php), I am now able to see what's going wrong.

So, here is the reason why my mail didn't send and why my page turned blank: Object of class Illuminate\Mail\Message could not be converted to string (View: /home/serveur-web/api.sl-projects.com/resources/views/emails/mailTemplate.blade.php).

The problem came from the fact that I was using the variable name $message, and this causes problems because it seems to be used in the class that handles the mails. My problem was therefore solved by renaming this variable $mailContent.

I hope this will help other people. :D

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Problem at startup laravel9 system after git clone

 Programing Coderfunda     May 17, 2022     Laravel, php     No comments   

 

 Answer

0

I am pretty sure it's browser dependent. The error appears every time in Mac Safari but not in Firefox and only sometimes in Chrome. I downloaded MS Edge also and Webshooter comes up without problems. So I think it's Safari that's the problem. Thank's all for your help anyway.

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

I can't concatenate correctly to the source of the image

 Programing Coderfunda     May 17, 2022     Laravel, php     No comments   

 

1 Answer

0

Try to use <?php __(your code here)__ ?> or <?-- __(your code here)__ ?> whenever you use php variables between html code.

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

How to create a multi language website with Laravel 9

 Programing Coderfunda     May 17, 2022     Laravel, php     No comments   

 Q


What is the best way to setup a multi language website with Laravel? The URL must contain the language (nl, fr, en). For example: mywebsite.com/en/faq. Most examples and tutorials I find use the session to store the current language which is completely useless of course. I should be able to directly link to a page in a specific language.

I could create a route per language but that does not really seem like a good idea. Ideally this could be made dynamic in order to easily create more locales.



Answers

0

I'm curious, why you cannot use session (it's a true question, I really would like to understand)?

You can use the same approach than with session: create a middleware to set App::setLocale("YourLanguage") based on query string.

public function handle(Request $request, Closure $next)
{
    // This example try to get "language" variable, \
    // if it not exist will define pt as default \
    // you also can use some config value: replace 'pt' by Config::get('app.locale')

    App::setLocale(request('language', 'pt'));

    return $next($request);
}

Or you can do it by Route:

// This example check the if the language exist in an array before apply
Route::get('/{language?}', function ($language = null) {
    if (isset($language) && in_array($language, config('app.available_locales'))) {
        app()->setLocale($language);
    } else {
      app()->setLocale(Config::get('app.locale'));
    }
    
    return view('welcome');
});

Source:

Laravel change language depending on a $_GET parameter

https://lokalise.com/blog/laravel-localization-step-by-step/

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Posts Older Posts Home

Meta

Popular Posts

  • Vue.js Tutorial
    Vue.js Tutorial Vue.js tutorial provides basic and advanced concepts of Vue.js. Our Vue.js Tutorial is designed for beginners and profession...
  • Automaticall generate pass through to child object function
    I would like to automatically forward a function call to a child in python, so that the parent class has said function and then forwards the...
  • Generalization
    Generalization Generalization is like a bottom-up approach in which two or more entities of lower-level levels combine to form a higher-leve...
  • Blade Component to Serve Images and Download Files
      Smart   makes it possible to serve images and download files from any location, including Laravel disks within a Laravel application. It e...
  • I made this code, but I am not understanding how JavaScript is handling the swapping??? (I have seen this before in an article but didnt explain)
    What is the name or official term of what I want to understand below in JavaScript?? I have never been able to find a clear article expla...

Categories

  • Ajax (26)
  • Bootstrap (30)
  • DBMS (42)
  • HTML (12)
  • HTML5 (45)
  • JavaScript (10)
  • Jquery (34)
  • Jquery UI (2)
  • JqueryUI (32)
  • Laravel (1017)
  • Laravel Tutorials (23)
  • Laravel-Question (6)
  • Magento (9)
  • Magento 2 (95)
  • MariaDB (1)
  • MySql Tutorial (2)
  • PHP-Interview-Questions (3)
  • Php Question (13)
  • Python (36)
  • RDBMS (13)
  • SQL Tutorial (79)
  • Vue.js Tutorial (69)
  • Wordpress (150)
  • Wordpress Theme (3)
  • codeigniter (108)
  • oops (4)
  • php (853)

Social Media Links

  • Follow on Twitter
  • Like on Facebook
  • Subscribe on Youtube
  • Follow on Instagram

Pages

  • Home
  • Contact Us
  • Privacy Policy
  • About us

Blog Archive

  • July (4)
  • September (100)
  • August (50)
  • July (56)
  • June (46)
  • May (59)
  • April (50)
  • March (60)
  • February (42)
  • January (53)
  • December (58)
  • November (61)
  • October (39)
  • September (36)
  • August (36)
  • July (34)
  • June (34)
  • May (36)
  • April (29)
  • March (82)
  • February (1)
  • January (8)
  • December (14)
  • November (41)
  • October (13)
  • September (5)
  • August (48)
  • July (9)
  • June (6)
  • May (119)
  • April (259)
  • March (122)
  • February (368)
  • January (33)
  • October (2)
  • July (11)
  • June (29)
  • May (25)
  • April (168)
  • March (93)
  • February (60)
  • January (28)
  • December (195)
  • November (24)
  • October (40)
  • September (55)
  • August (6)
  • July (48)
  • May (2)
  • January (2)
  • July (6)
  • June (6)
  • February (17)
  • January (69)
  • December (122)
  • November (56)
  • October (92)
  • September (76)
  • August (6)

Loading...

Laravel News

Loading...

Copyright © CoderFunda | Powered by Blogger
Design by Coderfunda | Blogger Theme by Coderfunda | Distributed By Coderfunda