Data fetch and listing issue faced in my Laravel project - laravel

I have developing the ticket system. First table data fetch and display the view page but reply result is showing the last records. How to solve this issue. Please support me.
Controller
$ticketdetails = TicketDetails::where('ticket_id', '=', $ticketID)
->orderBy('ticket_id', 'asc')->get();
if($ticketdetails->count()){
foreach ($ticketdetails as $ticketItem) {
$ticketID = $ticketItem->ticket_id;
$reply_check = Reply::where('reply_ticket_id', '=', $ticketID)->count();
if($reply_check!="")
{
$reply = Reply::where('reply_ticket_id', '=', $ticketID)->first();
}
} //Close foreach
} // Close if loop
return view('view-ticket',compact('data','ticketdetails','reply'));
View Page
if($ticketdetails->count())
#foreach($ticketdetails as $ticketdetails)
<p>{{$ticketdetails->ticket_details}}</p>
$replyid = $reply->reply_ticket_id;
$ticketdetailsid = $ticketdetails->ticket_id;
#php
if($replyid==$ticketdetailsid)
{
#endphp
<p>{{$reply->reply_ticket_comments}}</p>
#php
}
#endphp
#endforeach
#endif
Expecting View page- For example
Ticket Case : Printer not working
Reply:Restart Printer - this is first reply
Ticket Case : After restart same issue.
Reply:okay, we will check now -- this is second reply
Display For view page
Ticket Case : Printer not working
Reply:okay, we will check now -- this is second reply
Ticket Case : After restart same issue.
Reply:okay, we will check now -- this is second reply
Note:Ticket case data display is correct but reply data only showing the last record.

Initialize reply as array and with each iteration push new value to that array. It will look like following
$ticketdetails = TicketDetails::where('ticket_id', '=', $ticketID)
->orderBy('ticket_id', 'asc')->get();
$reply = [];
if(count($ticketdetails) > 0){
foreach ($ticketdetails as $ticketItem) {
$ticketID = $ticketItem->ticket_id;
// $reply_check = Reply::where('reply_ticket_id', '=', $ticketID)->count();
//if($reply_check!=""){
//Code change below
$reply[$ticketID] = Reply::where('reply_ticket_id', '=', $ticketID)->first() ?? [];
// }
} //Close foreach
} // Close if loop
return view('view-ticket',compact('data','ticketdetails','reply'));
reply will consist of all the records with matching ticketID found in ticketdetails
And in view page you can do following
#if(count($ticketdetails) > 0)
#foreach($ticketdetails as $ticketItem) //Change in name of variable
<p>{{$ticketItem->ticket_details}}</p>
//You don't need to check for id as new reply array consist of all records mapped with ticket ID
#if(count($reply) >0)
#if(count($reply[$ticketItem->ticket_id]) >0)
<p>{{$reply[$ticketItem->ticket_id]['reply_ticket_comments']}}</p>
#endif
#endif
#endforeach
#endif

Thanks for the support.
I have stored the reply count in array variable $reply_display[ ] =$reply_check; Then view page check the array values
#php
$replyck = $reply_display[$i];
if($replyck==1){ #endphp
<p>{{$reply[$ticketItem->ticket_id]['reply_ticket_comments']}}</p>
#php }
$i = $i+1;
#endphp
:)- my error issue solved.

Related

How to loop through model and get each id in a varaible [duplicate]

This question already has answers here:
Eloquent get only one column as an array
(5 answers)
Closed 3 years ago.
Hello I am new to laravel, I want to loop through a data table and get each id of it in a variable.
So i did it like this, but unfortunatly nothing works
$station_id = 0;
$stations = Station::all();
foreach ($stations as $station) {
$station_id = $station->id;
}
return $station_id;
this only showing the last id;
What you can do is loop all of the result set and add the ids to an array. First, you need an empty array:
$stations = Station::all();
$station_ids = [];
foreach ($stations as $station) {
$station_ids[] = $station->id;
}
return $station_ids;
Once you have all station_ids, you can then loop in your view like this
Controller
$stations = Station::all();
$station_ids = [];
foreach ($stations as $station) {
$station_ids['ids'] = $station->id;
}
return view('view.blade.php',$station_ids);
view
#foreach($ids as $id)
{{ $id->fieldname}}
#endforeach
You can not assign a single variable in a loop to a single id.
The way you are trying it your $station_id will be set to the very last ID.. If you want to have an array of ids from the stations just use this:
$stations = Station::pluck('id')->toArray();

Mass update in laravel using foreach

How to Update mass records without deleting or replacing old one inserted records. Here mine problem is that last inserted record will replace with latest records.
/*DB::table('syllabuses')
->where('course_id', $curse_id)
->Where('semster',$semesterid)
->delete();*/
$Syllabus = Syllabus::find($id);
foreach ($data as $value)
{
//$Syllabus = new Syllabus;//
$Syllabus->slno = $value->SlNo;
$Syllabus->coursecode =$value->cousrecode;
$Syllabus->coursename =$value->coursename;
$Syllabus->credit =$value->credit;
$Syllabus->papertype=$value->papertype;
$Syllabus->deptoffering_name=$value->Deptoffer;
$Syllabus->deptoffering_id=$value->Department;
$Syllabus->dept_id = $details['depart'];
$Syllabus->save();
}
Please try to understand the difference between INSERT and UPDATE first. Definitely update query replace the old values of the relevant record. In your query you are tying updating same "Syllabus".
$Syllabus = Syllabus::find($id);
foreach ($data as $value)
{
//$Syllabus = new Syllabus;//
$Syllabus->slno = $value->SlNo;
$Syllabus->coursecode =$value->cousrecode;
$Syllabus->coursename =$value->coursename;
$Syllabus->credit =$value->credit;
$Syllabus->papertype=$value->papertype;
$Syllabus->deptoffering_name=$value->Deptoffer;
$Syllabus->deptoffering_id=$value->Department;
$Syllabus->dept_id = $details['depart'];
$Syllabus->save();
}
In above code, you find the Syllabus outside of the foreach. Then you update the that found record inside the foreach. If you want to update many "Syllabus", then try this:
foreach ($data as $value)
{
$Syllabus = Syllabus::find("SyllabusID");
$Syllabus->slno = $value->SlNo;
$Syllabus->coursecode =$value->cousrecode;
$Syllabus->coursename =$value->coursename;
$Syllabus->credit =$value->credit;
$Syllabus->papertype=$value->papertype;
$Syllabus->deptoffering_name=$value->Deptoffer;
$Syllabus->deptoffering_id=$value->Department;
$Syllabus->dept_id = $details['depart'];
$Syllabus->save();
}
Or else, if you want to remain old records, please try to create new records in db using following code
foreach ($data as $value)
{
$Syllabus = new Syllabus();
$Syllabus->slno = $value->SlNo;
$Syllabus->coursecode =$value->cousrecode;
$Syllabus->coursename =$value->coursename;
$Syllabus->credit =$value->credit;
$Syllabus->papertype=$value->papertype;
$Syllabus->deptoffering_name=$value->Deptoffer;
$Syllabus->deptoffering_id=$value->Department;
$Syllabus->dept_id = $details['depart'];
$Syllabus->save();
}
Read official laravel documentation for further details: https://laravel.com/docs/5.7/eloquent

how to change the color of each crud entry when ajax table is enabled?

I am using laravel backpack and recently enabled $this->crud->enableAjaxTable(); in my crud because there was a lot of data to show.
But now I am not able to color my crud entries depending upon a expiry_date as I was doing before by overriding list.blade.php like this:
#if (!$crud->ajaxTable())
#foreach ($entries as $k => $entry)
<?php
use Carbon\Carbon;
$today_date = Carbon::now();
$data_difference = $today_date->diffInDays(Carbon::parse($entry->expiry_date), false);
if($data_difference <= 7 && $data_difference >= 0) {
$color="#FF9900";
} elseif($data_difference < 0) {
$color="#EA2C12";
} elseif($data_difference > 7) {
$color="#539E05";
}
?>
<tr data-entry-id="{{ $entry->getKey() }}" style="color: {{$color}}">
Maybe because of this:
#if (!$crud->ajaxTable())
I tried to customize the AjaxTable.php search query using this link but I was not successful. Here is the code I tried in my ExampleCrudController by overriding search query of ajax:
public function search()
{
$this->crud->hasAccessOrFail('list');
// create an array with the names of the searchable columns
$columns = collect($this->crud->columns)
->reject(function ($column, $key) {
// the select_multiple, model_function and model_function_attribute columns are not searchable
return isset($column['type']) && ($column['type'] == 'select_multiple' || $column['type'] == 'model_function' || $column['type'] == 'model_function_attribute');
})
->pluck('name')
// add the primary key, otherwise the buttons won't work
->merge($this->crud->model->getKeyName())
->toArray();
// structure the response in a DataTable-friendly way
$dataTable = new \LiveControl\EloquentDataTable\DataTable($this->crud->query, $columns);
// make the datatable use the column types instead of just echoing the text
$dataTable->setFormatRowFunction(function ($entry) {
$today_date = Carbon::now();
$data_difference = $today_date->diffInDays(Carbon::parse($entry->expiry_date), false);
if($data_difference <= 7 && $data_difference >= 0) {
$color="#FF9900";
} elseif($data_difference < 0) {
$color="#EA2C12";
} elseif($data_difference > 7) {
$color="#539E05";
}
// get the actual HTML for each row's cell
$row_items = $this->crud->getRowViews($entry, $this->crud, $color);
// add the buttons as the last column
if ($this->crud->buttons->where('stack', 'line')->count()) {
$row_items[] = \View::make('crud::inc.button_stack', ['stack' => 'line'])
->with('crud', $this->crud)
->with('entry', $entry)
->render();
}
// add the details_row buttons as the first column
if ($this->crud->details_row) {
array_unshift($row_items, \View::make('crud::columns.details_row_button')
->with('crud', $this->crud)
->with('entry', $entry)
->render());
}
return $row_items;
});
return $dataTable->make();
}
So my question is how can I color my crud entries depending upon expiry_date when enableajaxtable is active in laravel backpack?
When using AjaxDataTables, the rows no longer taken from the DB directly and outputed as HTML, but taken from the DB with an AJAX call. So your previous code wouldn't work, I'm afraid.
The best way I can think of to achieve the same thing would be to use a custom view for this CRUD panel, with $this->crud->setListView('your-view');. This would allow you to setup some custom JavaScript in that file, to modify DataTables.js to color the rows before it puts them in the table.
A cleaner alternative, if you're using Backpack\CRUD 3.2+, would be to customize the list.js file, to have all that logic there.
Hope it helps!

Multiple Eloquent Where statements with multiple parameters

I have a parsing problem where I want to get all of the people with a particular subscription but not people who have another type of subscription. The subscriptions are stored in a comma delineated list in the subscriptions table in the subscriptions column. Here is the code I have so far:
$includeQuery = [];
foreach ($includeSegment as $include) {
$singleQuery = ['subscriptions','like', '%'.$include.'%', 'or'];
array_push($includeQuery, $singleQuery);
}
$excludeQuery = [];
foreach ($excludeSegment as $exclude) {
$singleQuery = ['subscriptions', 'not like', '%'.$exclude.'%', 'or'];
array_push($excludeQuery, $singleQuery);
}
$included = Subscription::where($excludeQuery)->where($includeQuery)->get();
I get results back but some of them have the excluded subscriptions in them.
use whereIn and whereNotIn instead :
Subscription::whereIn($includeSegment)->whereNotIn($excludeSegment)->get();
then you dont need to also iterate segments once they are arrays of strings
The problem with code above was in the boolean parameter I had "or" instead of "and". So only if all of the subscriptions for a particular user were present would the record get thrown out. Here is the updated code:
$includeQuery = [];
foreach ($includeSegment as $include) {
$singleQuery = ['subscriptions','like', '%'.$include.'%', 'or'];
array_push($includeQuery, $singleQuery);
}
$excludeQuery = [];
foreach ($excludeSegment as $exclude) {
$singleQuery = ['subscriptions', 'not like', '%'.$exclude.'%', 'and'];
array_push($excludeQuery, $singleQuery);
}
// $included = Subscription::where($excludeQuery)->where($includeQuery)->get();
$included = DB::table('subscriptions')
->join('app_users', 'app_users.customer_number', '=', 'subscriptions.customer_number')
->join('app_datas', 'app_datas.customer_number', '=', 'subscriptions.customer_number')
->where($includeQuery)
->where($excludeQuery)
->select('app_datas.device_token')
->get();

error foreach in View Codeigniter : Illegal string offset

Please help, i use foreach to return data from database, but it only return the last data (array always update with last data). So i modified the code but i got error : Illegal string offset in foreach.
here is my controller
foreach($dataDesa as $desa)
{
$namaDesa = json_decode($this->curl->simple_get($desa->alamat_api.'/info_desa?ID_DESA=1'));
$datakonten[$namaDesa] = array(
'proyek' =>json_decode($this->curl->simple_get($desa->alamat_api.'/proyek_pertanian')),
'nama_desa' =>json_decode($this->curl->simple_get($desa->alamat_api.'/info_desa?ID_DESA=1')),
'lelang' =>json_decode($this->curl->simple_get($desa->alamat_api.'/pelelangan'))
);
}
$datakonten['getAllDesa'] = $this->M_desa->getAllDesa($idStatus);
$nama_Desa = array();
foreach($datakonten['getAllDesa'] as $row)
{
$nama_Desa[] = $row->nama_desa;
}
$datakonten['nama_desa']=$nama_Desa;
$data['content'] = $this->load->view('public/konten/v_home',$datakonten,TRUE);
$this->load->view('public/halaman/v_home',$data);
and here is my view
$i=0;
foreach($nama_desa[$i]['proyek'] as $rows)
{
$nama = $rows->nama_proyek;
i++;
}
i've tested $nama_desa[0] and $nama_desa[1] and they have value returned (the value is "Kranon" and "Wulung") and i use the value like $Kranon['proyek'] and theres no error and returned the value that i want, but when i combined it with $nama_desa[$i]['proyek'] i got this error.
`
Please help, thanks in advance
You only push $row->nama_desa to $nama_desa. and try to get ['proyek'] from $nama_desa?
As you explained above, you getting value with this $nama_desa[1] but you trying to access this key $nama_desa[$i]['proyek'] which means its looking for $nama_desa[1]['proyek'] which is not exist.
Just do
print_r($name_desa);die;
You will find there is no key present with the name of 'proyek' on $name_desa[1] (key '1' i have only given for example. It can be any number of key)
I guess you will get your value by accessing this $nama_desa['proyek'] OR You will get idea with print result.
I have just found my solution.
I always getting an error whenever i want to pass variable that refers another value into foreach, so I modified my controller so I can pass the value into foreach in View directly. And I moved kind of "get-data-from-API" from Controller into View.
My Controller :
$datakonten['getAllDesa'] = $this->M_desa->getAllDesa($idStatus);
$data['content'] = $this->load>view('public/konten/v_home',$datakonten,TRUE);
My View :
foreach($getAllDesa as $rows)
{
$proyek = json_decode($this->curl->simple_get($rows->alamat_api_lelang));
foreach($proyek as $baris)
{
$namaProyek = $baris->nama_proyek;
?>
Nama Proyek : <?php echo $namaProyek;?></br>
<?php
}
}

Resources