May be is simple but I have one table where I want to take value from one column. They are two records and always will be two records and I need value from second id.. Here is visual
id key value
1 key_1 Value_1
2 key_2 value_2
I want to take value_2. Here is what I have in my controller
$free = DB::table('settings')->select('value')->where('key', '=', 'free')->get();
return View::make('site.cart.order', [
'cart' => $cart,
'free' => $free
]);
And in my view I'm trying this
#if( $total < $free['value'] )
// loop
#endif
Currently I'm getting - Undefined index: value
If I'm right Laravel returns a standard class there and you should access it like this.
$free->value
Instead of
$free['value']
But you have to change now it selects the first record
$free = DB::table('settings')->select('value')->where('key', '=', 'free')->first();
Then you can access it like
$free->value
Or if you don't want to change get() to first() you can access it like this because get() returns an array.
$free[0]->value
Use first() instead of get() and use $free->value, get() return an array of object
Related
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]);
I got an error in
Call to a member function first() on double
this is query builder in laravel
$player = DB::table('players')
->join('stats', 'players.username', '=', 'stats.player')
->sum('stats.winpot');
->first();
of course. it's obvious. the sum() function return sum of the selected column. a single value (not a collection of values)
the first() is for Collections, to get the first element from a collection.
Since the sum function returns the sum of the column values (A single value, not a Collection), you cant call first() function on it.
And Most importantly You Don't Have To.
NOTE
If you want to get both 'player' and the 'sum' you can execute two queries and bind those two results together.
Possible solution.
$player = DB::table('players')
->join('stats', 'players.username', '=', 'stats.player')
->first();
$player->sum = DB::table('players')
->join('stats', 'players.username', '=', 'stats.player')
->sum('stats.winpot');
now there is an attribute in retrieved 'player' as 'sum'. you can access it as $player->sum
today i was just practicing my skills and got stuck. I just want to fetch data from the database using specific ids which i put inside my session as an array. The problem is, i cant find a way to fetch all the data that includes these ids.
here's my cart checkout method
public function checkout(){
$uniqid = session('products');
$post=Post::find($uniqid);
return view('pages.checkout')->with('post',$post);
}
I am getting no data, even if i try to get something i only get one field when there's something like 7 of them.
Here's the session products array
0 "15be4aa3b55f10"
1 "15be4814ceb2a0"
2 "15be4814ceb2a0"
3 "15be4aa3b55f10"
4 "15be4aa3b55f10"
5 "15be4aa3b55f10"
6 "15be4aa3b55f10"
7 "15be4aa3b55f10"
The find query returns single row of data. If you want to fetch more than 1 data, use the all query:
$post = Post::all();
or if you want to get all data with the same id as your $uniqid, use the where query:
$post = Post::where('column_name', $uniqid)->get();
EDIT
IF your session has many id, you may want to loop and find all the data with the same id as your $uniqid
public function checkout(){
$products = [];
foreach(session('products') as $session){
$post=Post::find($session->id);
$products[] = $post;
}
return view('pages.checkout')->with('products ',$products);
}
you may try this:
public function checkout(){
$uniqid = session('products');
$products = Post::whereIn('unique_id', $uniqid)->get();
return view('pages.checkout')->with('products',$products);
}
where whereIn method takes a field name and array of values
From laravel docs:
The whereIn method verifies that a given column's value is contained within the given array
You should use whereIn() method to fetch records against multiple ids. do like this
public function checkout(){
$uniqidsArray= session('products');
$post=DB::table('yourTableName')
->whereIn('id', $uniqIdsArray)
->get();
return view('pages.checkout')->with('post',$post);
}
i am using laravel 5.1 and want to retrieve a single row in the database then manipulate it after.
my current code is
$profiles = Profile::where('user_id', $this->user->user_id)
->where('profile_code', $this->user->profile_code)
->get();
foreach($profiles as $profile ){
$data['address'] = $profile->address;
}
why cant i do it like this?
$profiles = Profile::where('user_id', $this->user->user_id)
->where('profile_code', $this->user->profile_code)
->get();
$data['address'] = $profiles->address;
am i using a wrong function or something?
thanks in advance.
Try this:
$data['address'] = $profiles[0]->address;
When you are using get(), it returns an array of Std class object.
In addition to retrieving all of the records for a given table, you may also retrieve single records using first. Instead of returning a collection of models, these methods return a single model instance:
// Retrieve the first model matching the query constraints...
$flight = App\Flight::where('active', 1)->first();
laravel 5.8
Retrieving A Single Row / Column From Profile
If you just need to retrieve a single row from the database table, you may use the first() method. This method will return a single stdClass object:
$Profile = DB::table('Profiles')->where('user_id', $this->user->user_id)
->where('profile_code', $this->user->profile_code)->first();
If you don't even need an entire row, you may extract a single value from a record using the value() method. This method will return the value of the column directly:
$address = DB::table('Profiles')->where('user_id', $this->user->user_id)
->where('profile_code', $this->user->profile_code)->value('address');
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();