How to use condition in laravel query - laravel

Hello I am new to laravel. I am getting an error saying 'Property [pickupdate] does not exist on this collection instance.', after fowarding to 'overdue_pickup' view.
My Controller code
public function overdue_pickup(){
$id = "2";
$curr_date = date('m/d/yy');
$overdue_pickup = DB::table('archive_pickup')
->where('ridder_id_',$id)
->get();
if($overdue_pickup->pickupdate < $curr_date){
return view('overdue_pickup',['overdue_pickup' =>
$overdue_pickup]);
}else{
return "No Overdue Pickup";
}
}
My overdue_pickup view
<?php $i = 1; ?>
#foreach($overdue_pickup as $overdue_pickup)
<div class="col-md-offset-1 col-md-10">
<div class="box box-success">
<div class="box-header with-border">
<h3 class="box-title" style="padding:5px">Order No: <b><?php echo $i;?> {{$overdue_pickup->status}}</b></h3>
</div>
<div class="box-body">
<div class="col-md-offset-1 col-md-10">
<div style="overflow-x:auto;">
<p><i class="fa fa-dashboard"></i> <b>Pickup Time:</b> {{$overdue_pickup->pickuptime}}</p>
<p><i class="fa fa-dashboard"></i> <b>Delivery Time:</b> {{$overdue_pickup->deliverytime}}</p>
</div>
</div>
</div>
</div>
</div>
<?php $i++; ?>
#endforeach

Considering that your record for $id=2 is one,
Change
$assign_pickup = DB::table('archive_pickup')
->where('ridder_id',$id)
->get();
to first()
$assign_pickup = DB::table('archive_pickup')
->where('ridder_id',$id)
->first();
Then use
if($assign_pickup->pickdate > $curr_date){
return view('Ridder.assign',['assigned_pickup' => $assigned_pickup]);
}else{
return "No Overdue Pickup";
}
If not one then use foreach & no need to change get() statement,
foreach($assign_pickup as $assign_pick){
if($assign_pick->pickdate > $curr_date){
// ... your logic
}else{
// ..your logic
}
}
EDIT:
public function overdue_pickup(){
$id = "2";
$curr_date = date('m/d/yy');
$overdue_pickup = DB::table('archive_pickup')
->where('ridder_id_',$id)
->get();
return view('overdue_pickup',['overdue_pickup' =>
$overdue_pickup]);
}
In your view, you can just foreach loop through it,
#foreach($overdue_pickup as $overdue_pick)
#if($overdue_pick->pickdate > $curr_date)
{{-- show your view --}}
#else
<p class="text-warning">No Overdue Pickup</p>
#endif
#endforeach

Related

Need to call the function after submit the form using laravel

Need to call the the function after submit the form. In my controller contains two api calls.I need to call the first API and get the "sld" value from that and pass the "sld" value to second API Url after that execute the Second API. But i got a two api output at the same time. please suggest any solution to call the first api after that display the second API data.
My Controller Code
public function domaincheck(Request $request)
{
ini_set('max_execution_time', 300);
//set_time_limit(300);
$sld = $request['sld'];
$tld = $request['tld'];
$response = file_get_contents('https://reseller.enom.com/interface.asp?command=check&sld='. $sld .'&tld='. $tld .'&uid=resellid&pw=resellpw&responsetype=xml');
$data = simplexml_load_string($response);
$configdata = json_encode($data);
$final_data = json_decode($configdata,true);// Use true to get data in array rather than object
// dd($final_data);
$response1 = file_get_contents('http://reseller.enom.com/interface.asp?command=GETNAMESUGGESTIONS&uid=resellid&pw=resellpw&SearchTerm='. $sld .'&MaxResults=50&ResponseType=XML');
$data1 = simplexml_load_string($response1);
$configdata1 = json_encode($data1);
$final_data1 = json_decode($configdata1,true);// Use true to get data in array rather than object
//dd($final_data1);
}
My View Code
<form class="form-horizontal" method="get">
<div class="form-group">
<div class=" col-lg-2"></div>
<div class="col-lg-8">
<div class="input-group m-b">
<span class="input-group-addon" style="padding-left:10px; background-color: #999;" class='unclickable'>www</span>
<input type="text" name="sld" class="form-control">
<span class="input-group-addon">
<select class="form-control" name="tld" style="width: 100px;">
<?php $j = 0; ?>
#foreach($final_data2['tldlist']['tld'] as $value)
<?php $j++; ?>
#endforeach
#for ($i = 0; $i < $j-1;)
<option value="{{($final_data2['tldlist']['tld'][$i]['tld'])}}">{{($final_data2['tldlist']['tld'][$i]['tld'])}}</option>
<?php $i++; ?>
#endfor
</select>
</span>
<span class="input-group-addon">
<button type="submit" class="btn btn-sm btn-success" >Submit</button>
</span>
</div>
<p class="text-center text-dark customFont" >
#foreach($final_data as $key=>$value)
#if($key=='DomainName')
<b>{{$value}}</b> <b>-</b>
#endif
#if($key=='RRPText')
<b>{{$value}}</b>
#endif
#endforeach
</p>
#foreach($final_data1['DomainSuggestions']['Domain'] as $value)
{{$value}}<br>
#endforeach
Please suggest any solution to solve this issue
You should rather have store those two API calls as functions into separate folder under app directory. For example, under \app\Repositories directory you can create two separate php files and store the api calls as function. While doing this, please take class based approach. See following example, inside DomainSuggestion.php
<?php
namespace App\Repositories;
class DomainSuggestion
{
function getdomain(&$domainArray)
{
$response = file_get_contents('https://reseller.enom.com/interface.asp?command=check&sld='. $domainArray[0] .'&tld='. $domainArray[1] .'&uid=resellid&pw=resellpw&responsetype=xml');
$data = simplexml_load_string($response);
$configdata = json_encode($data);
$final_data = json_decode($configdata,true);
return $final_data;
}
}
and in your controller funtion
use App\Repositories\DomainSuggestion;
$sld = $request['sld'];
$tld = $request['tld'];
$domainArray = array($sld, $tld);
$dataObject = new DomainSuggestion();
$result = $dataObject->getdomain($domainArray);
Initialize the object of this class and call the function (inside your controller). The returned value from the first function can be utilized into the second call. You can also pass required parameter into the function. Don't forget to include the function directory into your controller (use App\Repositories\ClassName;)

Laravel Select the First Row From HasMany Relation

I have 2 table where 1 products have many prodphotos
I can retrieve all prodphotos from products with the same id, but my case is listing all the products but only take 1 photo from prodphotos.
Controller :
public function daftarproduk()
{
$produks = Product::orderBy('created_at', 'desc')->get();
$select = Product::with('prodphotos')->firstorfail();
$photo = $select->prodphotos->pluck('photo');
$kategori = Category::orderBy('created_at', 'asc')->get();
return view('guest.daftarproduk')
->with('produks',$produks)
->with('kategori',$kategori)
->with('select',$select)
->with('photo',$photo);
}
View :
#foreach($produks as $value)
<div class="col-xs-6 col-md-4 col-lg-3 box-product-outer">
<div class="box-product">
<div class="img-wrapper">
<a href="detail/{{$value->id}}/{{str_slug($value->name)}}">
<img alt="Product" src="images/gambarproduk/thumb_{{ i dont know what i must throw here to only get first picture }}">
</a>
</div>
<h6>{{$value->name}}</h6>
</div>
</div>
#endforeach
I dont know what function I must use to get the first photo name from $select or $photo from controller. or my foreach logic is wrong? Please help me.
Add a featured photo relation with hasOne type to your Product model.
Product model
public function featuredPhoto() {
return $this->hasOne(PhotosModel);
}
In your controller
public function daftarproduk()
{
// Get the products with featured image
$produks = Product::with('featuredPhoto')->orderBy('created_at', 'desc')->get();
$kategori = Category::orderBy('created_at', 'asc')->get();
return view('guest.daftarproduk')
->with('produks',$produks)
->with('kategori',$kategori);
}
View
#foreach($produks as $value)
<div class="col-xs-6 col-md-4 col-lg-3 box-product-outer">
<div class="box-product">
<div class="img-wrapper">
<a href="detail/{{$value->id}}/{{str_slug($value->name)}}">
<img alt="Product" src="images/gambarproduk/thumb_{{ $value->featuredPhoto->photo }}">
</a>
</div>
<h6>{{$value->name}}</h6>
</div>
</div>
#endforeach

Call to a member function tasks() on null on laravel 5

Let me explain with a couple of word my problem.
On my controller i have this line:
$tasks = $student->group->tasks()->orderBy('created_at', 'desc')->withPivot('id')->get();
This works for existing users, but when i try to create new ones i receive
Call to a member function tasks() on null
Can i with something like this or what do you suggest ?
if(!is_null($tasks))
$tasks = $student->group->tasks()->orderBy('created_at', 'desc')->withPivot('id')->get();
}
This i my show function on controller
public function show(){
$user = Auth::user();
if(!$user)
return redirect('/')->withErrors(config('constants.NA'));
$countries = Country::all()->lists('name', 'id')->toArray();
$profile = $user->profile;
$student = $profile->student;
// Tasks showed on students profile
if ($student->group) {
$tasks = $student->group->tasks()
->orderBy('created_at', 'desc')
->withPivot('id')
->get();
}
// Classmates
if ($student->group) {
$classmates = $student->group->students()
->where('id', '!=', $student->id)
->get();
}
// Activated books
$books = Auth::user()->books()->orderBy('grade_id', 'asc')->get();
if(!is_null($student->group))
$iTasks = $student->group->tasks->count();
else {
$iTasks = 0;
}
$iTodos = $user->todos->count();
return view('student.profile.show',compact('user','profile', 'countries', 'iTasks', 'iTodos', 'tasks', 'classmates', 'books'));
}
This is my show view, for the tasks
<div class="tab-pane <?php if(isset($tab) && $tab == 'timeline'): ?> active <?php endif; ?>" id="timeline">
#if($tasks->count())
<div class="timeline">
#foreach($tasks as $task)
<!-- TIMELINE ITEM -->
<div class="timeline-item">
<div class="timeline-badge">
<div class="timeline-icon">
<i class="mdi mdi-clipboard-text font-red-intense"></i>
</div>
</div>
<div class="timeline-body">
<div class="timeline-body-arrow"> </div>
<div class="timeline-body-head">
<div class="timeline-body-head-caption">
<span class="timeline-body-title font-blue-madison">
{{ $task->professor->profile->user->name }}
</span>
<span class="timeline-body-time font-grey-cascade">ju ca caktuar një detyrë të re.</span>
</div>
</div>
<div class="timeline-body-content">
<span class="font-grey-cascade">
{{ $task->pivot->comment }}
</span>
</div>
<hr>
Lenda: <span class="timeline-body-time font-grey-cascade sbold">{{ $task->subject->name }}</span>
<div class="pull-right">
Krijuar më: <span class="timeline-body-time font-grey-cascade sbold">{{ $task->created_at->format(Config::get('klasaime.date_format')) }}</span>
</div>
</div>
</div>
<!-- END TIMELINE ITEM -->
#endforeach
</div>
#else
<div class="alert">
Ju nuk keni asnje detyrë të caktuar!
</div>
#endif
</div>
Looks to me like you haven't added the student to a group yet, or that somehow hasn't been persisted. If you have added the student to a group after creating and saving the student, try this:
$student->load('group')
Before running:
$tasks = $student->group->tasks()->orderBy('created_at', 'desc')->withPivot('id')->get();
I'll need to see more of your code to give a more accurate answer. But the error you're getting is related to the ->group, of your student, not your student itself.
Do a simple check to see if the group exists and then carry on with whatever action you need to perform.
// controller
$tasks = null;
if ($student->group) {
$tasks = $student->group->tasks()
->orderBy('created_at', 'desc')
->withPivot('id')
->get();
}
// view
#if($tasks)
{{ $tasks->id }}
#else
No tasks found
#endif
Edit : In your controller add $tasks = null; and $classmates = null; at top. And in your view change #if($tasks->count()) to #if($tasks). I'm not sure where your use the classmates variable, but add a check to see if it's null before you use it.

how search between date using post in codeignitier

Dear Expert need Help first see my view code in codeigniter :
<div class="form-group">
<label for="tglawal" class="col-sm-2 control-label">Periode</label>
<div class="col-sm-3">
<div class="input-group date">
<div class="input-group-addon">
<i class="fa fa-calendar"></i>
</div>
<input type="date" class="form-control" name="tglawal" id="tglawal">
</div>
</div>
<div class="col-sm-3">
<div class="input-group date">
<div class="input-group-addon">
<i class="fa fa-calendar"></i>
</div>
<input type="date" class="form-control" name="tglakhir" id="tglawal1">
</div>
</div>
</div>
and this my model code :
private function _get_datatables_query()
{
//add custom filter here
if($this->input->post('tglawal'))
{
$this->db->where('b.tglawal', $this->input->post('tglawal'));
}
if($this->input->post('tglakhir'))
{
$this->db->where('b.tglakhir', $this->input->post('tglakhir'));
}
}
public function get_datatables()
{
$this->_get_datatables_query();
if($_POST['length'] != -1)
$this->db->limit($_POST['length'], $_POST['start']);
$query = $this->db->get();
return $query->result();
}
and my controller if i get the important code is:
public function index()
{
$this->load->helper('url');
$this->load->helper('form');
$this->load->view('infokunjungan_view', $data);
}
else redirect(base_url());
}
public function ajax_list()
{
$list = $this->Infokunjungan->get_datatables();
$data = array();
$no = $_POST['start'];
foreach ($list as $infokunjungan) {
$no++;
$row = array();
$row[] = "<td style='vertical-align:middle'><center>{$no}<center></td>";
$row[] = "<td style='font-size:9px; vertical-align:left;'>{$infokunjungan->tglawal}<center></td>";
$row[] = "<td style='font-size:9px; vertical-align:left;'>{$infokunjungan->tglakhir}<center></td>";
$output = array(
"draw" => $_POST['draw'],
"recordsTotal" => $this->Infokunjungan->count_all(),
"recordsFiltered" => $this->Infokunjungan->count_filtered(),
"data" => $data,
);
//output to json format
echo json_encode($output);
}
the problem is if searching between two date tglawal and tglakhir
im using between 2016-12-04 and 2016-12-04 output display will empty
but if using between 2016-12-04 and 2016-12-06 output success where is my problem or maybe im using where or i have to use like?
You need to use the >= and <= operator.
In your model try the below.
if($this->input->post('tglawal'))
{
$this->db->where('b.tglawal >=', $this->input->post('tglawal')); //assuming this is your begining (from) date
}
if($this->input->post('tglakhir'))
{
$this->db->where('b.tglakhir <=', $this->input->post('tglakhir')); //assuming this is your end(to) date
}
The above will search for the between dates including the dates selected.
Use the operator depending on the beginning and ending variable.

Form dropdown is giving me index and not the text

Can you help out a new learner of Codeigniter? for some reason my form dropdown is giving me the index as input->post. I just need the text selected.
Model
function get_items(){
$this->db->select('item_name');
$this->db->from('commissary_items');
$query=$this->db->get();
$result=$query->result();
$item_names=array('-SELECT-');
for($i=0;$i<count($result);$i++){
array_push($item_names,$result[$i]->item_name);
}
return $item_names;
}
View
<div class="form-group">
<div class="row colbox">
<div class="col-lg-4 col-sm-4">
<label for="item_name" class="control-label">Item</label>
</div>
<div class="col-lg-8 col-sm-8">
<?php
$attributes = 'class = "form-control" id = "item_name"';
echo form_dropdown('item_name',$item_name,set_value('item_name'),$attributes);?>
<span class="text-danger"><?php echo form_error('item_name'); ?></span>
</div>
</div>
</div>
Controller
public function new_inventory(){
$data['item_name']=$this->commissary_model->get_items();
$this->form_validation->set_rules('date_added','Date Added','required');
$this->form_validation->set_rules('item_name','Item Name','callback_combo_check');
$this->form_validation->set_rules('quantity','Quantity','required');
$this->form_validation->set_rules('amount','Amount','required|numeric');
$this->form_validation->set_rules('username','User Name');
if($this->form_validation->run()==FALSE){
// $data="";
$this->load->view('new_inventory_view',$data);
}else{
$data=array(
'date_added'=>#date('Y-m-d',#strtotime($this->input->post('date_added'))),
'item_name'=>$this->input->post('item_name'),
'quantity'=>$this->input->post('quantity'),
'amount'=>$this->input->post('amount'),
'username'=>$this->session->userdata('username')
);
$this->db->insert('add_inventory',$data);
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">Item added to inventory.</div>');
redirect('commissary/added_to_inventory');
}
}
Instead of the "text value" inside the form dropdown, I get the index 1 or 2 or 3 or 4 or 5 etc.... Thank you.
That is how <select> options work - the value of the select option is returned.
If you want the text you will have to setup $item_names differently and have the index be the same as the text. Easily accomplished with a little restructuring in the model.
function get_items()
{
$this->db->select('item_name');
$this->db->from('commissary_items');
$query = $this->db->get();
$result = $query->result();
$item_names = array();
foreach($result as $item)
{
$item_names[$item->item_name] = $item->item_name;
}
return $item_names;
}

Resources