samedi 3 mars 2018

Laravel 5 Saving the Selected input from a Form::Select

I a struggling to save the chosen information from my selection box. I get the message:

"SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (clientpad_notes.notebooks, CONSTRAINT notebooks_contact_id_foreign FOREIGN KEY (contact_id) REFERENCES contacts (id) ON DELETE CASCADE) (SQL: insert into notebooks (name, contact_id, note_description, note_body, user_id, updated_at, created_at) values (assddsaasdadasasd, 3, saS,

ssss

, 2, 2018-03-03 19:18:51, 2018-03-03 19:18:51)

My problem is that I am bringing in information to notes create page from contacts table. So I have my tables linked with authentication with user_id and inside the notes tables I have contacts_id. I am aiming to select and save a contact name fetching it by it's id, when creating a new note. It is possible I have been going around doing this in a wrong way, I am a beginner at Laravel so any help would be appreciated.

Here is my notes controllers and create a note page.

NotesController.php

public function create() {

    $user_id = auth()->user()->id;
    $user = User::find($user_id);
    $contacts = Contact::find($user->contacts)->pluck('fullName');
    return view('notebooks.create')->with('contacts', $contacts)->with('user', $user);
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{

    $this->validate($request,[
        'name' => 'required',
        'contact_id' => 'required',
        'note_description' => 'required',
        'note_body' => 'required',
    ]);

    //Create a note
    $notebook = new Notebook;
    $notebook->name = $request->input('name');
    $notebook->contact_id = $request->input('contact_id');
    $notebook->note_description = $request->input('note_description');
    $notebook->note_body = $request->input('note_body');
    $notebook->user_id = auth()->user()->id; //currently logged in user show their notes
    $notebook->save();

    return redirect('/dashboard')->with('success', 'Your Note Was Created');
}

create.blade.php

                <div class="col-6 col-sm-3">
                        
                        

                </div>
            </div>

In the database which saved notes I have this:

Schema::create('notebooks', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->unsigned()->nullable();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->integer('contact_id')->unsigned()->nullable();
        $table->foreign('contact_id')->references('id')->on('contacts')->onDelete('cascade');
        $table->string('name');
        $table->mediumText('note_description');
        $table->mediumText('note_body');
        $table->timestamps();
    });



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire