Category «Laravel»

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’  

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 | …

Fix error 1071 Specified key was too long after change AppServiceProvider.php

When get error: [Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table users add unique users_email_unique(email)) Try below first: If any body facing same problem, Please follow the below steps to fix the issue: 1. Update the “config/database.php” for ‘mysql’ ‘engine’ => null, with ‘engine’ …