Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error in passing the foreign key in the form for editing

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 class="row mt-3">
            <div class="col-12 col-md-6 namelabel">
                <form action="{{route('project.store')}}" method="post" enctype="multipart/form-data">
                    @csrf
                    <div class="mb-3">
                        <input type="hidden" class="form-control" name="client_id" value="{{$client_id}}" >
                    </div>
                    <div class="mb-3">
                        <label for="name" class="form-label">Project name</label>
                        <input type="text" class="form-control" name="name" value="{{$project->name}}">
                    </div>
                    <div class="mb-3">
                    <div class="mb-3">
                        <label for="logo" class="form-label">Insert image</label>
                        <input type="file" name="logo">
                    </div>
                    <div class="mb-3">
                        <label for="project_start_date" class="form-label">Data init</label>
                        <input type="date" class="form-control" name="project_start_date" value="{{$project->project_start_date}}">
                    </div>
                    <label for="description" class="form-label">Description</label>
                        <textarea name="description" cols="30" rows="10" class="form-control">{{$project->description}}</textarea>
                    </div>
                    <button type="submit" class="btn btn-primary mb-5">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

>Solution :

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}}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading