Echo data with eloquent - laravel

I am trying to get data out of my table, and send that array of output over to my view and there echo out specific parts of it.
I get an exception error: Undefined index bassengId.
index.php
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Bassengweb</title>
</head>
<body>
<?php
if(isset($htt))
{
echo $htt['malingsId'];
}
?>
</body>
</html>
routes.php
Route::get('/', 'HomeController#showIndex');
Route::post('/data', 'HomeController#showInput');
homecontroller.php
public function showIndex()
{
return View::make('index');
}
public function showInput()
{
$htt = hvertredjetime::all();
return View::make('index')->with('htt', $htt);
}
If I try to just echo the $htt variable from index, I get:
[{"malingsId":1,"dato":"25.02.2014","tid":"12:44:00","frittKlor":"4.00","bundetKlor":"5.00","totalKlor":"9.00","ph":"7.00","autoPh":"8.00","autoKlor":"9.00","redox":"5.00","bassengId":1}]
I am a little stuck here, being new to this and not really seeing what I do wrong.

By using the ::all() method you are returning an array
public function showInput()
{
$htt = hvertredjetime::all();
return View::make('index')->with('htt', $htt);
}
In the View
#if(! is_null($htt))
#foreach($htt as $item)
<p>{{ $item->bassengId }}</p>
#endforeach
#endif

If you want to access data like this, you should do:
return View::make('index')->with(array('htt' => $htt));
Instead, return it like this:
return View::make('index')->with($htt);
And then, in view, access it directly:
{{ bassengId }}

You should access to the variable like this :
if(isset($htt))
{
echo $htt->bassengId;
}
or if you're using blade template :
#if(isset($htt))
{{ $htt->bassengId }}
#endif

Related

how to use the where clause in laravel to retrieve different categories of products in the same table

I am currently making an ecommerce site and am new to Laravel, and at the moment I'd like to retrieve data based on different categories from the same table, using a where clause to store the retrieved data in separate variables. The problem here is whenever I run this code it say undefined variable $newproducts. Below is my code. Thanks in advance
My Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\product;
class ProductsController extends Controller
{
public function index(){
$allproducts=product::all();
$bestsellerproducts=product::where('category','iphone 11')->get();
$newproducts=product::where('category','iphone 12')->get();
return view('test',['products'=>$allproducts],['bestsellerproducts'=>$bestsellerproducts],['newproducts'=>$newproducts]);
}
}
My view Template
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<h2>Best Seller products</h2>
#foreach($bestsellerproducts as $bestsellerproduct)
<label>Name</label>
{{ $bestsellerproduct['name'] }}
<label>Price</label>
{{ $bestsellerproduct['price'] }}
#endforeach<br>
<h2>New products</h2>
#foreach($newproducts as $newproduct)
<label>BaseName</label>
{{ $newproduct['name'] }}
<label>Price</label>
{{ $newproduct['price'] }}
#endforeach<br>
<h2>All products</h2>
#foreach($products as $product)
<label>BaseName</label>
{{ $product['name'] }}
<label>Price</label>
{{ $product['price'] }}
#endforeach<br>
</body>
</html>
You need to pass all view variables in a single array, not multiple arrays:
class ProductsController extends Controller
{
public function index(){
$allproducts=product::all();
$bestsellerproducts=product::where('category','iphone 11')->get();
$newproducts=product::where('category','iphone 12')->get();
return view('test',['products'=>$allproducts, 'bestsellerproducts'=>$bestsellerproducts,'newproducts'=>$newproducts]);
}
}
Alternatively you can use with:
class ProductsController extends Controller
{
public function index(){
$allproducts=product::all();
$bestsellerproducts=product::where('category','iphone 11')->get();
$newproducts=product::where('category','iphone 12')->get();
return view('test')
->with('products',$allproducts)
->with('bestsellerproducts', $bestsellerproducts)
->with('newproducts', $newproducts);
}
}

Trying to get property of non-object - Undefined variable: data/ Codeigniter

i'm try to get news_id from database but when go to view say : error -> Trying to get property of non-object / Message: Undefined variable: data
this model - >
class Model1 extends CI_Model {
public function get_art()
{
$query = $this->db->get('entries');
return $query->result();
}
}
here controller Code - >
class Home extends CI_Controller
{
public function members()
{
$this->load->model('model1');
$data=$this->model1->get_art();
$this->load->view('members', $data);
}
}
and this Full View - >
<html>
<head>
<meta charset="utf-8">
<title>Welcome to CodeIgniter</title>
</head>
<body>
<h1>
<? echo $data->body; ?>
</h1>
</body>
</html>
This is because of you invalid pass data to view. At controller replace line
$data=$this->model1->get_art();
with
$data["query"] = $this->model1->get_art();
Then at view you will have var $query with results of your database query.
You can use it like this:
<h1>
<? foreach($query as $row) {
echo $row->body;
}
?>
</h1>

Codeigniter links for search results

I'm a new to CodeIgniter and I have a problem when I display a list of items, I want each items to be a link, so when the user clicks that particular item, it will show the details of that item. Or perhaps give some suggestion on what I should google for. I have literally no idea whats terms or keywords that should I search about.
controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site extends CI_Controller
{
public function index()
{
$this->getListOfAgent();
}
function getListOfAgent()
{
$this->load->model("agentDB_model");
$data['results'] = $this->agentDB_model->getAllAgentInfo();
$this->load->view("viewAgent_view", $data);
}
function userInformation()
{
//how to i get the selected item which the user clicked and show the details accordingly?
}
}
model
<?php
class agentDB_model extends CI_Model
{
function getAllAgentInfo()
{
$query = $this->db->query("SELECT * FROM user");
return $query->result();
}
function addAgent($newAgent)
{
$this->db->insert("user", $newAgent);
}
}
view
<!DOCTYPE html>
<html lang="en">
<head>
<title>Welcome</title>
</head>
<body>
<div id="content">
<h1>Home Page</h1>
<p>List Of Agent in database</p>
</div>
<?php
foreach($results as $row)
{
echo "<a href = 'Site/userInformation'>$row->userName</a>";
echo "</br>";
echo "</br>";
}
//$this->load->controller(Site/userInformation);
?>
<div id="footer">
<p>Copyright (c) 2012 basicsite.com</p>
</div>
</body>
</html>
You need to tell the Site/userInformation method for what record you want the information. You use URI segments for this.
So, in your view, change the following line:
echo "<a href = 'Site/userInformation'>$row->userName</a>";
to:
echo "<a href = 'Site/userInformation/$row->userID'>$row->userName</a>";
Then, in your controller method, add the parameter to the method declaration:
function userInformation($userID)
{
// now, you use the model to get the correct record from the db based on
// the $userID and display the information
}

Undefined variable : variable from controller to view

Hi i dont know why its not any more working.
i got undefinied variable if i try to echo the variable in the view.
Here the controller
class save_settings extends CI_Controller {
function save()
{
$data['test'] = 'content';
$this->load->view('help', $data);
}
}
and view
<!DOCTYPE HTML>
<html lang="de">
<head></head>
<body>
<?php echo $test ; ?>
</body>
</html>
Some issue to check -
1) Correct view name and path
2) Correct URL - /save_settings/save
3) Try to print and exit something to check whether the controller is loading perfectly.
function save()
{
$data['test'] = 'content';
echo '<pre>'; print_r($data); exit; // <== Debug
$this->load->view('help', $data);
}
// Should give
Array(
[test] => content
)

Laravel without Blade - Controllers and Views

I am more efficient at setting up my view logic with straight up php. Blade is cool but it's not for me. I am trying to translate all the Blade specific examples and docs to just php. I don't like the fact that I need to assign all the variables for my views in an array of View::make(). I did found all of this so far.
controllers/home.php:
class Home_Controller extends Base_Controller {
public $layout = 'layouts.default';
public function action_index()
{
$this->layout->name = 'James';
$this->layout->nest('content', 'home.index');
}
}
views/layouts/default.php:
// head code
<?php echo Section::yield('content') ?>
// footer code
views/home/index.php
<?php Section::start('content'); ?>
<?php echo $name ?>
<?php Section::stop(); ?>
I am greeted with this error: Error rendering view: [home.index] Undefined variable: name. I know that $this->layout->nest('content', 'home.index', array('name' => 'James')); works but that negates my point about having to send all my variables to an array. This can't be the only way.
The view templating docs doesn't seem to touch on doing variables with nested views from controllers.
you can pass variables this way;
class Home_Controller extends Base_Controller {
public $layout = 'layouts.default';
public function action_index()
{
$this->layout->nest('content', 'home.index')
->with('name', 'James');
}
}
Here's an example of how I'm templating with laravel.
Class Products_Controller extends Whatever_Controller {
public $layout = 'layouts.main';
public function get_index()
{
// .. snip ..
$view = View::make('home.product')
->with('product', $product); // passing all of my variable to the view
$this->layout->page_title = $cat_title . $product->title;
$this->layout->meta_desc = $product->description;
$this->layout->content = $view->render(); // notice the render()
}
}
my main layout looks like
<html>
<head>
<title> {{ $page_title }} </title>
<meta name="description" content="{{ $meta_desc }}" />
</head>
<body>
{{ $content }}
</body>
</html>
and the home/product page looks like
<div class="whatev">
<h1> {{ $product->title }} </h1>
<p> {{ $product->description }} </p>
</div>
Hope that helps you clear some things up
I know it has been a while on this question, but since it was asked, Laravel 4 has come out and there are newer ways to do things.
If you are reading this these days you should consider using View Composers to prepare the data for your views.
Example:
class MyViewComposer {
public function compose($view){
$view->title = 'this is my title';
$view->name = 'joe';
...
$view->propertyX = ...;
}
}
After setting up your view composer register it with the app:
View::composer('home.index', 'MyViewComposer');
For more information check out the laravel docs on view composers:
http://laravel.com/docs/responses

Resources