The following is the get(); query :
$zero_others = DB::table('postusrs')
->where('post_id', $p_id)
->where('user_id','!=', $op_id)
->where('highted','=', 1)
->get();
Here I will fill the array (which I believe is wrong, and I need to know the correct syntax)
$zero_id = $zero_others->id;
And lastly, I will update the group of IDs received in $zero_id into the DB :
DB::table('postusrs')->where('id', $zero_id)->update(['highted' => 0]);
I'm seeing an error of: Trying to get property of non-object. Please advise how to fill the array ?
If you want to collect an array of ids, in Laravel 5.2. Try this:
$zero_ids = DB::table('postusrs')
->where('post_id', $p_id)
->where('user_id','!=', $op_id)
->where('highted','=', 1)
->pluck('id');
But you have to change the update query using whereIn:
DB::table('postusrs')->whereIn('id', $zero_ids)->update(['highted' => 0]);
Related
I have the following record in my database where I have to get all records containing any of the records.
Table record contains:
$record_ids = ["123","234","111"]
Trying to search for it like the following with Laravel:
$records = DB::table('records_table')
->where('id', $id)
->whereRaw('JSON_CONTAINS(record_id, ?)', $record_ids)
->pluck('records');
I've also tried with similar solutions like:
->whereIn('record_id', $record_ids)
I'm using Laravel 5.5 and Mysql.
Update:
It works when I 'manually' add it like the following:
->whereIn('record_id', ["123","234","111"])
But when fetching the array from the database it doesn't work
I think you want to use here:
->whereIn('record_id', json_decode($json, true));
The types have to match (integer vs. string) and the search value has to be JSON encoded:
$search = json_encode(array_map('intval', $record_ids));
$records = DB::table('records_table')
->where('id', $id)
->whereRaw('JSON_CONTAINS(record_id, ?)', $search)
->pluck('records');
In Laravel 5.6, you can use whereJsonContains():
->whereJsonContains('record_id', array_map('intval', $record_ids));
When building a query to find multiple values from a model, eloquent would look something like this:
return Contact::whereIn('user_name', explode(',', $userNames)
But let's say that the value for userNames is ['foo', 'bar']; but I only have foo as a valid username, is there a way to get bar (all failed to find) out as part of the same query without having to compare the result ->get() against the request?
It's not possible to get the query to return the list of username that doesn't exist from the given condition. But you could do this.
$allUserNames = explode(',', $userNames);
$validUserNames = Contact::whereIn('user_name', $allUserNames)
->pluck('user_name')
->toArray();
$invalidUserNames = array_values(array_diff($allUserNames, $validUserNames));
You can use array diff to remove the unvalid value form the select region, and you lost the ->get() to get the results;
return Contact::whereIn('user_name', array_diff(explode(',', $userNames, ['bar']))->get()
I'm having a problem getting some datas from my database via eloquent:
at first I do this to get 'id' and 'capMax' from clase_schedule table:
$plazas = DB::table('clase_schedule')->select(['schedule_id', DB::raw('SUM(capMax) as capMax')])->groupBy('schedule_id')->get();
and then i want to get the the name of table schedule where the id i got before coincides with the one on this table, like this:
$nom_horarios = DB::table('schedule')->select('name')->where('id', 'in', $plazas->schedule_id)->get();
I get this error:
"Trying to get property of non-object"
Since, $plazas is an array so you should loop the $plazas to get the value in next query as below :
foreach ($plazas as $plaza)
{
$nom_horarios[] = DB::table('schedule')->select('name')->where('id', 'in',$plaza->schedule_id)->get();
}
print_r($nom_horarios);
$plazas returns an array of items, instead of get(), use first(). This will return single item.
$plazas = DB::table('clase_schedule')->select(['schedule_id', DB::raw('SUM(capMax) as capMax')])->groupBy('schedule_id')->first();
Or loop plazas in foreach
What is returning in the $plazas variable?
I think the problem is you are getting a collection and not a item... If you want to get just one plaza you should ->first() instead of ->get();
This is my Laravel controller code
public function switchInfo($prisw, $secsw){
$cur_sw_pair_id = DB::table('sw_pairs')
->select('sw_pair_id')
->where('pri_sw','=',$prisw)
->where('sec_sw','=',$secsw)
->get();
$infolist = DB::table('cust_sw_pair')
->select('interface','pri_sw_vlan','sec_sw_vlan','pri_sw_admin_status','sec_sw_admin_status','description')
->where('sw_pair_id', '=', $cur_sw_pair_id)
->get();
return view('switchinfo.switchinfoview',compact('infolist'));
}
when this code executing it gives Object of class stdClass could not be converted to string error. How can i solve this? How can i pass my infolist into switchinfoview view without getting this error?
You can use pluck() to get value of a column.
Your first query should like this:
$cur_sw_pair_id = DB::table('sw_pairs')
->where('pri_sw','=',$prisw)
->where('sec_sw','=',$secsw)
->pluck('sw_pairs');
Keep the second query as it is.
Hope it will help.
The issue is that ->get() returns a collection, which is a 0-indexed collection of results (rows) from the database. In your case, you want to be using the following query:
$cur_sw_pair_id = DB::table('sw_pairs')
->select('sw_pair_id')
->where('pri_sw','=',$prisw)
->where('sec_sw','=',$secsw)
->first();
And then accessing the property sw_pair_id of $cur_sw_pair_id, like so:
$infolist = DB::table('cust_sw_pair')
->select('interface','pri_sw_vlan','sec_sw_vlan','pri_sw_admin_status','sec_sw_admin_status','description')
->where('sw_pair_id', '=', $cur_sw_pair_id->sw_pair_id)
->first();
Then, in your view, you can access all the properties (anything in the ->select() statement) of $infolist using $infolist->PROPERTY_HERE
Note that using ->first() is essentially using the same as ->get();, but you have to access the results of ->get() using an index, so
$cur_sw_pair_id[0]->sw_pair_id
Hope that provides some insight for you.
I get some values from laravel query builder. I would like to get and set these values into somewhere of my code. So i get undefined index exception message with the following codes :
laravel query :
$generatedRowValue = DB::table('myTableName')
->where('id', '=', 'someValue')
->where('anyOtherValue', '=', 'anotherValue')
->get();
I want to reach the values set in $generatedRowValue variable.
P.S. : Yeah i can get the values from the query, checked it out with var_dump(); dd(); etc. and i know my values are not undefined.
I'm new on this platform so i don't know how to reach these values.
Any help would be appreciated.
---UPDATE---
This is a simple HTML file which includes php partially.
<?php
$html = new simple_html_dom();
$html->load('<input type="text" value="'.$generatedRowValue['thevalueName'].'">')
?>
So i need $generatedRowValue['thevalueName'] 's value.
get() returns an array of objects. I think you want to use first() instead since you only expect one result.
$generatedRowValue = DB::table('myTableName')
->where('id', '=', 'someValue')
->where('anyOtherValue', '=', 'anotherValue')
->first();
By the way, you can also use the object syntax in your view (if you want)
$generatedRowValue->thevalueName