Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.5 Method save does not exist when updating entries with modified primary key

I am working with laravel 5.5 to update entries. The problem is after changing the primary key 'id', which is elequoent default pk to 'project_id'. adding an item works fine but updating an item is not working properly. Here is the error I am getting.

Method save does not exist.

Here is my Model.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Project extends Model
{
   protected $primaryKey = 'project_id';
    public function user()
    {
        return $this->belongsTo(User::class);
    }
    public function tasks()
    {
        return $this->hasMany(Task::class);
    }
}

Here is my controller function.

public function editProject($id){

        $project = Project::where('project_id', $id)->firstOrFail();
        $data = ["project_info" => $project];
        return view('projects.edit')->with($data);
    }
    public function updateProject(Request $request){
        $data = $request->all();
        $validator = Validator::make($data, [
            'project_title' => 'required',
            'project_description' => 'required',
            'project_start_date' => 'required',
            'project_end_date' => 'required',
            'project_status' => 'required',
        ]);

        $response = [];

        if ($validator->fails()){
            $response["errors"] = [$validator->messages()->first()];
            $response["success"] = false;
            return json_encode($response);
        }

        else{
            $project = Project::where("project_id", $request->input('project_id'))->get();
            $project->project_title = $request->project_title;
            $project->user_id = Session::get('user_id');
            $project->project_description = $request->project_description;
            $project->project_start_date = $request->project_start_date;
            $project->project_end_date = $request->project_end_date;
            $project->project_status = $request->project_status;
            $project->save();
            return redirect('/listProjects');
        }
    }
like image 737
Melaku Minas Kasaye Avatar asked Oct 23 '17 08:10

Melaku Minas Kasaye


3 Answers

Using get() returns a collection. Despite the fact you are passing in a 'unique' ID, the project_id, it will still return a collection - the collection will simply have one element in it.

Subsequently, your code will not work as you have experienced, or at least not without a few changes to make $project reference the first element in the collection.

It's a quick fix though, just change this:

$project = Project::where("project_id", $request->input('project_id'))->get();

to this:

$project = Project::where("project_id", $request->input('project_id'))->first();

By using first(), eloquent will return the first element that matches the query and actually return the element (as opposed to a collection with one element) and so you can directly edit and save it.

like image 68
James Avatar answered Oct 21 '22 09:10

James


Here is the solution I found.

$project_id = $request->input('project_id');
$project = Project::find($project_id);
$project->save();
like image 35
Melaku Minas Kasaye Avatar answered Oct 21 '22 08:10

Melaku Minas Kasaye


You can find it by id using

Project::find($id);

Or get the first element like James said:

$project = Project::where("project_id", $request->input('project_id'))->first();
like image 43
Thiago Maciel Avatar answered Oct 21 '22 07:10

Thiago Maciel