Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
974 views
in Technique[技术] by (71.8m points)

save - Laravel problem with saving into database

I have a problem with Laravel CRUD When im trying to add something to database nothing happen. Everything just freeze on /note/store

F12 result

I think there are some problems with $note->save(); in store function. It is also important to mention that redirect in store function (NoteController) also doesnt work.

Controller:

 <?php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use AppModelsNote;

class NoteController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return IlluminateHttpResponse
     */
    public function index()
    {
        $notes = Note::all();
    // var_dump($cele);
    return view('note.index')->with('notes', $notes);
    }

    
    public function create()
    {
        return view('note.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  IlluminateHttpRequest  $request
     * @return IlluminateHttpResponse
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required',
            'subtitle' => 'required',
            'content' => 'required',
            'color' => 'required',
            'tag' => 'required'
          ]);
      
          $current_date = date('Y-m-d H:i:s');

         
          $note = new Note;
          $note->name = $request->input('name');
          $note->subtitle = $request->input('subtitle');
          $note->content = $request->input('content');
          $note->color = $request->input('color');
          $note->tag = $request->input('tag');
          //$note->created_at = $current_date;
          //$note->updated_at = $current_date;
          $note->save();

          return redirect()->route('/index')->with('success','dodano');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return IlluminateHttpResponse
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return IlluminateHttpResponse
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  int  $id
     * @return IlluminateHttpResponse
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return IlluminateHttpResponse
     */
    public function destroy($id)
    {
        //
    }
}

Model:

<?php

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;

class Note extends Model
{
 use HasFactory;

 public $table = "notes";
 public $timestamps = true;

 protected $fillable = [
     'name',
     'subtitle',
     'content',
     'color',
     'tag'
 ];
}

web.php

    <?php

use IlluminateSupportFacadesRoute;
use AppHttpControllersNoteController;



//Route::get('/', function () {
   // return view('welcome');
//});

Auth::routes();

//Route::get('/home', [AppHttpControllersHomeController::class, 'index'])->name('home');


//Route::get('/', 'NoteController@index');

Route::resource('note', NoteController::class);


Route::post('/note/store', function () {
    return back()->withInput();
});

Auth::routes();

Route::get('/note', [AppHttpControllersNoteController::class, 'index'])->name('note');
Route::get('/note/create', [AppHttpControllersNoteController::class, 'create'])->name('create');
Route::post('/note/store', [AppHttpControllersNoteController::class, 'store'])->name('store');
//Route::get('/note/store', [AppHttpControllersNoteController::class, 'store'])->name('store');


//Route::get('note/create', array('uses' => 'NoteController@index', 'as' => 'note/create'));

migration:

    <?php

use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;

class CreateNotesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('notes', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('subtitle');
            $table->mediumText('content');
            $table->string('color');
            $table->string('tag');
            $table->timestamps();
        });

        // Uzupe?nienie tabeli rekordami
    DB::table('notes')->insert([
        [
            'name' => 'Notatka1',
            'subtitle' => 'Podtytu?',
            'content' => 'h3213',
            'color' => 'black',
            'tag' => 'notatka',
        ],
        [
            'name' => 'Notatka2',
            'subtitle' => 'Podtytu?',
            'content' => 'text',
            'color' => 'red',
            'tag' => 'notatka',
        ]
    ]);
    }

    

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('notes');
    }
}

Please,,, help;(

question from:https://stackoverflow.com/questions/65872208/laravel-problem-with-saving-into-database

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your web.php file, there is no route /index. Are you sure you have a road of this name ?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.7k users

...