mirsahib
3 months ago
332
2
Laravel

Add new row to model table

Posted 3 months ago by mirsahib

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

Please sign in or create an account to participate in this conversation.