I don't know if it's possible but, I've two table with a relation one-to-many (players->statistics).
I get all the stats with this method :
return $this->db->get('statistics')->result_array();
Which means I also get the player_id in there, but I can get it as a string and not as an object.
What I wanted to do is this (Twig) :
{{stat.player_id.name}}
But that doesn't work as it's not an object.
Is it possible to do that in Codeigniter and how ?
You mean something like this :
$this->db->select('*');
$this->db->from('statistics');
$this->db->join('joueurs', 'joueurs.id = statistics.player_id');
return $query = $this->db->get()->result_array();
Yes you can do it in codeIgniter
instead of result_array() you need to use result()
return $this->db->get('statistics')->result();
With the help of joins in codeIgniter you can achieve your desire result
It's not working for me. I'm probably missing something.
Here's my function in Statistics_model.php :
function get_all_statistics()
{
$this->db->select('*');
$this->db->from('statistics');
$this->db->join('joueurs', 'joueurs.id = statistics.player_id');
return $query = $this->db->get()->result();
}
Index action in the controller :
function index()
{
$data['statistics'] = $this->Statistic_model->get_all_statistics();
$this->load->library('twig');
$this->twig->display('statistics/index', $data);
}
And finally the view :
{% for stat in statistics %}
<tr>
<td>{{stat.stat_id}}</td>
<td>{{stat.leg_id}}</td>
<td>{{stat.player_id.name}}</td>
<td>{{stat.darts}}</td>
<td>{{stat.finish}}</td>
<td>{{stat.reste}}</td>
<td>{{stat.max}}</td>
</tr>
{% endfor %}
Everything works fine but, I cannot get the player name
Related
Please check the below code for view and controller.
Controller:
$staffreply = StaffReply::where('ticket_id', '=', $ticketID)->orderBy('id', 'asc')->get();
if ($staffreply->count()) {
//Get the data values
} else {
$staffreply = "";
$supportstaff = "";
$supportteam = "";
}
return view('pages/support/service-view',compact('data','staffreply','supportstaff','supportteam'));
View Page:
#if($staffreply->count())
#foreach($staffreply as $supportDesk)
<span>Posted on {{$supportDesk->created_date;}}</span>
#endforeach
#endif
Error:
Call to a member function count() on int (View: --\htdocs\laravel\project\resources\views\pages\support\service-view.blade.php)
How to solve the issue.
Even though it is not clear what $staffreply is. You can't use count() on non-Arrayables.
First make sure that $staffreply is in fact a collection in the controller. If not, the fallback in the else should probably equal an empty collection and not a 0. If so, in the view you wouldn't need to check if $staffreply->count().
Thanks for the support, after I have edited the view page code issue solved.
Edited Code
#if($staffreply != "")
#foreach($staffreply as $supportDesk)
<span>Posted on {{$supportDesk->created_date;}}</span>
#endforeach
#endif
I've been scratching my head over this since last night. I have this function on my controller:
public function generate_documents($id)
{
$contract = Contract::find($id);
$booking_ids = $contract->booking_id;
$booking_ids = json_decode($booking_ids);
$bookings = Booking::find($booking_ids);
}
$booking_ids returns a json of ids that are stored in the database. I then find the booking records based on the IDs.
foreach($bookings as $booking) {
$asset = Omg_asset::where('id', $booking->omg_asset)->first();
}
Here's the part where I am expecting 3 results but only the last one gets returned on my view. I tried this method:
$asset = '';
foreach($bookings as $booking) {
$asset .= Omg_asset::where('id', $booking->omg_asset)->first();
}
But whenever I try to retrieve the values on my view, it's returning a json containing all the records I need and I can't access it via index.
Attaching a screenshot for better context:
Here's my code on my view by the way:
#foreach($bookings as $booking)
<tr>
<td>{{ $asset }}</td>
</tr>
#endforeach
Here's the error thrown whenever I try to do {{ $asset['asset_name'] }} on view:
"Illegal string offset 'asset_name'"
I haven't used Laravel in a long time so I would really appreciate your help. If there's also a better way to do this, I'm open for suggestions.
Thanks in advance!
I am new to laravel and I am trying to get a pagination function into my result pages, so I have the following function to generate results from query and I would like to have a pagination on the results page, but I don't seem to get it work correctly
public function showResults()
{
$selectedquery = Input::get('Annonces');
$what = Input::get('what');
$where = Input::get('where');
$results = DB::table('annonces')->where($selectedquery,'LIKE', '%'.$what.'%')
->where('Lieu','LIKE', '%'.$where.'%')
->get();
return View::make('results',array('results' => $results));
}
Any Help?
Well, for one, you're missing the call to ->paginate(n). Right now, your closure is ->get(), which returns all results for your annonces table. This is good, but doesn't work for pagination. Change the function like so:
$results = DB::table('annonces')->where($selectedquery,'LIKE', '%'.$what.'%')
->where('Lieu','LIKE', '%'.$where.'%')
->paginate(10);
This will return all results grouped into 10 results per page. Feel free to change that as you see fit.
Lastly, somewhere on your view where you display the results, you will need to use this code to display a page-viewer:
<?php echo $results->links(); ?>
<!-- OR -->
{{ $results->links(); }}
Also, be sure to check out the docs on Laravel's pagination. You'll find it's pretty comprehensive!
Laravel Pagination
Hope that helps!
i am new with codeigniter.
i have used the following code to execute query recursively.
Suppose $q query select 4 id (10,11,20,24)
then for each id showreply function (in foreach) call recursively then how can return the combine result.
$resultq3 = $this->showreply($reply_id);
<?php
public function showreply($reply_id)
{
$q1 =$this->db->select('*')
->from('forum_reply AS fr')
->where('fr.parent_id',$reply_id1)
->order_by('fr.id ')->get();;
foreach($q1->result_array() as $row4)
{
$id = $row4['id'];
$parent_id = $row4['parent_id'];
if($parent_id!=0)
{
$this->showreply($id);
}
}
return $result;
}
?>
I'm not really understanding what it is you're asking here. Maybe showing the showReply function would help, but you already have the combined result and are splitting that out in your foreach so what's the problem? Also why are you assigning reply_id to reply_id1? What is the point of that? Just use $reply_id in your query.
You're also executing an if statement that makes little sense since you can filter out the id's you don't want in the query itself (and are you seriously ever going to have an id that = 0?)
In fact the more I look at this code the more confused I become. Where is $id getting populated for $this->showreply($id)?
<?php
public function showreply($reply_id)
{
$q1 =$this->db->select('*')
->from('forum_reply AS fr')
->where('fr.parent_id',$reply_id)
->where('fr.parent_id !=',0)
->order_by('fr.id ')->get();;
//$i=0;
foreach($q1->result_array() as $row4)
{
$parent_id = $row4['parent_id'];
$this->showreply($id);
}
//below is the actual answer to your question on how to return the combined results.
return $q1->result_array();
}
?>
Okay after rereading your question I think I have a better understanding. If you pass the id's as an array like this:
$reply_id = array(10,11,20,24);
You can then modify your query to use:
$this->db->where_in('fr.parent_id', $reply_id);
That will return the results as one combined result with all 4 ids included.
Controller
public function category($id = '') {
$offset=..;
$var = $this->pm->get_items_by_category($id,$offset,$start_row);
}
But in my view I have :
<li>Mobile</li>
I think I know what you are trying to say but it's not very clear.
Your link needs to pass the limit and offset as well as the id. So do something like this
<li>Mobile</li>
Then in your controller set up your function like this.
public function category($limit, $offset, $id){
}
If you give more info I can give better answer.