Monthly archives: November, 2018

VueJs/ReactJS await for async axios function get/post method and sleep(n) seconds

Note: you cannot use await unless your function is marked async sleep(ms) { return new Promise(resolve=>setTimeout(resolve, ms)); }, async onSubmit() { this.loading=true; this.reports= []; if ( this.all_builders_selected ){ // NOT GOOD, GOT ERROR 429 (Too Many Requests) for (let [index, builder] ofthis.builders.entries()) { await this.sleep(1000); await this.getOneReport(builder.id); console.log(builder.id, index) //if ( index > 5 ) …

How to update a column from another column and append an additional string or variable in Laravel

How to use Laravel update a column from another column and append an additional string or variable $path = ‘providerImages/’; $results = DB::table(‘provider_websites’) ->where(‘logo_name’, ‘NOT LIKE’, ‘%’ . $path . ‘%’) ->where(‘logo_width’,’>’,1) //->limit(1) ->update([‘logo_name’ => DB::raw(“CONCAT(‘” . $path . “‘,logo_name)”)]) ;

How to use ReactJS upload file in a update form send data to Laravel api backend

jsx: <form enctype=”multipart/form-data”>< input id=”photo” accept=”image/*” name=”photo” type=”file” /> js: handleFieldChange(event) { if ( event.target.type == ‘file’){ this.setState({ [event.target.name]: event.target.files[0] }) } else { this.setState({ [event.target.name]: event.target.value }) } } let formData = new FormData(); formData.append(“_method”, ‘PUT’); for (let key in this.state) { //console.log(this.state[key]); formData.append(key, this.state[key]); } //console.log(formData); return false; axios.post(`/api/xxxx`, formData) .then(response => { …

How to get all rows and ignore soft deleted using with() relationship by Laravel?

How to get all rows and ignore soft deleted using with() relationship by Laravel? ->with([ ‘provider’ =>function($query) { $query->withTrashed(); }, ‘builderPurchases’ => function ($query) Use($user, $startDate, $endDate) { $query->where(‘builder_id’, ‘=’, $user->builder_id) ->where(‘date_payment_received’, ‘>=’, $startDate) ->where(‘date_payment_received’, ‘<=’, $endDate) ; } ]) ->all();

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 => { letitem=response.data.data; letnewStateObj= {} Object.keys(this.state).forEach(function(key) { if ( item[key]){ newStateObj[key] =item[key] } }); this.setState(newStateObj); }) }