Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mirsahib's avatar

Add new row to model table

I am try to insert new item to Payment table Here is my Payment table

public function up()
    {
        Schema::create('payments', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('tenant_id')->unsigned();
            $table->integer('paid_rent')->default(0);
            $table->integer('dues')->default(0);
            $table->string('pay_month',15);
            $table->string('pay_year',5);
            $table->boolean('pay_status')->default(0);
            $table->string('comment')->default("Earum eligendi reprehenderit impedit eveniet. Omnis et optio voluptatem dolorum non.");
            $table->timestamps();
            $table->foreign('tenant_id')->references('id')->on('tenants');
        });
    }

Here is my controller function

public function create(Request $request){
        $validateData = Validator::make($request->all(),[
            'month'=>'required',
            'year'=>'required',
        ]);
        if($validateData->fails()){
            return response()->json(["message"=>$validateData->messages()], 404);
        }else{
            $tenantRows = DB::select(DB::raw("SELECT COUNT(tenants.id) FROM tenants WHERE tenants.tenant_status=:status"),array('status'=>1));
            $tenantsId = DB::select(DB::raw("SELECT tenants.id FROM tenants WHERE tenants.tenant_status=:status"),array('status'=>1));
            foreach($tenantsId as $id){
                $payment = new Payment;
                $payment->tenant_id = $id;
                $payment->pay_month = $request->month;
                $payment->pay_year = $request->year;
                $payment->save();                
            }
            return response()->json('success');
        }

    }

I am getting the following error while inserting new row to the table

Object of class stdClass could not be converted to string
0 likes
2 replies
MarianoMoreyra's avatar
Level 25

Hi @mirsahib !

According to documentation (https://laravel.com/docs/7.x/database#running-queries):

The select method will always return an array of results. Each result within the array will be a PHP stdClass object, allowing you to access the values of the results:

So you should access the id using $id->id instead.

Anyway, why are you using DB::select instead of Eloquent Model queries?

In this example you could simply do something like this (assuming you have a Tenant model):

    $tenants = Tenant::where('status', 1)->get();
    
    foreach($tenants as $tenant){
         $payment = new Payment;
         $payment->tenant_id = $tenant->id;
         $payment->pay_month = $request->month;
         $payment->pay_year = $request->year;
         $payment->save();                
    }

mirsahib's avatar

Thanks @marianomoreyra I am using DB because i learn sql before eloquent and i am kind of lazy to learn the eloquent syntax. look like i have to change my habit

Please or to participate in this conversation.