Laravel deadlock exception thrown only when request is coming from iOS - php

I have two mobile applications: Android and iOS. Both make requests to a laravel backend.
I just added a little form inside the app and tried to save it on the DB. The problem is that whenever I try to save it on iOS i get this error:
PDOException: SQLSTATE[40001]: Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction in /var/app/current/vendor/laravel/framework/src/Illuminate/Database/Connection.php:480
On Android, no matter how hard I try, the error is never thrown.
Specifically the exception is thrown by an update query and no jobs should be touching that table at any time.
This is the Controller function that throws the error (exact row highlighted):
public function questionnaireUpdate($trainingId, Request $request)
{
$training = Auth::user()->club->trainings()->findOrFail($trainingId);
$workloads = Workload::getAppWorkloads();
$user = Auth::user();
foreach ($workloads as $key => $w) {
$workload = $training->workloads()
->where('workload_id', '=', $w->id)
->where('user_id', '=', Auth::user()->id)
->first();
if ($workload) {
DB::table('athlete_workload')
->where('training_set_id', '=', $training->id)
->where('workload_id', '=', $w->id)
->where('user_id', '=', Auth::user()->id)
->update([
This is the row ====> 'value' => floatval($request->get(WorkloadHelper::getQuestionnaireSlug($w))), <=== This is the row
]);
} else {
// Performs an insert
}
}
WorkloadHelper::updateUserSumAndAverageWorkloadValues(Auth::user()->id, $trainingId);
return new JsonResponse(['message' => 'Questionnaire updated'], 200);
}
I tried logging the requests, the values that get passed to the update functions and even the query they are executing with ->toSql(), but they are exactly the same. Any idea of what could be causing this? And why only iOS?

Related

Can multiple queries be applied to a model in Laravel?

I'm currently trying to get data from the DB with a Laravel controller through a model Maintain. Is there a way to query the DB based on the request data made by an axios post from the front end, this includes both variables sub and zone, I have the ability to get the data from using sub OR zone but not both, is there a way to construct a multi where query if $request contains the necessary variables and a standard request if they are null or ""?
public function all(Request $request){
$query = [];
if($request->sub != ""){
array_push($query, ['subsystem', '=', $request->sub]);
}
if($request->zone != ""){
array_push($query, ['zone', '=', $request->zone]);
}
if(count($query) > 0){
return Maintain::all()->where($query);
}
else{
return Maintain::all();
}
}
Currently This returns with an error ErrorException: array_key_exists(): The first argument should be either a string or an integer in file but I've been using the Laravel reference and it doesn't seem to be working. I used Postman to get the readout of $query and it contains the following:
[
['sub', '=', 'Subsystem 1'],
['zone', '=', 'Zone 1']
]
Any help would be appreciated.
Try like this
public function all(Request $request){
$result = Maintain::when($request->zone, function ($q) use($request){
$q->where('zone', $request->zone);
})
->when($request->sub, function ($qw) use($request){
$qw->where('subsystem', $request->sub);
})
->get();
return($result);
}
when() method look like if-else
Edited: Let we know if you get an error: Happy coding

I have problem with fetching data with laravel when i use relations and paginate

what is error when i want to fetching data with paginate(10) the Vue js dosn't do it
but when i use paginate(5) it working good
this code of Controller with relationship in model files
and the response status 200 ok working
$results = Posts::with(['comment'])
->orderBy('created_at', 'desc')
->paginate(5);
return response()
->json(['results' => $results]);
this code is actually worked for me but i want to make 10 results in my page
Like this
$results = Posts::with(['comment'])
->orderBy('created_at', 'desc')
->paginate(10);
return response()
->json(['results' => $results]);
with ->paginate(10)or > 5 not giving any data and get error on console with Vue js but the response is ok 200
Try this:
$results = Posts::with('comment')->orderBy('created_at', 'desc')->paginate(10);
return response()->json(['results' => $results]);

Exception trigger on column update laravel 4.2

I working in laravel 4.2 version and I am using chunk method in larval eloquent when I run my query then I always receive the column name instead of update.
Here is my method to update table column
$result = Message::where('message_to', '=', $userData['id'])
->where('message_from', '=', $userData['message_from'])
->orwhere(function($query) use($userData) {
$query->where('message_to', '=', $userData['message_from'])
->where('message_from', '=', $userData['id']);
})->chunk(100,function($messages) use($userData){
foreach($messages as $msg){
$update = empty($msg->deleted_one) ? ['deleted_one'=>$userData['id']] : ['deleted_two'=>$userData['id']];
$msg->update($update);
}
});
Below is the response that I always receive when I run this query
{"success":"false","message":"deleted_two"}
In message I receive the column name.

Dingo API pagination with illuminate builder not eloquent

I am in the process of starting to learn Laravel 5.3 and Dingo API to build a basic REST API. I am trying to return the paginated JSON representation of a report query result. I can get pagination to work using Eloquent models, however not when using the raw Illuminate QueryBuilder for DB access. Here is what I've been trying:
public function getWentCommission(Request $request, $start, $end)
{
$filters = $request->input('filter');
$query = DB::table('commissions')
->selectRaw("fields")
->leftJoin('users', 'users.id', '=', 'commissions.customerid')
->leftJoin('customers', 'customers.id', '=', 'commissions.customerid')
->whereBetween('datestart', [$start, $end])
->orWhereBetween('dateend', [$start, $end])
->orWhereNull('dateend')
->where('void', '=', 0)
->orderBy('startdate');
// Filter by users?
if($filters && array_key_exists('users', $filters))
{
$rawids = array_map('intval', array_unique(array_filter(explode(',', $filters['users']), 'is_numeric' )));
$query = $query->whereIn('users.id', $rawids);
}
return $this->response->paginator(new IlluminatePaginatorAdapter($query->paginate(25)), new ObjectTransformer);
}
However, this only nets me the following error which I have yet to figure out how to resolve. Any advice would be greatly appreciated. I'm sure I'm missing something obvious here.
Type error: Argument 1 passed to League\Fractal\Pagination\IlluminatePaginatorAdapter::__construct() must implement interface Illuminate\Contracts\Pagination\LengthAwarePaginator, instance of Illuminate\Support\Collection given

Laravel - No query results for model

I am gettin error 'No query results for model [App\User].', when i am trying to find an user with this query.
if(User::where('email', '=', Request::only('email'))->firstOrFail()){
$validator->errors()->add(...);
}
Request::only() will actually return an array. In your case: ['email' => 'the-value-of-email']. Either do this:
User::where(Request::only('email'))->firstOrFail()
Or just use Request::input() to retrieve only the value:
User::where('email', '=', Request::input('email'))->firstOrFail()

Categories