How to add property/element to Laravel collection

// prepare api data for reactjs $new_data = collect([]); foreach ($sorts as $time=>$date) { $tmp_ary[‘num’] = $records[$date][‘num’]; $tmp_ary[‘date_formated’] = date(‘F Y’, strtotime($date)); $tmp_ary[‘earned’] = round($records[$date][‘earned’],2); $tmp_ary[‘used’] = $records[$date][‘used’]; $tmp_ary[‘total_points’] = round($totalPoints[$date],2); $tmp_ary[‘date’] = $date; //$new_data->put($date, $tmp_ary); $new_data->push($tmp_ary); }

How to add/update/sync many to many relationship pivot table records using Laravel eloquent

First, make sure you have many to many(belongsToMany NOT hasMany) relationships in the module: public function builderBusinessNatures() { return $this->belongsToMany(\App\Models\BuilderBusinessNature::class, ‘builder_business_natures’, ‘builder_id’, ‘business_nature_id’); } $builder->builderBusinessNatures()->sync( $data[‘business_nature_ids’]);

How to search relationship in Laravel inside with()

$businessNatures = $this->businessNatureRepository ->with([‘builderBusinessNatures’ => function ($query) Use($request) { $query->select([‘builder_id’, ‘business_nature_id’])->where(‘builder_id’, ‘=’, $request->builder_id); }])->all([‘id’,’title’, ‘sort_value’]);

No error when use with() to retrieve a record, because of no deleted_at column

$builder = $this->builderRepository ->with([‘builderTypesPivots’]) ->findWithoutFail($id); Once add relationship with([‘builderTypesPivots’]) always can’t retrieve the record, but no error: { success: false, message: “Builder not found” } change to : $builder = Builder::with([‘builderTypesPivots’])->find($id); dd($builder); then the error shows: SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘builder_types_pivot.deleted_at’ in ‘where clause’  

Dynamic loop and set this.state base on result/record from the database

Dynamic loop and set this.state base on result/record from the database componentDidMount() { axios.get(‘/api/providers/’+this.state.provider_id, { params: { //filter: ‘id;phone;company_name;email’, } }).then(response => {; letnewStateObj= {} Object.keys(this.state).forEach(function(key) { if ( item[key]){ newStateObj[key] =item[key] } }); this.setState(newStateObj); }) }

Use React.Fragment to avoid JSX elements must be wrapped in an enclosing tag

app.js:62227 Uncaught Error: Module build failed: SyntaxError: Adjacent JSX elements must be wrapped in an enclosing tag function Glossary(props) { return ( <dl> { => ( // Without the `key`, React will fire a key warning <React.Fragment key={}> <dt>{item.term}</dt> <dd>{item.description}</dd> </React.Fragment> ))} </dl> ); } Warning: Each child in an array or iterator should have …

Laravel webpack.mix.js set up externals to reduce the size of app.js from 400KB to 66KB

Laravel webpack.mix.js set up externals and use third CDN js file to reduce the size of app.js const mix = require(‘laravel-mix’); /* |————————————————————————– | Mix Asset Management |————————————————————————– | | Mix provides a clean, fluent API for defining some Webpack build steps | for your Laravel application. By default, we are compiling the Sass | …

How to efficiently update/sync pivot table/join table using PHP with selected multiple checkbox

First, Let’s assume we pass selected multiple checkboxes ids to parameter permissionIds, eg. 4,5,2,12,9 $existsPageIdsAry = array_column($existsPermissions, ‘pageID’); $permissionIdsAry = explode(‘,’,$data[‘permissionIds’]); $pageIdsNeedRemove = array_diff($existsPageIdsAry, $permissionIdsAry); $pageIdsNeedAdd = array_diff($permissionIdsAry, $existsPageIdsAry); Then we can do delete where id in ($pageIdsNeedRemove) and insert pivot table records in $pageIdsNeedAdd

How to insert multiple record using Doctrine

/** * Insert multiple record together * improve speed from 8 seconds to less than 1 second for insert 200 records */ $em = $this->_service->getEntityManager(); foreach ($pageIdsNeedAdd as $pageID ){ $rp = newRolePermission; $rp->pageID = $pageID; $rp->roleID = $data[‘roleID’]; $em->persist($rp); } $em->flush(); $em->clear();