Exception trigger on column update laravel 4.2 - php

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.

Related

how to select join in laravel with condition

public function clientcmd()
{
$client = Commande::join('clients', 'commande.id_cli', "=", 'clients.id')
->select('clients.*')
->where('commande.id_cli', '')
->get();
return response()->json($client);
}
I want to select all the client where id client = id cli in command table but that not work they return an empty array
We don't know the data in your table. I assume you want to select fields whose id_cli field is not empty string.
$client = Commande::join('clients', 'commande.id_cli', "=", 'clients.id')
->select('clients.*')
->where('commande.id_cli','!=', '')
->get();
Use Query builder:
use Illuminate\Support\Facades\DB;
$clients = DB::table('clients')
->join('commande', 'commande.id_cli', '=', 'clients.id')
->select('clients.*')
->where(//you can put condition here)
->get();
You can use Client Model to get all client records
$clients = Client::select('*')
->join('commande', 'commande.id_cli', '=', 'clients.id')
->where("clients.id",$client_id) // put your condition here
->get();

Jquery datatable Invalid Argument exception - message - Trailing data on Laravel eloquent raw query

I have setup Laravel Yajra datatable backend package to handle the work for the ajax query calls to view via a jquery datatable. Everything worked great until I changed the eloquent query from:
static function for_table($farm_ids)
{
return ChemProgramUser::select('chemprogramuser.id', 'chemprogramuser.farms_id as fid',
'chemprogramuser.description as head', 'chemprogramuser.hectares as ha',
'chemprogramapp.updated_at',
'farms.name as fname', 'users.name', 'users.id as uid', 'chemprogramuser.type')
->leftJoin('farms', 'farms.id', '=', 'chemprogramuser.farms_id')
->leftJoin('users', 'users.id', '=', 'chemprogramuser.manager_id')
->groupBy('chemprogramuser.id')
->orderBy('chemprogramuser.id', 'desc')
->where('chemprogramuser.program', '=', 1)
->whereIn('chemprogramuser.farms_id', $farm_ids)
->get();
to
static function for_table($farm_ids)
{
return ChemProgramUser::select('chemprogramuser.id', 'chemprogramuser.farms_id as fid',
'chemprogramuser.description as head', 'chemprogramuser.hectares as ha',
DB::raw('GREATEST(chemprogramuser.updated_at, COALESCE(chemprogramapp.updated_at, \'1000-01-01\')) AS updated_at'), // this raw query caused the error
'farms.name as fname', 'users.name', 'users.id as uid', 'chemprogramuser.type')
->leftJoin('farms', 'farms.id', '=', 'chemprogramuser.farms_id')
->leftJoin('users', 'users.id', '=', 'chemprogramuser.manager_id')
->leftJoin('chemprogramapp', 'chemprogramapp.programuser_id', '=', 'chemprogramuser.id')
->groupBy('chemprogramuser.id')
->orderBy('chemprogramuser.id', 'desc')
->where('chemprogramuser.program', '=', 1)
->whereIn('chemprogramuser.farms_id', $farm_ids)
->get();
I am selecting the GREATEST date from two tables using the DB facade's raw method. As soon I changed the query I received a jquery InvalidArgumentException with message Trailing data. But this is just on the production server, not on my localhost.
Can someone please give an idea why this error will occur?
Laravel version: 5.6.39
So I found my answer, so I am posting it. It seems it is the way I called the DB::raw() function on the query that caused the error. The raw() function should incapsulate the complete select query string i.e.
select(
DB::raw(
' chemprogramuser.id, chemprogramuser.farms_id as fid, ' .
' chemprogramuser.description as head, chemprogramuser.hectares as ha, ' .
' GREATEST(chemprogramuser.updated_at, COALESCE(chemprogramapp.updated_at, \'1000-01-01\')) AS updated_at, ' .
' farms.name as fname, users.name, users.id as uid, chemprogramuser.type '
)
)
The query then for some reason causes no error, even if the error is something obscure as InvalidArgumentException with message Trailing data.

Trying to get property of non-object error in Laravel when assigning an ID to a variable

I'm getting the row in the following code :
$grpusrow = DB::table('grpusrs')
->where('group_id', $id)
->where('user_id', $u_id)
->get();
And then getting the ID in this line :
$grpusrid = $grpusrow->id;
I receive the following error on the line in number 2 :
Trying to get property of non-object
->get() will always return multiple objects at once. You're simply not telling it which index of $grpusrow to get the ID from.
$grpusrows = DB::table('grpusrs')
->where('group_id', $id)
->where('user_id', $u_id)
->get();
foreach ($grpusrows as $grpusrow) {
dump($grpusrow->id);
}
// Or
$grpusrow = DB::table('grpusrs')
->where('group_id', $id)
->where('user_id', $u_id)
->first();
dump($grpusrow->id);
Maybe 2 solutions:
If you want a unique row in return (with a primary key in arguments for example), you should try ->first() instead of ->get().
The problem is the using of where clauses, I think it works with array when you want more than one where clause, try like this :
->where([['group_id', '=', $id],['user_id', '=', $u_id]])->get(); ( or first() )

How to add a where clause when using "with" on an Eloquent query in Laravel

I have a query built where I'm using "with" to include related models. However, I'm not sure how to filter those related models in a where clause.
return \App\Project::with("projectLeaders")->join('companies', 'company_id', '=', 'companies.id')
->join('project_status', 'project_status.id', '=', 'projects.status_id')
->select('companies.*', 'project_status.name AS statusName', 'projects.*');
Please note the with("projectLeaders") in the query. So, ProjectLeaders is a relation that brings objects of kind Employee, how can I filter in that query those "Employees" whose attribute "Lastname" is like "Smith" ?
You can implement where class both tables. Please check following code and comments.
return \App\Proyecto::with(["projectLeaders" => function($query){
$query->where() //if condition with inner table.
}])->join('empresas', 'id_empresa', '=', 'empresas.id')
->join('tipo_estado_proyecto', 'tipo_estado_proyecto.id', '=', 'proyectos.id_tipo_estado_proyecto')
->where() //if condition with main table column.
->select('empresas.*', 'tipo_estado_proyecto.nombre AS nombreEstadoProyecto', 'proyectos.*');
You can use Closure when accessing relation using with. Check below code for more details:
return \App\Project::with(["projectLeaders" => function($query){
$query->where('Lastname', 'Smith') //check lastname
}])->join('companies', 'company_id', '=', 'companies.id')
->join('project_status', 'project_status.id', '=', 'projects.status_id')
->select('companies.*', 'project_status.name AS statusName', 'projects.*');
You may use the where method on a query builder instance to add where clauses to the query. The most basic call to where requires three arguments. The first argument is the name of the column. The second argument is an operator, which can be any of the database's supported operators. Finally, the third argument is the value to evaluate against the column.
return \App\Project::with("projectLeaders")->join('companies', 'company_id', '=', 'companies.id')
->join('project_status', 'project_status.id', '=', 'projects.status_id')
->where('lastname','=','Smith')
->select('companies.*', 'project_status.name AS statusName', 'projects.*');
Don't forget to return results with a get();
The query you have written is correct. But after building the query you need to fetch the data from database.
METHOD ONE
So adding get() method to your query:
return App\Project::with('projectLeaders')
->leftJoin('companies', 'company_id', '=', 'companies.id')
->leftJoin('project_status', 'project_status.id', '=', 'projects.status_id')
->select('companies.*', 'project_status.name AS statusName', 'projects.*')
->get();
METHOD TWO (with pagination)
return App\Project::with('projectLeaders')
->leftJoin('companies', 'company_id', '=', 'companies.id')
->leftJoin('project_status', 'project_status.id', '=', 'projects.status_id')
->select('companies.*', 'project_status.name AS statusName', 'projects.*')
->paginate(3);

Laravel Eloquent query returning only one row from the database table

I made this code from a project I'm currently developing at work. The problem I'm having is that this code:
php
public function getAllEstaciones(Request $request){
$estaciones = Estacion::select('IdEstacion', 'proyectoplanning.NombreProyectoPlanning', 'modeloequipo.NombreModeloEquipo',
'tipoinstalacion.NombreTipoInstalacion', 'estacion.IdEstadoGeneral', 'estadogeneral.NombreEstadoGeneral',
'mediotransmision.NombreMedioTransmision', 'propietarioestacion.NombrePropietarioEstacion', 'centropoblado.NombreCentroPoblado',
'distrito.NombreDistrito', 'provincia.NombreProvincia', 'departamento.NombreDepartamento', 'tiposervicio.NombreTipoServicio', 'estacion.IdBtsEstacion',
'btsestacion.Direccion', 'CodigoPresupuestalPlanning', 'CodigoUnicoPlanning', 'Prioridad', 'LatitudPlanning', 'LongitudPlanning',
'distrito.NombreDistrito', 'provincia.NombreProvincia', 'departamento.NombreDepartamento', 'tiposervicio.NombreTipoServicio',
'estacion.IdBtsEstacion', 'btsestacion.Direccion', 'CodigoPresupuestalPlanning', 'CodigoUnicoPlanning', 'Prioridad',
'LatitudPlanning', 'LongitudPlanning')
->join('proyectoplanning', 'proyectoplanning.IdProyectoPlanning', '=', 'estacion.IdProyectoPlanning')
->join('modeloequipo', 'modeloequipo.IdModeloEquipo', '=', 'estacion.IdModeloEquipo')
->join('tipoinstalacion', 'tipoinstalacion.IdTipoInstalacion', '=', 'estacion.IdTipoInstalacion')
->join('estadogeneral', 'estadogeneral.IdEstadoGeneral', '=', 'estacion.IdEstadoGeneral')
->join('mediotransmision', 'mediotransmision.IdMedioTransmision', '=', 'estacion.IdMedioTransmision')
->join('propietarioestacion', 'propietarioestacion.IdPropietarioEstacion', '=', 'estacion.IdPropietarioEstacion')
->join('centropoblado', 'centropoblado.IdCentroPoblado', '=', 'estacion.IdCentroPoblado')
->join('distrito', 'distrito.IdDistrito', '=', 'centropoblado.IdDistrito')
->join('provincia', 'provincia.IdProvincia', '=', 'distrito.IdProvincia')
->join('departamento', 'departamento.IdDepartamento', '=', 'provincia.IdDepartamento')
->join('tiposervicio', 'tiposervicio.IdTipoServicio', '=', 'estacion.IdTipoServicio')
->join('btsestacion', 'btsestacion.IdBtsEstacion', '=', 'estacion.IdBtsEstacion')
->get();
return response()->json($estaciones);
}
This is a query I built using the Eloquent module form Laravel. It's only returning one row from the table "Estaciones". I tried doing $estaciones = DB::table("estacion") with no success. I'd be grateful if you guys could give me some insight.

Categories