I'm trying to retrieve single column from my table grades.
For that I have used following code in my controller:
public function verify($id,$sid)
{
$grade=Grade::all('annual')->whereLoose('id',$id);
return $grade;
}
Where, annual is column name. But it is returning empty set of array [].
all() takes a list of columns to load from the database. In your case, you're fetching only one column called annual, therefore filtering on id later on does not return results. Replace your code with the following and it should work:
$grade = Grade::all('id', 'annual')->whereLoose('id', $id);
Keep in mind that it will return a collection of objects, not a single object.
NOTE: you're always loading all Grade objects from the database which is not efficient and not necessary. You can simply fetch object with given id with the following code:
$grade = Grade::find($id); // fetch all columns
$grade = Grade::find($id, ['id', 'annual']); // fetch only selected columns
The code you are using is loading all rows from the grades table and filtering them in code. It is better to let your query do the filter work.
For the columns part, you can add the columns you need to the first() function of the query, like so:
public function verify($id,$sid)
{
$grade = Grade::where('id', $id)->first(['annual']);
return $grade->annual;
}
Related
In Controller,
public function detail($id)
{
$item = Item::find($id);
return view('frontend.detail',compact('item'));
}
In blade,
{{$item->subcategory->items}} //question //this code print all the items//
You need to select the items manually if you don't want to fetch the id
Currently, you are fetching all the items by using $item = Item::find($id); it means that fetch all the fields of the particular table againt the given id.
you can use below query by modifying you required fields
Item::select('name','surname')->where('id', 1)->get();
You Need to make hidden id of the column in query Example
Item::find($id)->makeHidden(['id']);
return view('frontend.detail',compact('item'));
I need to make pagination in Laravel, and as I read laravel documentation here is a several way to do it. but in my cases it is a little bit complexity. So let say I've two table in my DB, table1 and table2(many to many relationship), I want to paginate table1 with specific ids. So I mean if table1 contains ids 1,2,3,4,5 ... n, I want to paginate only rows which id is 2 and 3
I have tried :
private function check_wear($type){
$products_id = array(); //here is ids which $type is set as 'ON'
$wears = DB::select("SELECT prod_id FROM wears WHERE ".$type." = 'on' "); //Select specific products ids. (in our case it is **table1**)
foreach ($wears as $wr){
array_push($products_id,$wr->prod_id);
}
return $products_id; //return array with ids which should be paginate
}
public function hat($locale){
$hat_prod_ids = $this->check_wear('coat');
$p=12;
$products = DB::table('products')->paginate($p); // 12 per page
dd($products);
my code only paginate one table with all data, is there any built-in function to somehow write a logic condition? for pagination
If you need to filter your pagination with elements which you've in $hat_prod_ids, you're able to use WhereIn() method, that one checks if your elements (in our case ids) exist in your $hat_prod_ids and returns true or false
$products = DB::table('products')
->whereIn('id',$hat_prod_ids)
->paginate($p);
add that code and now you will be able to paginate only that ids which is matched in your array
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 have a situtaion where I have data in two columns in a table, which need to be multiplied on a row basis. Then each value from the multiplication has to be added to provide a net result
I tried this but it does not work
public function getCampaignStats($item)
{
$query = CampaignStats::where('item',$item);
foreach($query as $q) {
$q->p_c;
$q->caa;
dd($q->p_c);
}
return $query;
}
I get this exception when i try to do this
Object of class Illuminate\Database\Eloquent\Builder could not be converted to string
Is there a better way to do this foreach loop in laravel
You can use raw queries for such a case and do the math within your query. Have a look at the docs:
https://laravel.com/docs/5.4/queries#raw-expressions
https://laravel.com/docs/5.4/queries#where-exists-clauses
Hi I was able to get the values from two tables, now I want this two values to be subtracted. How can i do it here in laravel?
public function displayBalance()
{
$results= DB::table('accountspayable')
->selectRaw('sum(accountspayable.amount) as sum')
->where('accountspayable.regnum','=','15459')
->get();
$subjects= DB::table('pay')
->selectRaw('sum(pay.amount) as sum')
->join('accountspayable','pay.accountno','=','accountspayable.accountno')
->where('accountspayable.regnum','=','15459')
->get();
return View::make('users.Balance')->with(array('results'=>$results,'subjects'=>$subjects));
}
Try it this way:
public function displayBalance()
{
$results = DB::table('accountspayable')->
where('regnum'. '='. '15459')->
sum('amount');
$subjects = DB::table('pay')->
join('accountspayable','pay.accountno','=','accountspayable.accountno')->
where('accountspayable.regnum','=','15459')->
sum('pay.amount');
return View::make('users.Balance', compact('results', 'subjects'));
}
You were probably getting collections of 1 record having 1 attribute (sum). And that's the only thing you basically need. So you can use aggregate functions directly in your query builder to get the SUM values.
Also I used "compact" to assign the values you just retrieved to the view. This is not necessary but saves some code and is easier to read.