Laravel multiple function in a view - laravel

i need help, i'm new in laravel, i want to make query to get year only from table like this
public function index() {
$items = Items::distinct()->orderBy('date','desc')->select(DB::raw('YEAR(`date`) as date'))->get();
$itemDet = Items::where('date', $items->date);
}
then i want to show other value from table based on year above with this function
public function itemDetail($year) {
$itemDet = Items::where('YEAR(`date`)', $year);
return $itemDet;
}
But i don't know how to call it in view, i only know how to call the first function in a view, and I don't know how to pass value year from first function as parameters to second function.
2018
- item2018
- item2018
- item2018
2017
- item2017
2016
-item2016
-item2016
sorry for my bad grammar, thank you!

public function itemDetail($year) {
$itemDet = Items::where('YEAR(`date`)', $year);
//return $itemDet; change it to
return view('yourViewPath',compact('itemDet'));
}
in view you can access like this way
{{ $itemDet }}

You can get that output right away in your index function without passing it to second function, one way to do it.
public function index() {
$arr_container = []; //create array container
$items = Items::distinct()->orderBy('date','desc')->select(DB::raw('YEAR(`date`) as date'))->get();
foreach($items as $val){
$itemDet = Items::select(DB::raw('item'))->where('YEAR(`date`)', $val->year)->get();
if($itemDet->count()>0){
$arr_container[$val->year][] = $itemDet[0]->item;
}
}
return view('view')->with('years',$arr_container);
}
You should get this output when you call {{$years}}.
2018
- item2018
- item2018
- item2018
2017
- item2017
2016
-item2016
-item2016

Related

What does the Object of class stdClass could not be converted to int indicate

I did some math operation trying to deduct one variable from another variable but fails to give me a results say 30 - 10 returns an error. If I change ->first('leave_entitlement'); to ->sum('leave_entitlement'); and able to do the deduction. However I am not summing leave_entitlement. How to I do it right? please
Leave Model function
public static function getSumOfLeaveTaken($leave_category_id){
$total_leave_taken = DB::table('leaves')
->where('created_by', \Auth::user()->id)
->where('leave_category_id', $leave_category_id)
->sum('num_days');
return $total_leave_taken ;
}
LeaveBalance Model function
public static function getNumOfLeaveEntitlment(){
$leaveEntitlement = DB::table('salary_level_user')
->where('user_id', \Auth::user()->id)->first('leave_entitlement');
return $leaveEntitlement;
}
Inside my Leave Controller
$total_leave_taken = Leave::getSumOfLeaveTaken($request->leave_category_id);
$leaveEntitlement = LeaveBalance::getNumOfLeaveEntitlment();
$total_leave_balance = (int)$leaveEntitlement - (int)$total_leave_taken ;
Solved:
Replace ->first with ->value to return a single value and there you can do the deductions
->first('leave_entitlement')
replace with
->value('leave_entitlement')

How do I return records by using chunk()?

I'm using chunk() to find a value from the database based on the question. But how do I return the entityFound[] array?
$chunkRecord = $this->company()->entityValues()->chunk(5,
function ($entityValues) use ($entityFound, $question) {
foreach ($entityValues as $entityValue)
if (str_contains($question, $entityValue->name))
$entityFound[] = $entityValue->name;
return $entityFound;
});
// I want to run this array and do something else.
dd($entityFound);

How to call information from one model to another Codeigniter

I'm stuck on this from a while.Can't figured it out.I reed documantion, tried with several videos and tried like 10 different ways, nothing is working yet.So I have one view/model for one thing, in this example Destination and I have separate files for Offers.The controllers for both are in one file.I want tho the information that is in destination to go to Offers as well.Please help I can't figure out what I'm missing:
So here is the most important parts:
destination_model.php
<?php class Destination_model extends CI_Model
{
public function getDestinationDetails($slug) {
$this->db->select('
id,
title,
information
');
$this->db->where('slug', $slug);
$query = $this->db->get('destinations')->row();
if(count($query) > 0)
{
return $query;
}
else
{
// redirect(base_url());
}
}
public function getOffersByDestination($destination_id)
{
$this->db->select('
o.short_title,
o.price,
o.currency,
o.information,
o.long_title_slug,
oi.image,
c.slug as parent_category
');
$this->db->from('offers o');
$this->db->join('offers_images oi', 'oi.offer_id = o.id', 'left');
$this->db->join('categories c', 'c.id = o.category');
$this->db->group_by('o.id');
$this->db->where('o.destination', $destination_id);
$this->db->where('o.active', '1');
return $this->db->get();
} }
And then in the controller for offers I put this:
$this->load->model('frontend/destination_model');
$this->params['destination'] = $this->destination_model->getOffersByDestination($data->id);
All I need is the title and the information about the destination.
Here is the whole controller for the offers:
$data = $this->slugs_model->getOfferDetails(strtok($this->uri->segment(2), "."));
$this->load->model('frontend/offers_model');
$this->load->model('frontend/destination_model');
$this->params['main'] = 'frontend/pages/offer_details';
$this->params['title'] = $data->long_title;
$this->params['breadcumb'] = $this->slugs_model->getSlugName($this->uri->segment(1));
$this->params['data'] = $data;
$this->params['images'] = $this->slugs_model->getOfferImages($data->id);
$this->params['similar'] = $this->slugs_model->getSimilarOffers($data->category, $data->id);
$this->params['destination'] = $this->destination_model->getOffersByDestination($data->id);
$this->params['offers'] = $this->offers_model->getImportantOffers($data->offers, $data->category, $data->id);
You need to generate query results after you get it from model,
e.g: row_array(), this function returns a single result row.
here's the doc: Generating Query Results
try this:
$this->load->model('frontend/destination_model');
$data['destination'] = $this->destination_model->getOffersByDestination($data->id)->row_array();
$this->load->view('view_name', $data);
And in your view echo $destination['attribut_name'];,
or you can print the array, to see if it's work print_r($destination);

Difficulties with scope in Laravel

I'm trying to build an application where there's a device with an expire date:
the expire date must be created with values from 2 other tables: history (last date) & model (interval)
I've create following scope in my Device model:
public function scopeExpiryDate($query, $operator) {
if ($this->histories->last() != null) {
$cal_date = $this->histories->sortBy('date')->last()->date;
$interval = $this->product->interval;
return $query->where($cal_date->addDays($interval), $operator, Carbon::now());
}
return $query;
}
Then I could use the next line:
$devices->ExpiryDate('<=')->get();
Update
like #ceejayoz told in the comments I checked the scopes section on the Laravel-site.
I edited this question.
So how do I get this scope working?
Assuming that $cal_date is a Carbon\Carbon instance, you can use this query:
$query->whereDate($cal_date->addDays($interval), $operator, Carbon::now());
For more information, there's a great tutorial on date filtering here
Ok i've found the solution. It was not found in the scopes, but in adding a attribute.
I did the following in my Device model:
public function getExpirydateAttribute() {
if ($this->histories->last() != null) {
$cal_date = $this->histories->sortBy('date')->last()->date;
$interval = $this->product->interval;
return $cal_date->addDays($interval);
}
return Carbon::now();
}
and in my view I can call something like this:
$devices->filter(function($item){ return $item->Expirydate > now()->modify('last day of this month'); })->count()
Thanks to the people who answered to my question.

view does not appear, only data appears

i want to passing data to view
my controller
public function create()
{
$maxcode=Product::selectRaw('MAX(RIGHT(product_code, 3)) as max')->first();
$prx='P2018-';
if($maxcode->count()>0)
{
$tmp = ((int)$maxcode->max)+1;
$newcode = $prx.sprintf("%03s", $tmp);
}
else
{
$newcode = $prx."P2018-001";
}
return $newcode;
return view('product.create', $newcode);
}
my view
...other code...
#foreach($newcode as $nc)
{{$nc->newcode}}
#endforeach
---other code...
the code works well, but the only results appear, the other code on the view does not work.
someone might be able to help me
remove return $newcode;
return view('product.create')->with('newcode',$newcode);
$newcode variable is not an array, you can not use foreach.
use {{$newcode}} in blade to display value of variable.
remove return $newcode; line and change your return function like this,
return view('product.create', compact('newcode'));

Resources