I've got a controller exposing object-data to a view:
$artifacts = Artifact::with('product');
return View::make('index', compact('artifacts'));
This used to work fine until last week. For some reasons now I can't use the data anymore in the template itself.
//this works
<?php
print_r($artifacts->first());
?>
//this doesnt
#foreach ($artifacts as $artifact)
<?php
print_r($artifact);
?>
#endforeach
when using eager loading, you need to get the actual resultset.
$artifacts = Artifact::with('product')->get();
Related
Iam trying to make this:
I need that when I create a new poll, it loads the aspects and a dropdown with the valorations choices list from the database like this:
I have a table for aspects, and another for valorations, which is a number from 1 to 5.
Normally, when I create a new poll i have to add manually all the rows and select the aspect and the valoration, but in this way its too much job, so i want it to load all the aspects and it would just need to set the valoration for each row and save it. thanks and sorry for my english
As far as I can understand your query, you want something like this:
You have a database table: Aspects (which I expect has static data)
You have a database table: valorat.(which I expect has static data of 1-5)
Now, whenever you create a new poll, it should automatically populate itself with the aspects and valoration dropdown table.
If I am right, you can try the following logic,
You can run the following logic
//run a loop for each aspect row fetched from database
<?php foreach($aspects as $value): ?>
<tr>
<td>
//here goes the id
</td>
<td>
<?= $value ?> //here goes your aspect value
</td>
<td>
<select>
//here run a loop for valorations you fetched from database
<?php foreach($valoration as $key=>$value): ?>
<option value="<?=$key?>"><?= $value ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
<?php endforeach; ?>
In this way, you can achieve the desired table with the aspects and valoration.
If you are using the Yii2 grid view, it is far more simple than this. But without looking at your code and database tables, I can't give you more.
Thank You!! I hope you will get some idea how to do this.
This code works well in the "images" view under images_model:
<?php if($images_model):?>
<?php foreach($images_model as $images):?>
<div class="col-sm-4">
<div class="well">
<img src="<?php echo base_url()."uploads/".$images->name;?>" alt="" class="img-thumbnail">
The code in images_model is the following:
function save_image($data){
$this->db->insert('images', $data);
}
When I try to use the same code in the "wall" view (main_controller) I get the severity notice error saying "images_model" is undefined; even though I load the images_model in the main_controller or auto-load the both models:
$autoload['model'] = array('main_model', 'images_model');
I originally questioned the "foreach" code in the "images" view, but I thought if it works in one view shouldn't it also work in another if I load the related model? It just doesn't seem to be reading the images_model.
I'm just getting to know codeigniter a bit and would appreciate any feedback.
Are you passing the $images_model variable to your view from your main_controller ?
// Set your variable
$data['images_model'] = $images_model;
// Pass variable to view
$this->load->view('main', $data);
You typically shouldn't have access to a model directly in the view (following MVC principals). Your controller should get what it wants from the model, then pass it to the view.
Not sure why it is not getting all the value
In my view
<?php $sum_selfmark =0?>
#foreach
($criteria_criteriamarks as $criteria_criteriamark)
SelfMark:{{$criteria_criteriamark->selfmark}}
<?php $sum_selfmark+= $criteria_criteriamark->selfmark ?>
#endforeach
<p>Total:{{$sum_selfmark}}</p>
In my controller
public function go_to_self_marking($id){
$criteria=Criteria::find($id);
$criteria_criteriamarks =$criteria->criteriamarks;
return view('criterias/self-marking')
->with('criteria_criteriamarks',$criteria_criteriamarks);
}
You are trying to for each a 1 row. Because $criteria=Criteria::find($id); returns only 1 row.
If you want to for each then use $criteria=Criteria::all(); It will return all the row in your Criterias Table.
First and foremost, did you try to die and dump (dd) the $criteria->criteriamarks? So that we can check what's going on with your loop.
Seems like the #foreach loop has no issue.
Currently I am working on a project where views need to be rendered through a custom Blade directive. However I came across a few (limitations?) errors I cannot solve (for a long time).
My custom Blade directive with tree different ways to output a view.
Blade::directive('lwField', function ($expression) {
// 1
return view('lw::module.field.field')->render();
// 2
return Blade::compileString('{!! view("lw::module.field.field")->render() !!}');
// 3
return Blade::compileString('#include("lw::module.field.field")');
});
The field view:
Get to the choppa!
The main view:
#extends('layout.default')
#section('main_content')
#lwField()
#endsection
The layout file:
// ... some cool html
#section ('main_content')
#show
// ... even more cool html
The following happens when I execute the three methods separately:
1 return view('lw::module.field.field')->render();
The fist time it executes it throws an error:
include(/somepath/storage/framework/views/b30c24f5b8fd420ef1a08edb52e92174e2dfe911.php): failed to open stream: No such file or directory (View: /somepath/resources/views/page/default.blade.php)
This is true since there is only one view in my cached folder:
// 5b27802352643346357e49b847d934736c36cd07.php
// The main view with the field view
<?php $__env->startSection('main_content'); ?>
Get to the choppa!
<?php $__env->stopSection(); ?>
The second time I run this it magically works. It will generate 3 other files:
// b30c24f5b8fd420ef1a08edb52e92174e2dfe911.php
// the main view with yielding layout view without field view
<?php $__env->startSection('main_content'); ?>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layout.default', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>
// e4cec91e7d4adb5dac5c63e5bfa85ba9a258f664.php
// the layout file
// ...
<?php endforeach; $__env->popLoop(); $loop = $__env->getFirstLoop(); ?>
// ...
// ffd60653d8490007c272c527abef3a5ede092a33.php
// layout view and main view
// ...
<?php $__env->startSection('main_content'); ?>
<?php echo $__env->yieldSection(); ?>
// ...
It looks like it generates two files that are the same but one without the field view.
2 return Blade::compileString('{!! view("lw::module.field.field")->render() !!}');
3 return Blade::compileString('#include("lw::module.field.field")');
These methods will return a blanko page and generate two files in cache without showing/logging any errors.
// 5b27802352643346357e49b847d934736c36cd07.php
// Field view
Get to the choppa!
// b30c24f5b8fd420ef1a08edb52e92174e2dfe911.php
// Main view and field view
<?php $__env->startSection('main_content'); ?>
<?php echo view("lw::module.field.field")->render(); ?>
<?php $__env->stopSection(); ?>
This method will not generate the layout file.
Does anyone have a clue on what is happening. Maybe this kind of behavior is not strange at all and this is how Blade should work.
I have been working on a project that required me to do the same thing, the problem is that making a new Blade directive is not a good practice when you want to render another view, It's because of the Cache, every Blade directive is being cached so if you want to render dynamic data it will cause you a lot of problems.
Since I have tried a lot of options and you want to render another view file and not just push html to the blade directive, I suggest you to make a new helper or even a service that you can pass there the parameters and it will return the render function that outputs clean HTML so this way you can be sure that your content will be always dynamic and won't be cached.
TL;DR
Example:
In your helpers functions file put:
function render_my_view() {
return view(''lw::module.field.field'')->render();
}
So then in yout blade file you can use:
{!! render_my_view() !!}
I got a problem about browser cache when I already add/edit record in database. The data in database changed but in view not change. I must press F5 in browser for refresh that page and it'll show updated data.
Please guide to me how to solve this problem, Thanks.
Sorry I forgot to told you guys, In my application has two part.
Admin part (Backend) for add/edit data. (There's not any problem in
this part)
Front part for show data to viewer (Problem here! After
add/edit record in Admin part, this part not changed. I must refresh
page to see update)
This's my relevant code in Front part.
In Controller
class ProductController extends Controller
{
public function actionIndex()
{
$products = Product::model()->getProductList(); // use findAll() method
$this->render('index', array('products' => $products));
}
}
In Model
class Product extents CActiveRecord
{
/*
*
* The other code that generated by Gii
*
*/
public function getProductList()
{
return $this->findAll();
}
}
In View
<html>
<body>
<ul class="thumbnails thumbnails-product">
<?php foreach ($products as $product): ?>
<li class="span3" style="margin-left:0px;">
<div class="thumbnail">
<img class="img-thumb" src="../images/<?php echo $product->PDT_IMG" />
<?php echo CHtml::link($product->PDT_NAME, array('product/view/' . $product->PDT_ID)); ?>
<span class="price"><?php echo $product->PDT_PRICE; ?></span>
</div>
</li>
<?php endforeach; ?>
</ul>
</body>
</html>
If you want something to update on-the-fly, I think you're looking for in place editing.
You could take a look at this extension which does that for you.
Especially this part.
May be this can help you out:
Real-time display of server push data using Server-Sent Events (SSE)
this is server push technique by which we can fetch data in every changes made automatically and also after a certain time.
The problem has been solved.
I just add
header ("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");
Everytime that view load, it'll reread page and doesn't keep any cache.