I am a beginner in Codeigniter framework and I am also learning from youtube
In my admin.php code is
<?php
class Admin extends MY_Controller {
public function dashboard(){
$this->load->model('articlesmodel');
$articles = $this->articlesmodel->articles_list();
$this->load->view('admin/dashboard', ['articles'=> $articles]);
}
}
?>
And in my dashboard.php code is
<table class="table">
<thead>
<th>Sr. No</th>
<th>Title</th>
<th>Action</th>
</thead>
<tbody>
<?php if ( count($articles) ):?>
<?php foreach($articles as $article): ?>
<tr>
<td>1</td>
<td>
<?= $article->title ?>
</td>
<td>
Edit
Delete
</td>
</tr>
<?php endforeach; ?>
<?php else:?>
<tr>
<td colspan="3">No Records Found</td>
</tr>
<?php endif;?>
</tbody>
</table>
I am fetching the articles from my database in table format, but the following error is presented: Undefined variable: articles in dashboard.php page
change My_Controller to CI_Controller in Admin controller class definition.
When creating a controller class in Codeigniter, you should always extend CI_Controller class. Because $this->load() function is implemented in CI_Controller class. See the documentation
As a best practice create an array and pass it into view() function instead of initializing an array within view() function parameters. ex:
$data['articles'] = $this->articlesmodel->articles_list();
$this->load->view('admin/dashboard',$data);
controller
change these lines
$articles = $this->articlesmodel->articles_list();
$this->load->view('admin/dashboard', ['articles'=> $articles]);
to this
$data['articles'] = $this->articlesmodel->articles_list();
$this->load->view('admin/dashboard',$data);
Tip:
try to look at documentation syntax
https://www.codeigniter.com/user_guide/general/views.html
Related
Before I start here are some translation, since I'm not using english for my code:
Penyedia = Vendor
Pekerjaan = Work
So I have two table, Vendor Table and Work Table. Each Vendor can have multiple of Works, and each Work can be own by only one Vendor. It supposed to be one to many relationship. Here is my table
Vendor Table
Work Table
So, I'm using LARAVEL as my framework. I have created a vendor table with an action button, that I want it to function which if click, it will show the work (pekerjaan) list owned by the exact vendor name. As you can see from the table above I added the vendor name (nama) to the "Work Table" in my database. How could I do this function to work? I'm new to this "Eloquent" in Laravel.
Here is my model files:
Pekerjaan.php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model as Eloquent;
class Pekerjaan extends Eloquent
{
use HasFactory;
protected $guarded = [];
public function penyedia(){
return $this->belongsTo('App\Models\Penyedia');
}
}
Penyedia.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model as Eloquent;
class Penyedia extends Eloquent
{
use HasFactory;
protected $guarded = [];
public $timestamps = false;
public function pekerjaan(){
return $this->hasMany('App\Models\Pekerjaan');
}
}
AdminController.php
public function showpekerjaan(){
$penyedia = penyedia::all();
return view('admin.showpekerjaan', compact('penyedia'));
}
tabelnilai_pekerjaan.blade.php (I want to show this work table according to the vendor)
<section class="content">
<div class="container-fluid">
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-header">
<h3 class="card-title">Tabel Nilai Pekerjaan</h3>
</div>
<!-- /.card-header -->
<div class="card-body table-responsive">
<table id="tabelpekerjaan" class="table table-bordered">
<thead>
<tr>
<th style="width: 10px">No.</th>
<th>Paket Pekerjaan</th>
<th>Nama Perusahaan</th>
<th>Lokasi Pekerjaan</th>
<th>HPS</th>
<th>Nilai Kontrak</th>
<th style="width: 120px">Aksi</th>
</tr>
</thead>
<tbody>
#php $no = 1; #endphp
#foreach ($penyedia as $penyedias)
<tr>
<td>{{$no++}}</td>
<td>{{$penyedias->pekerjaan->pekerjaan}}</td>
<td>{{$penyedias->pekerjaan->nama}}</td>
<td>{{$penyedias->pekerjaan->lokasi}}</td>
<td>Rp. {{number_format($pekerjaans->pekerjaan->hps,0,',',',')}}</td>
<td>Rp. {{number_format($pekerjaans->pekerjaan->nilai_kontrak,0,',',',')}}</td>
<td>
Edit
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
<!-- /.card-body -->
</div>
<!-- /.card -->
</div>
</section>
Is there anything that I need to add or edit or change? Thank you
You are trying to print array as string. Your pekerjaan() relation has hasmany property and it returns array. If you like to display all pekerjaan, you need to run a second loop inside a loop. and if you need to display only one, you can simply do
<td>{{$penyedias->pekerjaan[0]->pekerjaan}}</td>
To display all pekerjaan, you can loop on $penyedias->pekerjaan like:
#php $no = 1; #endphp
#foreach ($penyedia as $penyedias)
#foreach($penyedias->pekerjaan as $pekerjaan)
<tr>
<td>{{$no++}}</td>
<td>{{$pekerjaan->pekerjaan}}</td>
<td>{{$penyedias->nama}}</td>
<td>{{$pekerjaan->lokasi}}</td>
<td>Rp. {{number_format($pekerjaan->hps,0,',',',')}}</td>
<td>Rp. {{number_format($pekerjaan->nilai_kontrak,0,',',',')}}</td>
<td>
Edit
</td>
</tr>
#endforeach
#endforeach
And change $penyedia = penyedia::all(); on your controller to $penyedia = Penyedia::all();
My View:
<table border="1" style="text-align: left;">
<td>
<b>Firstname</b>
</td>
<td>
<b>Actions</b>
</td>
#foreach($Newslist as $News)
<tr>
<td>
{{ $News->lastname }}
</td>
<td>
<button type="button" class="btn btn-primary">Edit</button>
</td>
</tr>
#endforeach
</table>
My routes/web.php:
Route::Post('NewsEdit/{{$News->id}}/EditForm','NewsControllere#EditForm');
My Controller.php:
<?php
namespace App\Http\Controllers;
use App\Newsmodel;
use Illuminate\Http\Request;
class NewsControllere extends Controller
{
public function EditForm($NewsId)
{ dd(request()->all());
echo "deepakkeynes";exit();
//$Newsmodel = Newsmodel::find($NewsId);
//return view('/News')->with('News',$Newsmodel);
}
}
While clicking the edit button in the view, the following result is obtained:
Result: 404 page
Expected Result: The Id of the edit button along with the echo value!
Can anyone help me out? I am a newbie in laravel..!
Have a look at this LaraCast tutorial on Route Model Binding. This explains the underlying documentation well.
Essentially:
routes/web.php:
Route::get('NewsEdit/{news}/EditForm','NewsControllere#EditForm');
newsController.php:
public function EditForm(Newsmodel $news)
{
return view('/News')->with('News',$news);
}
i want to show all users in a table and paginate eloquent results.
i call paginate method in user controller like this:
$users=User::orderBy('id','DESC')->paginate(10);
and returning view after that like:
return view('admin.users.index',compact('users'));
and in my view(index.blade.php) try to display pagination result using blade like:
<table>
<thead>
<th>id</th>
<th>name</th>
<th>email</th>
</thead>
<tbody>
#if ($users->count()>0)
#foreach ($users as $user)
<tr>
<td>{{$user->id}}</td>
<td>{{$user->name}}</td>
<td>{{$user->email}}</td>
</tr>
#endforeach
#endif
</tbody>
</table>
{{$users->links()}}
i tried to use simplePaginate and getting same error..
by removing {{$users->links()}} works fine. so paginator method works and i get the Paginator object.
this is controller with method called in that route:
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
class UsersController extends Controller
{
public function index()
{
$users=User::orderBy('id','DESC')->paginate(10);
return view('admin.users.index',compact('users'));
}
}
Good day
I'm facing some errors while trying to fetch data from database
here are my codes
my table name is meeting and I used the formal documentation to write the codes
controllers.php
public function meetingdet()
{
$mretive = \DB::table('meeting')-> $name = $request->input('meetingname');}
view
<table border="1" >
<tr>
<td><b>blah</b> </td>
<td><b>blah</b> </td></tr>
<tr>
<td>
</td>
<td><li>foreach ($name as $retrive) {
echo $retrive-> meeting;}
#endforeach
</li>
</td>
</tr>
</table>
but this what is showing for me
syntax error, unexpected 'endforeach' (T_ENDFOREACH) and whan I remove
endforeach it display for me the for each without fetching the data
By the way I'm working on laravel 5.1
Regards
You can try with the following sample code:
controllers.php
//include your model file
use App\model;
public function meetingdet()
{
$meetingData = model::getMeetingData();
$view = array(
'name'=>$meetingData
);
return view('test', compact('meetingData'));
}
Model.php
public static function getMeetingData()
{
$value=DB::table('meeting')->orderBy('meeting_id', 'asc')->get();
return $value;
}
view.blade.php
<table border="1" >
<tr>
<td><b>blah</b> </td>
<td><b>blah</b> </td></tr>
<tr>
<td>
</td>
<td>
<ul>
<li>
#foreach ($meetingData as $retrive)
{{{ $retrive->meeting_id }}}
#endforeach
</li>
</ul>
</td>
</tr>
</table>
<li>
#foreach ($name as $retrive)
{{ $retrive->meeting }}
#endforeach
</li>
you didn't add # to foreach..
Right, i have:
Controller/PostsController.php
Model/Post.php
View/Posts/index.ctp
the controller finds all the posts in a database and then passes the values into my view/Posts/index.ctp, where i foreach though each post.
I now want to be able to access the same posts but on another controller and view. How do i do this?
PostsController is;
class PostsController extends AppController {
public function index() {
$this->set('posts', $this->Post->find('all'));
}
}
View/Posts/index.ctp is;
<table>
<tr>
<th>Id</th>
<th>Title</th>
<th>Actions</th>
<th>Created</th>
</tr>
<!-- Here's where we loop through our $posts array, printing out post info -->
<?php foreach ($posts as $post): ?>
<tr>
<td><?php echo $post['Post']['id']; ?></td>
<td>
<?php
echo $this->Html->link(
$post['Post']['title']
);
?>
</td>
<td>
<?php echo $post['Post']['created']; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
Now in my AboutsController and Abouts/index.ctp i want to be able to access the same data so that i can show the posts on the view/abouts/index.ctp
when i make the view/abouts/index.ctp, have the same code as View/Posts/index.ctp i get the error message;
Undefined variable: posts [APP\View\Abouts\index.ctp, line 12]
This is because i do not know how to make the data accessible in my Abouts/Controller.
For the global variable , i suggest to set it un the appController from which you can access into it wherever you are, in the appController :
class AppController extends Controller {
var $uses = array('Post'); // to use the model
public function beforeFilter(){
$this->set('posts', $this->Post->find('all'));
}
}
then you can get posts in any view ...