how to display data from two table, one to many in codeigniter - codeigniter

i have two table SQL
table users
no_kk
name
table kk
id
name_kk
status
educate
no_id
in this case no_kk relation with no_id. i will display all spesific data by parameter no_kk, please help me thanks all

Use join query for this.
sample for user table:
$this->db->select('kk.*,users.*')->join('users', 'users.no_kk = kk.no_id');
$this->db->where('kk.no_id', $no_kk);
$query = $this->db->get();

You can compose your query as follows
public function get_data($id){
$this->db->select('*');
$this->db->from('kk');
$this->db->join('users','kk.no_id = users. no_kk');
$this->db->where('kk.no_id = "$id"');
$result_set = $this->db->get();
if($result_set->num_rows() > 0){
return $result_set->result();
}else{
return FALSE;
}
}
Pass in your user id to the model and return the result set.
In the controller, while loading the view, pass in the array returned from the model.
public function your-controller-method(){
$data['result'] = $this->your-model->get_data($user_id);
$this->load->view('your-view-name',$data);
}
I hope that helps you.

In view page you can display like this in table format or any other you like.
View page code in table format:
<?php foreach ($result as $row): ?>
<tr><td><?php echo $row['fieldname'];?></td>
<td><?php echo $row['fieldname1'];?></td>
<td>...</td>.......
</<tr>
<?php endforeach ?>

I have read your question, you have done with getting data from two tables by using the join query, but for now, your problem is to show how to view this data in your table view.
for this purpose, you have to create few files in different folders
in view
use the for each loop to fetch data in table view
<?php if (!empty($users))
{
foreach ($users as $user)
{ ?>
<tr>
<td> <?php echo $user['name'] ?> </td>
<?php
}
?>
</tr>
<?php
}
}
?>
and in controller file add view function
public function view()
{
$this->load->model('uModel');
$data['users']= $this->uModel->All();
$this->load->view('list' , $data);
}
and in model file add this function
public function All()
{
return $users = $this->db->get('users')->result_array();
}
it will definitely help you out
Thanks

Related

get value from another table based on id laravel

i have two tables ( Factories , Products )
Factories table has two columns ( id , factory_name )
and Products table has three columns ( id , factory_id , product_name )
i want to display factory_name column in Factories table in blade.php based on factory_id in Products table
the code in Factory Model :
public function products()
{
return $this->hasMany('App\Product');
}
and the code in Product Model :
public function factories()
{
return $this->belongsTo('App\Factory');
}
the code in ProductsController :
public function index()
{
$data['Products'] = Product::with('factories')->orderBy('created_at', 'desc')->get();
return view('all_products')->with($data);
}
and the code in view :
#foreach ($Products as $Product)
<tr>
<td>{{$Product->id}}</td>
<td>{{$Product->factory_name}}</td>
<td>{{$Product->product_name}}</td>
</tr>
#endforeach
and this not work ... so how i get factory_name
It's always a good idea to add {{dd($data)}} to blade file to see what you have.
I think the relation between products and factories is one-to-many. In this case, it would be better if you make the function name factory instead of factories.
public function factory()
{
return $this->belongsTo('App\Factory');
}
Unless it's a correct syntax that I'm not familiar with, the problem should be $data['Products'] and with($data). Let's change them:
public function index()
{
$data = Product::with('factory')->orderBy('created_at', 'desc')->get();
return view('all_products')->with('Products', $data);
}
Now, you have $Products on your blade file.
Since you get factory_name through relation, factory info is associated with a key. You need this:
<td>{{$Product->factory->factory_name}}</td>
its easily like this :
#foreach ($Products as $Product)
<tr>
<td>{{$Product->id}}</td>
<td>{{$Product->factories->factory_name}}</td>
<td>{{$Product->product_name}}</td>
</tr>
#endforeach
since you already have a relationship called factories between your two models
As you have shown that you are trying fetching Products along with factories
Product::with('factories')->orderBy('created_at', 'desc')->get();
So the result that you will get from this query will be in the following format
{
id: product_id,
factories: {
id:factory_id,
factory_name: factory_name,
...
}
}
So in order to fetch the factory name in products list you need to use:
<td>{{$Product->factories->factory_name}}</td>
instead of
<td>{{$Product->factory_name}}</td>
Hope this solve your problems

How to get data from different Model in one controller in laravel5.8?

I have Two Model
1. NewJob
2. ClientInfo
From NewJob model I inserted the data, fetched the data and displayed it nicely...
My question is - In my insert model I am using html dropdown, where all clients name should display dynamically using ClientInfo model. How to do this?
NewJobController code -
public function index(Request $request)
{
$posts = NewJob::orderBy('won', 'desc')->paginate(5);
$clients = ClientInfo::orderBy('id', 'asc');
if ($request->ajax())
{
return view('Admin.NewJob.ajax', compact('posts', 'clients'));
}
else
{
return view('Admin.NewJob.newjob', compact('posts' , 'clients'));
}
}
In view file
#foreach($posts as $post)
<tr>
<td>{{$post->won}}</td>
<td>{{$post->proj_name}}</td>
<td>{{$post->department}}</td>
<td>{{$post->district}}</td>
</tr>
#endforeach
In my Insert Model I used:
<select>
#foreach($clients as $client)
<option value="{{$client->id}}">{{$client->name}}</option>
#endforeach
</select>
But in dropdown List no result is displaying... and also no error displaying.
Plz help... I am new in Laravel.
In my index method I changed this line then it works fine...
$clients = DB::select('select * from client_infos');

How to pass variable from foreach to view in laravel 5.4?

I want to count each location in my Job table by using location_id in my job table with id in location table. below code, I can count result correctly but I don't know how to pass this variable to the view. Please help?
//my code
public function index(){
$location = Location::all();
$count_location = [];
foreach ($location as $locations){
$count_location = Job::where('location_id', $locations->id)->count();
}
}
Use withCount() and view() to pass location with counted jobs to the view:
public function index(){
return view('view.name', [
'locations' => Location::withCount('jobs')->get()
]);
}
In the view:
#foreach ($locations as $location)
{{ $location->name }} has {{ $location->jobs_count }} jobs
#endforeach
You can return the collection of locations to the view and then loop through each object in the collection like so:
return view('index', [
'locations'=> $locations,
]);
Then in your index.blade.php you can use something like a #foreach or #forelse loop
#foreach ($locations as $location)
{{ $location->id }}
#endfoeach
EDIT
From the looks of it you would be better off defining a relationship between locations and jobs (i.e. a "many to many" or "one to many" relationship). this would allow you to get the counts for jobs at given locations very easily like so:
$location->jobs->count()
Eloquent relationships are explained in the documentation here
https://laravel.com/docs/5.5/eloquent-relationships
It would be more efficient if construct your query to fetch the count of related models instead of looping through all the results.
Have a look at Counting Related Models in the documentations.
For example, to get the count of all jobs related to a location, you could do:
$locations = App\Location::withCount('jobs')->get();
foreach ($locations as $location) {
echo $location->jobs_count;
}
You need to adjust the code according to your models structure.
Do this
public function index(){
$locations = Location::all();
return view('index', compact('locations'));
}
In your Location model make a relationship by adding this
public function jobs(){
return $this->hasMany(Job::class);
}
In your index view do this
#foreach ($locations as $location)
{{$location->jobs->count}}
#endforeach
Please note that Job should be there in your your model

i want to know, how to display values from controller to view page. the values are fetched from database

i fetch some values from database using laravel 5. but i have no idea to display that values to the view page please help me.
this is my controller code for fetch values from database
public function getcategory()
{
$category =DB::select('select CategoryTitle from category');
echo json_encode($category);
}
this is my route
Route::get('/addworkout/category',array('uses'=>'WorkoutController#getcategory'));
It is simple get data from database and show in views
Fetch data and return to view
public function getCategory()
{
//Get category from db
$category =DB::select('select CategoryTitle from category');
//It will return your category as data to post view
return view('pages.post')->withData($category)
}
Display category name in view
#foreach ($data as $cate)
{{ $cate->name }}
#endforeach

How do I use Eloquent ORM to retrieve data from a database in Laravel

I'm new to MVC and do not understand it very much at all. I have a database with one table called products
I have a Model called Productwhich looks like
<?php
class Product extends Eloquent {
// MASS ASSIGNMENT -------------------------------------------------------
// define which attributes are mass assignable (for security)
// we only want these 3 attributes able to be filled
protected $fillable = array('type', 'brand', 'image');
}
I have a Route like so
Route::get('products', function()
{
return View::make('products');
});
And my View looks like this
#extends('master')
#section('content')
PRODUCTS
#foreach($products as $product)
{{ $product->type }}
#endforeach
#stop
I see you can get all the rows like so $products = Product::all(); but I don't understand WHERE this goes. Does it go in the View? Does it go in the Model? Does it go in the Route? My current #foreach loop just results in an undefined variable: products
Can someone please clarify?
This should get you started for now:
Route::get('products', function()
{
$products = Product::all();
return View::make('products')->with('products', $products);
});

Resources