返回并获取一个类中的对象

I am unable to write a code to get object for another function.

I have one class called RepairsService where I have two functions.

First createRepair($request, $vehicle) is where I create object.

public function createRepair($request, $vehicle)
    {
        DB::transaction(function () use ($request, $vehicle) {

            Log::info('Creating new repair.');

            $repair = new Repair();
            $repair->vehicle_id = $vehicle->id;

            DB::insert('insert into repairs (vehicle_id) values (?)', 
                    [$repair->vehicle_id]); 
        });
       $this->addRepair($request, $vehicle, $this); //here I call second function trying to pass $this parameter as object 
    }

And second in the same class is

public function addRepair($request, $vehicle, $repair){

        DB::transaction(function () use ($request, $vehicle, $repair) {

            $workers_needed->repair_id = $repair->id;  //I need to get this id from the object 
            $workers_needed->worker_id = $request->worker_id;

            DB::insert('insert into repair_worker (repair_id, worker_id) values (?, ?)',
                    [$workers_needed->repair_id, $workers_needed->worker_id]);

            Log::info('Repair created.');
            flash('Repair successfully added.', 'success');
        });
    }

What am I doing wrong? Right now using this code, I am getting error Undefined property: App\Models\Repairs\RepairsService::$id what makes sense, since I am not passing object (I think).

UPDATE:

So the main problem described above is fixed in the following code, but I am heading a very little problem now. In the second function, I am trying to assign $repair-id to $workers_needed->repair_id but I get NULL value there. I have tried to assign value 1 (hard coded) instead of using $repair-id but it seems that my second function call runs before the first function is done with transaction which makes sense, because as far as I know, transaction inserts data only if everything goes well and in this case, it is waiting for second function to get its work done. So the repair instance is not inserted into database. How can I edit the code so the transaction gets done and then I call my second function? Or is there another way how to do it?

public function createRepair($request, $vehicle)
    {
        $repair = null;
        DB::transaction(function () use ($request, $vehicle, $repair) {

            Log::info('Creating new repair.');

            $repair = new Repair();
            $repair->vehicle_id = $vehicle->id;

            DB::insert('insert into repairs (vehicle_id) values (?)', 
                    [$repair->vehicle_id]); 
            $this->addRepairWorker($request, $vehicle, $repair);
        });
    }


    public function addRepairWorker(Request $request , Vehicle $vehicle, Repair $repair){

        $workers_needed = null;
        DB::transaction(function () use ($request, $vehicle, $repair) {

            Log::info('Creating new repair worker instance.');

            $workers_needed = new Repair_worker();
            $workers_needed->repair_id = $repair->id;
            $workers_needed->worker_id = $request->worker_id;

            DB::insert('insert into repair_worker (repair_id, worker_id) values (?, ?)',
                    [$workers_needed->repair_id, $workers_needed->worker_id]);

            Log::info('Repair created.');
            flash('Repair successfully added.', 'success');
        });
    }

Should it not be;

public function createRepair($request, $vehicle)
{
    $repair = null;
    DB::transaction(function () use ($request, $vehicle, $repair) {

        Log::info('Creating new repair.');

        $repair = new Repair();
        $repair->vehicle_id = $vehicle->id;

        DB::insert('insert into repairs (vehicle_id) values (?)', 
                [$repair->vehicle_id]); 
    });
   $this->addRepair($request, $vehicle, $repair);
}

This is because you are expecting a repair instance in that function call.

Additionally, you are using an anonymous function so you have to declare repair outside of the scope and declare it for the function using use.

UPDATE

In order to accommodate for the new error, you need to commit() the transaction in order for it to updated/inserted. More information can be found here.

DB::transaction(...);
DB::commit(); // This is where the query actually gets sent to the database

As you are rolling your own DB wrapper you may have to write the method yourself, an example of using PDO is below;

class DB {
    ...
    public function commit() {

        return $PDO->commit();
    }
    ...
}

Commit returns a bool on success/failure so you can rollback if false or proceed when true.

In your first method in class RepairsService you call your second method like this:

$this->addRepair($request, $vehicle, $this);

$this is an instance of RepairsService but in your second method it seems that you expect an object of class Repair there.

So you should probably call your second method using the object you just instantiated in the first method:

$this->addRepair($request, $vehicle, $repair);

In general it would be useful to add type-hinting so that you can catch these mistakes directly. Your second method would look for example something like:

public function addRepair(Request $request, Vehicle $vehicle, Repair $repair){
    ...

Assuming that I have your class names right...

Now your IDE should tell you that you are doing something wrong before you even run the code.

Edit: Note that if you want to use type-hinting (you should...), you need to use the correct classes.

So in your case it might very well be something like:

public function addRepair(\Illuminate\Http\Request $request, Vehicle $vehicle, Repair $repair){
    ...

But in the end it depends on what objects you want to pass and what classes they belong to exactly.