image values from database become null - php

the image from the database becomes null and does not appear on the website. in other case, the same image from the same database are appear. there are no mistyped variables, but can't load on the web. I've been trying to put the namespace where the image is, but still not working.
I'm still very new to codeigniter, i've tried my best to explain with my poor english.
please response if you have the answer, thankyou
InvenModel getinfo() function
<?php
namespace App\Models;
use CodeIgniter\Model;
class InvenModel extends Model
{
public function getinfo($slug = false)
{
if ($slug == false) {
return $this->findAll();
}
return $this->where(['slug' => $slug])->first();
}
}
?>
<?php
namespace App\Controllers;
use App\Models\InvenModel;
class inventori extends BaseController
{ public function detail($slug)
{
$data = [
"title" => 'Detail Produk',
"invent" => $this->InvenModel->getinfo($slug)
];
return view('inventori/detail', $data);
}
}
?>
<tr>
<td> <img src="<?= $invent['img']; ?>" class="img"></td> <!-- here the error message appears -->
<td> <?= $invent['nama']; ?> </td>
<td> <?= $invent['stok']; ?> </td>
<td> <?= $invent['terjual']; ?> </td>
<td> Edit </td>
<td> hapus </td>
</tr>

Related

Laravel : Passing value from view to controller

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);
}

how can i fix "Trying to get property of non-object"

i have a favorite list in my website for the users and they can add their favorite house to the wishlist
it goes well but he can not see the wishlist page and an error comes like this:
Trying to get property 'image' of non-object
it's my relations
class Home extends Model
{
protected $guarded = [];
public function favorite()
{
return $this->hasMany(favorite::class,'house_id');
}
}
class favorite extends Model
{
protected $guarded = [];
public function house()
{
return $this->belongsTo(home::class);
}
}
my index function in controller:
public function index()
{
$favorite = favorite::where('user_id',auth()->user()->id)->get();
return view('favorite.index',compact('favorite'));
}
my index:
#foreach($favorite as $fav)
<tr>
<td>
<a href="property-detail.html"><img src="{{$fav->home->image}}" alt=""
width="100"></a>
</td>
<td>{{$fav->home->title}}</td>
<td>خانه خانواده</td>
<td>اجاره</td>
<td>
<div class="price"><span>{{number_format($fav->home->price)}}</span><strong>تومان</strong>
</div>
</td>
<td>
<i class="fa fa-ban"></i> <span>حذف</span>
</td>
</tr>
#endforeach
First thing that your favorite model doesn't have home relation your relation is house and when you want to get its value you can use optional helper function like this:
optional($fav->house)->title
You are trying to access a relationship with a wrong name. You defined the relationship with name house:
public function house(){
return $this->belongsTo(home::class);
}
So you need to use this name to access:
#foreach($favorite as $fav)
<tr>
<td>
<img src="{{$fav->house->image}}" alt="" width="100">
</td>
<td>{{$fav->house->title}}</td>
<td>خانه خانواده</td>
<td>اجاره</td>
<td>
<div class="price"><span>{{number_format($fav->house->price)}}</span><strong>تومان</strong>
</div>
</td>
<td>
<i class="fa fa-ban"></i> <span>حذف</span>
</td>
</tr>
#endforeach
But you will have a problem too if the house relation come null. To avoid you can use the solution proposed by #Mohammed Aktaa:
optional($fav->house)->title

Fetch the record for database using Codeigniter

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

how to print (get) user_name using user id in codeigniter view page

I have to get user_name using user_id in my view.
Following is my mode:
// Model
class main_model extends CI_Model {
public function getOrders() {
$this->db->where( 'real', 1 );
$this->db->ORDER_BY( 'id', 'DESC' );
$query = $this->db->get('orders',10);
return $query->result();
}
public function getUsername($id) {
$this->db->where( 'id', $id );
$query = $this->db->get('users');
return $query->result();
}
// And my Controller goes here below:
public function index() {
$this->load->model('main_model');
$data['orders'] = $this->main_model->getOrders();
$this->load->view('index', $data);
}
// Following is my View file.
<div class="container-fluid">
<div class="row">
<table class="table table-responsive" border="3">
<tr>
<th>OID</th>
<th>user_id</th>
<th>Username</th>
</tr>
<?php foreach($orders as $order): ?>
<tr>
<td> <?= $order->id;?> </td>
<td> <?= $order->uid;?> </td>
<td> ? </td> <!-- Want to get user name using uid. -->
</tr>
<?php endforeach; ?>
</table>
</div>
</div>
I have got user_id from my order table, and user name is on my users table, thus I have to get it from there. Can anyone help please.
You can get user name by single query.
Simple change on getOrders() function in main_model
class main_model extends CI_Model {
public function getOrders() {
$this->db->select('orders.*,(select u.user_name from users as u where u.id = orders.uid) as user_name');
$this->db->where('real', 1 );
$this->db->ORDER_BY( 'id', 'DESC' );
$query = $this->db->get('orders',10);
return $query->result();
}
<tr>
<td> <?= $order->id;?> </td>
<td> <?= $order->uid;?> </td>
<td> <?= $order->user_name;?></td>
</tr>
First, get your user id from getOrders() call, then loop through your orders to get your user. Then from Your getUsername() function in the model returns a user object, so in the controller call it and store the variable:
$orders = $this->main_model->getOrders();
foreach($orders as $order) {
$user = $this->main_model->getUsername($uid);
$order->username = $user->username;
}
$data['orders'] = $orders;
Now the username is in your orders array, just add <td> <?= $order->username;?> </td>
perhaps something like this:
MODEL
public function getUsername($id) {
$this->db->where( 'id', $id );
$query = $this->db->get('users');
return $query->row();
}
VIEW
<?php foreach($orders as $order):
$username = $this->main_model->getUsername($order->uid);
?>
<tr>
<td> <?= $order->id; ?> </td>
<td> <?= $order->uid; ?> </td>
<td> <?= $username->user_name; ?> </td>
</tr>
<?php endforeach; ?>

how to access variables in a different controller with Cakephp

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 ...

Categories