Home > Software engineering >  Error in passing the foreign key in the form for editing
Error in passing the foreign key in the form for editing

Time:07-30

I have two tables, one named Client and the other named Projects linked together via a foreign key (this is client_id, which is present in Projects).

Each project has an edit button; when I click to edit a project I have a form with all fields secured to it.

To edit a project I have to pass the client id (client_id) associated with that project.

To do this, I did the following:

ROUTE

Route::get('/project/edit/{project}', [ProjectController::class, 'edit'])->name('project.edit');

CONTROLLER

public function edit(Project $project)
    {
        $client_id = Project::select('client_id')->where('id',$project->id)->get();
        //dd($client_id);
        return view('project.edit', compact('project','client_id'));
    }

VIEW

<div >
            <div >
                <form action="{{route('project.store')}}" method="post" enctype="multipart/form-data">
                    @csrf
                    <div >
                        <input type="hidden"  name="client_id" value="{{$client_id}}" >
                    </div>
                    <div >
                        <label for="name" >Project name</label>
                        <input type="text"  name="name" value="{{$project->name}}">
                    </div>
                    <div >
                    <div >
                        <label for="logo" >Insert image</label>
                        <input type="file" name="logo">
                    </div>
                    <div >
                        <label for="project_start_date" >Data init</label>
                        <input type="date"  name="project_start_date" value="{{$project->project_start_date}}">
                    </div>
                    <label for="description" >Description</label>
                        <textarea name="description" cols="30" rows="10" >{{$project->description}}</textarea>
                    </div>
                    <button type="submit" >Modifica progetto</button>
                </form>
            </div>
        </div>

I get the following error:

Incorrect integer value: '[{"client_id":14}]' for column 'client_id' at row 1

How can I solve this problem?

Thanks to those who will help me

CodePudding user response:

This statement

Project::select('client_id')->where('id',$project->id)->get()

does not return the client_id. It returns an Eloquent Collection of Projects with only the client_id Attribute.

You can chain the pluck function to extract only value and then you can call first to get the value from the collection.

Project::select('client_id')
  ->where('id',$project->id)
  ->get()
  ->pluck('client_id')
  ->first()

After checking your code in the full detail you don't need to do the select part at all.
You have already the Project Model so you can access all of it's fields directly.

$project->client_id

In your view you are doing this already with the name of the project:

{{$project->name}}

You can do the same with the client_id as well:

{{$project->client_id}}
  • Related