Merhaba @aysegul_hatap
Sırası ile yazıyorum 🙂
Gerekli alt yapımızı oluşturalım öncelikle
php artisan make:model Topic -crm
//bununla Model Controller ve Migration hepsi tek sefer de yarata bilirsin
ve bununla birlikte Migration dosyamız oluştu
//database/migrations/2019_09_30_193553_create_Topics_table.php
// update edelim bu şekilde düzenleyelim
public function up()
{
Schema::create('uopics', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->text('surname');
$table->timestamps();
});
}
php artisan migrate
// çalıştıralım
ve şimdi sıra geldi örneğimizi yapmaya
- öncelikle iki adet router ekledim bir örnek form yaptım bunu view etmek için birde formu post etmek için
//routes/web.php
Route::post('/deneme', 'TopicController@test')->name('topic.deneme');
Route::get('/test', 'TopicController@deneme')->name('topic.test');
TopicController ekledim adını da TopicController verdim içerisine iki adet Methot ekledim
//app/Http/Controllers/TopicController.php
public function deneme()
{
return view('test');
}
public function test(Request $request)
{
$json_field = [
$request->name => $request->surname
];
Topic::create([
'name' => $request->name,
'surname' => $request->surname,
'json' => json_encode($json_field)
]);
}
**3. Modele bunu ekledim insert edeceğim alanlara izin vermek için ben Topic diye bir Model yarattım**
/app/Topic.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Topic extends Model
{
protected $fillable = ['name','surname','json'];
}`
4. son olarak formu ekledim
//resources/views/test.blade.php
<form method="POST" action="{{ route('topic.deneme') }}">
@csrf
<input type="text" name="name" placeholder="Enter your name">
<input type="text" name="surname" placeholder="Enter your surname">
<input type="submit" value="Send">
</form>
Umarım sana vede başkalarına yardımcı olur