nivan34
nivan34

Reputation: 23

Method Illuminate\Support\Collection::save does not exist in Laravel

I have a dropdown list in a data capture view where a join is used in the Controller, but when I am trying to save the record, I am getting a below error.

Method Illuminate\Support\Collection::save does not exist.

I have tried to change the eloquent statement to

 $energy = DB::table('vehicleslog')->join('vehicle', 'vehicleslog.vehicle_id', '=', 'vehicle.id')->first();

instead of

$energy = DB::table('vehicleslog')->join('vehicle', 'vehicleslog.vehicle_id', '=', 'vehicle.id')->first();

however that gives me a below error.

Call to undefined method stdClass::save()

Does anyone know what the correct one is?

Controller:

  public function index()
    {

        // $energy = Maintenance::orderBy('id', 'desc')->paginate(5);
        $energy = DB::table('vehicleslog')->join('vehicle', 'vehicleslog.vehicle_id', '=', 'vehicle.id')->get();
        $cars = Vehicle::get();
        $staff = Staff::all();
        return view('admin.vmaintenance', compact('energy', 'cars', 'staff'));
    }
    public function store(Request $request)
    {

        // $energy = new Maintenance;
        $energy = DB::table('vehicleslog')->join('vehicle', 'vehicleslog.vehicle_id', '=', 'vehicle.id')->first();
        $cars = Vehicle::all();
        $staff = Staff::get();
        $energy->staff_key = $request->input('staff_key');
        $energy->vehicle_id = $request->input('vehicle_id');
        $energy->log_dt = $request->input('log_dt');
        $energy->admin_time = $request->input('admin_time');
        $energy->driving_time = $request->input('driving_time');
        $energy->work_time = $request->input('work_time');
        $energy->jobcard_count = $request->input('jobcard_count');
        $energy->start_odo = $request->input('start_odo');
        $energy->end_odo = $request->input('end_odo');


        $energy->save();
        return redirect('/vmaintenance')->with('success', 'data added');
    }

View:

<label>Select Vehicle</label>
                    <select name="vehicle_id" >
                        @foreach($cars as $car)
                        <option value="{{ $car->id }}">{{ $car['reg_number'] }}</option>
                @endforeach
                    </select>
                  
           
            

Upvotes: 0

Views: 1779

Answers (2)

Roman Samarsky
Roman Samarsky

Reputation: 390

Eager loading:

$energy = VehicleLog::with('vehicle')->first();

Also:

  • this variable never used in your store() action:
$cars = Vehicle::all();
$staff = Staff::get();
  • you can use fill() method:
$energy->fill($request->only([
    'staff_key', 'vehicle_id', 'admin_time',
    'driving_time', 'work_time', // ...
]));

Upvotes: 1

Bennett
Bennett

Reputation: 738

Since you aren't using a model you'll need to just use an insert statement.

DB::table('vehicleslog')->insert([
   'staff_key'   => $request->input('staff_key'),
   'vehicle_id'  => $request->input('vehicle_id'),
   'log_dt'      => $request->input('log_dt'),
   // etc........
)];

Upvotes: 0

Related Questions