I am about to implement the parser class to my codeigniter project and would like some guidance in passing the data from my model to the parser array. Which is the better more efficient way to do it.
My model gets the data and returns it to the controller. How can I get it into the array in the controller?
Model:
function getSeo($data){
$this->db->select('seo_title, seo_description, seo_keywords');
$this->db->from('content');
$this->db->where($data);
$query = $this->db->get();
$data = array();
foreach ($query->result() as $row) {
$data[] = array(
'seo_title' => $row->seo_title,
'seo_description' => $row->seo_description,
'seo_keywords' => $row->seo_keywords
);
}
return $data;
}
Controller:
$viewdata['pageseo'] = $this->Content_model->getSeo($data);
$viewdata = array(
'seo_title' => 'My seo Title',
'seo_description' => 'My seo description',
'seo_keywords' => 'My seo keywords',
);
What is the best way to get the data from my model into the '$viewdata' array, how is it done????
Since the getSeo function from your model returns an array, the Controller will store this information to your $viewdata array as well.
If you try a print_r($viewdata) you'll see that the structure is as expected. Which is $viewdata['seo_title'] => 'My seo Title'
and so on...
There is function called result_array() which is used to get the result set as array and the below link may help you. This is the core library function.
Plz refer,
http://codeigniter.com/user_guide/database/results.html
Related
I have an array in the post request as an example below:
$data = array(
array('title'=>'1st title', 'desc'=>'desc'),
array('title'=>'2nd title', 'desc'=>'desc'),
array('title'=>'3rd title', 'desc'=>'desc'),
)
Is there a way in Laravel using Eloquent I can save above data without using foreach? Note that the array keys which I am getting in the request is not same as column names of the table.
I hope this would help you
$data = [
['title' => '1st title', 'desc' => 'desc'],
['title' => '2nd title', 'desc' => 'desc']
.....
];
DB::table('users')->insert($data);
Put all the values you want to insert in to an array and then pass it to the insert function.
Source: https://laravel.com/docs/5.1/queries#inserts
try this:
DB::table('table_name')->insert($data);
Using eloquent: just as mentioned by Sethu, but a few lines will be:
Model::insert($data); // eg: Posts::insert($your_request_array);
Just pass in the array directly here: above will return true on success.
I'm learning Laravel and have created a public endpoint where I want to output only certain information of some comments if a user is not authenticated from a GET request.
I have managed to filter out the comments based on whether or not they are approved. I now want to filter out the data that is returned. I have attached a screenshot of what is currently returned.
Ideally, I only want to return the id, name and the body in the json. How can I go about this? I tried the pluck() method which did not give the desired results. Any pointers would be greatly appreciated
public function index(Request $request)
{
if (Auth::guard('api')->check()) {
return Comment::all();
} else {
$comments = Comment::where('approved', 1)->get();
return $comments->pluck('id','name','body');
}
}
To select the particular columns, you can pass columns name to get as
$comments = Comment::where('approved', 1) -> get(['id','name','body']);
You can use a transformer to map the incoming data to a sensible output based on the auth state. The following example comes from the Fractal lib:
<?php
use Acme\Model\Book;
use League\Fractal;
$books = Book::all();
$resource = new Fractal\Resource\Collection($books, function(Book $book) {
return [
'id' => (int) $book->id,
'title' => $book->title,
'year' => $book->yr,
'author' => [
'name' => $book->author_name,
'email' => $book->author_email,
],
'links' => [
[
'rel' => 'self',
'uri' => '/books/'.$book->id,
]
]
];
});
Ideally, you would create 2 classes that extend from Transformer and pass the correct one to the output.
If you want to pass the result as json respose
$comments = Comment::where('approved', 1)->pluck('id','name','body')->toArray();
return Response::json($comments);
If you want to pass the result as to blade
$comments = Comment::where('approved', 1)->pluck('id','name','body')->toArray();
return view('your_blade_name')->with('comments',$comments);
I have this code in layout/formsde/url.phtml:
<?php
$use_url = $this->use_url;
foreach($this->match_de as $k=>$v) {
if($this->serverUrl(true) == $k) {
$use_url = $v;
}
}
?>
I have 1000 pages with following line:
<?=$this->render("layout/formsde/url");?>
Now the problem is $this->use_url and $this->match_de is null its not getting the value from Controllers where it is assigned as below:
return new ViewModel(array(
'description' => $this->de_desc,
'use_url' => $this->layout()->use_url,
'match_de' => $this->layout()->match_de,
));
How can i pass the value to ->render() ? so that i have $this->match_de value with exact which is in controller?
You can pass array with values you need to the render
//Example
$this->render('layout/formsde/url', array(
'use_url' => $this->use_url,
'match_de' => $this->match_de));
Its possible to define variables in the template which is rendered by the view renderer Zend\View\Renderer\PhpRenderer. You can pass an second argument as array.
<?=$this->render("layout/formsde/url", array(
'description' => $this->de_desc,
'use_url' => $this->layout()->use_url,
'match_de' => $this->layout()->match_de,
));?>
More information about PhpRenderer::render() method can be found in the here.
I'm having some trouble updating records with the Codeigniter framework. I'm using the MVC pattern and active records.
public function save_yarn($data)
{
$this->db->select('sub_name');
$this->db->from('mst_subject');
$this->db->insert($data);
}
This function should works for you:
$data = array(
'title' => $title,
'name' => $name,
'date' => $date
);
$this->db->where('id', $id);
$this->db->update('mytable', $data);
https://ellislab.com/codeigniter/user-guide/database/active_record.html#update
If you're using models for your tables, you can do something easy like
$this->yarn_model->update($row_id_to_update, $data);
Otherwise, Angel's method will work too.
I have a Product model for a multi site application.
Depending on the domain(site) I want to load different data.
For example instead of having a name and description fields in my database I have posh_name, cheap_name, posh_description, and cheap_description.
if I set something up like this:
class Product extends AppModel
{
var $virtualFields = array(
'name' => 'posh_name',
'description' => 'posh_description'
);
}
Then it always works, whether accessed directly from the model or via association.
But I need the virtual fields to be different depending on the domain. So first I creating my 2 sets:
var $poshVirtualFields = array(
'name' => 'posh_name',
'description' => 'posh_description'
);
var $cheapVirtualFields = array(
'name' => 'cheap_name',
'description' => 'cheap_description'
);
So these are my 2 sets, but how do I assign the correct one based on domain? I do have a global function called isCheap() that lets me know if I am on the lower end domain or not.
so I tried this:
var $virtualFields = isCheap() ? $this->cheapVirtualFields : $this->poshVirtualFields;
This gives me an error. Apparently you cannot assign variables in a Class definition like this.
So I put this in my Product model instead:
function beforeFind($queryData)
{
$this->virtualFields = isCheap() ? $this->cheapVirtualFields : $this->poshVirtualFields;
return $queryData;
}
This works ONLY when the data is accessed directly from the model, DOES NOT work when the data is accessed via model association.
There has got to be a way to get this to work right. How?
Well if I put it in the constructor instead of the beforeFind callback it seems to work:
class Product extends AppModel
{
var $poshVirtualFields = array(
'name' => 'posh_name',
'description' => 'posh_description'
);
var $cheapVirtualFields = array(
'name' => 'cheap_name',
'description' => 'cheap_description'
);
function __construct($id = false, $table = null, $ds = null) {
parent::__construct($id, $table, $ds);
$this->virtualFields = isCheap() ? $this->cheapVirtualFields : $this->poshVirtualFields;
}
}
However, I am not sure if this is a CakePHP no no that can come back to bite me?
seems like the issue could be that the model association is a model that is built on the fly. eg AppModel
try and do pr(get_class($this->Relation)); in the code and see what the output is, it should be your models name and not AppModel.
also try and use:
var $poshVirtualFields = array(
'name' => 'Model.posh_name',
'description' => 'Model.posh_description'
);
var $cheapVirtualFields = array(
'name' => 'Model.cheap_name',
'description' => 'Model.cheap_description'
);