How to set the following model so that the largest content_id appears ?
$this->db->where('content_id', 'the largest content_id');
public function call_last_content()
{
$this->db->order_by('content_id','asc');
$this->db->where('content_id', 2);
$query = $this->db->get('content');
return $query->result_array();
}
Change your function as
public function call_last_content()
{
$this->db->order_by('content_id','DESC');
$query = $this->db->get('content');
return $query->row_array(); //returns the row of values of largest content_id as array format
}
Then call your model's function and get column values like this..
$data = $this->model_name->call_last_content();
$column_name = $data['column_name '];
Related
My Product Model is
public function IncomeRepo(){
return $this->hasMany(Income::class,'Product');
}
My Income Report Model Is
public function ProductData(){
return $this->belongsTo(Product::class,'Product','id');
}
My Query is
public function SearchIncomeData(Request $request){
$GetFromDate = $request->FromDate;
$GetToDate = $request->ToDate;
$ProductData = Product::with('IncomeRepo')->whereBetween('created_at', [$GetFromDate, $GetToDate])->get();
return view('Admin.Report.ProductSalesReport',compact('ProductData'));
}
When I return $ProductData it return products table created_at Data. BUT I nee Income table created_at data which be multiple
How can I get it?
My expectation show incomes table created_at data
If you want to filter data by child tables date then you need to use whereHas relationship.
$GetFromDate = $request->FromDate;
$GetToDate = $request->ToDate;
$ProductData = Product::with('IncomeRepo')
->whereHas('IncomeRepo',function($que) use($GetFromDate,$GetToDate) {
$que->whereBetween('created_at',[$GetFromDate, $GetToDate])})->get();
return view('Admin.Report.ProductSalesReport',compact('ProductData'));
In controller. (Use whereBetween)
public function SearchIncomeData(Request $request)
{
$GetFromDate = $request->FromDate;
$GetToDate = $request->ToDate;
$ProductData = Product::with(['IncomeRepo' => function ($query) use ($GetFromDate, $GetToDate)
{
$query->whereBetween('created_at', [$GetFromDate, $GetToDate]);
}
])->get();
return view('Admin.Report.ProductSalesReport', compact('ProductData'));
}
In view
foreach ($ProductData as $product)
{
foreach ($product->IncomeRepo as $income)
{
echo $income->created_at;
}
}
I'm having trouble merging two query results into an array - the merged output contains all the records of one of the queries ($factsheets) but only the last record of the other ($actives) where there are usually at least 3 records "available" to return.
My controller's code is as follows:
public function show($pest)
{
$theactives = self::getActives($pest);
$thefactsheets = self::getFactsheets($pest);
$merged = $theactives->merge($thefactsheets);
$result = $merged->all();
return $result;
}
public function getActives($pest){
$actives = Active::where('pests.id',$pest)
->join("active_pest","actives.id","=","active_pest.active_id")
->join("pests","pests.id","=","active_pest.pest_id")
->select('ai', 'groupcode', 'risk', 'pest')
->orderBy('ai')
->get();
return $actives;
}
public function getFactsheets($pest){
$factsheets = Factsheet::where('pest_id',$pest)
->join("factsheet_pest","factsheets.id","=","factsheet_pest.factsheet_id")
->select('title', 'factsheets.id')
->orderBy('title')
->get();
return $factsheets;
}
Again, my expectation has exceeded my ability - what am I doing wrong here?
you can't merge objects of the result set. So, you have to convert your result into array first before the merge. Try the below script.
public function show($pest)
{
$theactives = self::getActives($pest);
$thefactsheets = self::getFactsheets($pest);
return array_merge($theactives, $thefactsheets);
}
public function getActives($pest){
return Active::where('pests.id',$pest)
->join("active_pest","actives.id","=","active_pest.active_id")
->join("pests","pests.id","=","active_pest.pest_id")
->select('ai', 'groupcode', 'risk', 'pest')
->orderBy('ai')
->get()->toArray();
}
public function getFactsheets($pest){
return Factsheet::where('pest_id',$pest)
->join("factsheet_pest","factsheets.id","=","factsheet_pest.factsheet_id")
->select('title', 'factsheets.id')
->orderBy('title')
->get()->toArray();
}
You can use union in laravel
example
$silver = DB::table("product_silver")
->select("product_silver.name"
,"product_silver.price"
,"product_silver.quantity");
$gold = DB::table("product_gold")
->select("product_gold.name"
,"product_gold.price"
,"product_gold.quantity")
->union($silver)
->get();
then
dd($gold);
Hello I have those 4 tables:
Products (id, name)
Params (id, name)
Product_Param (id, product_id, param_id)
Values (product_param_id, value)
I can get all product params:
public function params() {
return $this->belongsToMany('App\Models\Param', 'product_param')->withPivot('id', 'type1_id', 'type2_id');
}
Code:
foreach($products->params as $param) {
var_dump($param)
}
And now I need to get param value.
I wrote stupid code like this:
public static function getPPV($product_id, $param_id) {
$value = new \App\Models\ParamValue();
$value->value = NULL;
$value->id = NULL;
$pp = \App\Models\ProductParam::where('product_id','=',$product_id)->where('param_id','=',$param_id)->first();
if (!$pp) return $value;
$ppv = \App\Models\ProductParamValue::where('product_param_id','=',$pp->id)->first();
if (!$ppv) return $value;
$value->value = $ppv->productvalue->value;
$value->id = $ppv->productvalue->id;
return $value;
}
It works but I want use better solution with ORM. Param model:
public function val() {
$p = $this->hasOne('App\Models\ProductParam')->orderBy('id','desc')->where('product_id','=',$this->product_id);
$ppv = $p->getResults()->hasOne('App\Models\ProductParamValue');
$val = $ppv->getResults()->belongsTo('App\Models\ParamValue','param_value_id');
return $val;
}
But in Param Model I can't get Product ID ->where('product_id','=',$this->product_id) (we have Product_Param (id, product_id, param_id) and I get all rows from Product_Param.
Please help to write ORM.
This may be not what you're looking for, but have you tried implementing the value column in the Values table to be its own column in the Product_Params table? Then you could access each of the values by saying:
foreach($products->params as $param) {
var_dump($param->pivot->value);
}
This is my controller
function index($id=NULL)
{
$this->load->model('article');
$id= $this->uri->segment(3);
$data = array('article' => $this->article->show_a_article($id),
'sidebar' => $this->article->side_bar(9),
'related'=> $this->article->related_keyword($article_title),
);
echo $article->News_News_ID;
$this->load->view('page/baiviet',$data);
}
I want to get article_title value from table article and put it in related_workword() function.
Update model The show a article model is to get article detail. The second model is to get all article related to the chosen article.
function show_a_article($id)
{
$query= $this->db->get_where('news_news', array('News_News_ID'=>$id));
return $query->row();
}
function related_keyword($rkey)
{
$sql="SELECT `News_News_ID`,`News_News_Headline`,`News_News_thumb1` FROM `news_news` WHERE `news_tags` like '%$rkey%' ORDER BY `News_News_ID` desc LIMIT 10";
$query= $this->db->query($sql);
return $query->result();
}
Update: Igot it
echo $this->article->show_a_article($id)->News_News_Headline;
Call model method from controller
$article_title = $this->article->get_article_details($id)->article_title;
Add function in model to return data.
function get_article_details($id)
{
$query = 'select article_title from table where col = '.$this->db->escape($id);
return $this->db->query($query)->row();
}
If I get sum of a column using Model::sum('column') , How can I get sums of multiple column returned in an array?
Using sum() for multiple columns is inefficient because it will trigger multiple queries. Add this to your model:
public static function sums($columns){
$instance = new static;
$columns = is_array($columns) ? $columns : func_get_args();
$selects = array_map(function($column){
return DB::raw('SUM('.$column.') AS '.$column);
}, $columns);
return $instance->select($selects)->first();
}
Usage:
Model::sums(array('column', 'foo', 'bar'));
// or
Model::sums('column', 'foo', 'bar');
Can you just do
$total = Model::sum('column1') + Model::sum('column2') + Model::sum('column3');
Edit:
You could just make a function in your Model
class Model extends Eloquent {
public static function getColumn($column_name) {
return Model::sum($column_name);
}
}
Then in your view you just do
$value->getSum('column');