I have this model in my Codeigniter setup:
function get_all_artists(){
$this->db->select('artist_id, artist_name');
$this->db->from('artists');
return $this->db->get();
}
How do I fetch those parameters in my Rest_Controller setup? How would the _get()-function look like?
public function artists_get()
{
???
}
Thanks in advance
Try
public function artists_get(){
$this->load->model('your_model_name');// load model here
$res = $this->your_model_name->get_all_artists();
$result = $res->result_array();// get result as associative array
return json_encode($result);// encode with json
}
With this library you need to append the method type to the function name. _get forGET, _post for POST, _put for PUT, _delete for DELETE. e.g. functionname_get().
As noted in another answer you'll want to retrieve results as an array. So $this->db->get()->result_array();
To retrieve data from the model:
$this->load->model('model_name');
$artists = $this->model_name->get_all_artists();
If you want to send json to front end then you need to do something like this:
$vars['artists'] = json_encode($artists);
$this->_getTemplate('default')->build('artists_page', $vars);
If you are using the REST EXTENSION for CodeIgniter then you must create an object and invoke the method "Respose".
public function artists_get() {
$response = array(
'status'=>TRUE,
'data'=>$result, // This can be an object or array
'obs'=>'all',
);
$this->response($response);
}
That's the official way to do it... and it works perfectly!
Related
guys i trying to filter my data from mysql with where clause but after put secound value laravel give me a blank result? If i try to filtered with first value example like this : http://localhost/transport/1 everything is good but if i try to set from destionation give me a blank result. example with fail : http://localhost/transport/1/Германия
Here is my Controller
class TransportController extends Controller
{
public function filtermethod($method){
$data['ads'] = db::table('ads')->where('method', $method)->get();
return view('transport', $data );
}
public function regionfrom($from){
$data['ads'] = db::table('ads')->where('from', $from)->get();
return view('transport', $data );
}
Here is my routes :
Route::get('transport/{method}', 'TransportController#filtermethod');
Route::get('transport/{method}/{from}', 'TransportController#regionfrom');
Your second route should be giving your controller 2 variables.
public function regionfrom($method, $from)
Is what your route your having problems with is calling, do the logic you like in there.
If you would like to filter twice, try this:
$data = DB::table('ads')-where('method', $method)->where('region', $region)->get();
I have this function in one of my controllers:
public function getApplicationFiles(Contract $contract) {
dd($contract->get());
}
The parameter is defind in the url. So my route is
Route::get('contracts/files/{contract}', 'ContractController#getApplicationFiles');
My problem is that the function getApplicationFiles displays all entries from the Contract type and not only the Object with the given id?
What am I doing wrong?
I would go with the following, make your route a named route like this:
Route::get('contracts/files/{contract}', 'ContractController#getApplicationFiles')->name('contract.files');
Then in your view use it like this:
Files
This should give you the expected result.
u don't need to pass all row u need to pass only one row try like this(if you are paassing only contract id in request)
public function getApplicationFiles(Contract $contract) {
//dd($contract);
return response()->json($contract); // if you pass json
return view('show',compact($contract); /// if you view
}
I need a little help and I can’t find an answer. I would like to replicate a row from one data table to another. My code is:
public function getClone($id) {
$item = Post::find($id);
$clone = $item->replicate();
unset($clone['name'],$clone['price']);
$data = json_decode($clone, true);
Order::create($data);
$orders = Order::orderBy('price', 'asc')->paginate(5);
return redirect ('/orders')->with('success', 'Success');
}
and i got an error :
"Missing argument 1 for
App\Http\Controllers\OrdersController::getClone()"
.
I have two models: Post and Order. After trying to walk around and write something like this:
public function getClone(Post $id) {
...
}
I got another error
Method replicate does not exist.
Where‘s my mistake? What wrong have i done? Maybe i should use another function? Do i need any additional file or code snippet used for json_decode ?
First of all, make sure your controller gets the $id parameter - you can read more about how routing works in Laravel here: https://laravel.com/docs/5.4/routing
Route::get('getClone/{id}','YourController#getClone');
Then, call the URL that contains the ID, e.g.:
localhost:8000/getClone/5
If you want to create an Order object based on a Post object, the following code will do the trick:
public function getClone($id) {
// find post with given ID
$post = Post::findOrFail($id);
// get all Post attributes
$data = $post->attributesToArray();
// remove name and price attributes
$data = array_except($data, ['name', 'price']);
// create new Order based on Post's data
$order = Order::create($data);
return redirect ('/orders')->with('success', 'Success');
}
By writing
public function getClone(Post $id)
you are telling the script that this function needs a variable $id from class Post, so you can rewrite this code like this :
public function getClone(){
$id = new Post;
}
However, in your case this does not make any sence, because you need and integer, from which you can find the required model.
To make things correct, you should look at your routes, because the url that executes this function is not correct, for example, if you have defined a route like this :
Route::get('getClone/{id}','YourController#getClone');
then the Url you are looking for is something like this :
localhost:8000/getClone/5
So that "5" is the actual ID of the post, and if its correct, then Post::find($id) will return the post and you will be able to replicate it, if not, it will return null and you will not be able to do so.
$item = Post::find($id);
if(!$item){
abort(404)
}
Using this will make a 404 page not found error, meaning that the ID is incorrect.
I'm freshman in CI.
Today I got an error like this when I learned CI tutorial.
You must use the "set" method to update an entry.
Filename:
F:\xampp\htdocs\ISA2013\ISA2013\system\database\DB_active_rec.php
Line Number: 1174
My Model code like this.
class Apply_model extends CI_Model {
public function add_record($data){
$query = $this->db->insert('help_user');
//return;
}
}
And I just coded like that in Control:
public function create(){
$data = array('USER_NUMBER' => $this->input->post('stuNO'),
'USER_EMAIL' => $this->input->post('email'),
'USER_INFO' => $this->input->post('info')
);
$this->apply_model->add_record($data);
$this->index();
}
..when I run class/create, I got the top of error..
Can someone help me?
You need to pass your data as parameter to $this->db->insert function as mentioned by nevermind
As mentioned by nevermind, you have to pass data array to the insert function. Also please note that data array should be an associative array, where keys will be the table fields and values will be values for the fields.
You don't pass the parameter data into $this->db->insert function. You can change the code line :
$query = $this->db->insert('help_user');
to:
$query = $this->db->insert('help_user', $data);
on your model you must like this
public function add_record($data)
{
$this->db->insert('help_user',$data); //it's more be simple
}
if you write a return; you will return a null value to your controller
if you write like
$query = $this->db->insert('help_user');
you just make a variable $query with value $this->db->insert('help_user'); so thats make a error
hey buddy you are missing one parameter and you are not returning the value, you should have
return $this->db->insert('help_user', $data);
I'm trying to learn the use of the Zend Framework and I am facing now the following issue.
I am reading some information from the database for a specific Post. I use Datamapper and Models.
$postMapper = new Application_Model_PostMapper();
$post = new Application_Model_Post();
$details = $postMapper->find($postID, $post);
$this->view->postDetail = $details;
In my View, I use a foreach($this->postDetail as $value) to read all the Post Information. But I was wondering now, if I can also access an Information without the foreach. I need just the Email Adress in the Controller and can't see why I would need a foreach. But how would I access this? A Zend_Debug comes with the following results:
array(1) {
[0] => object(Application_Model_Post)#87 (27) {
["_email":protected] => string(10) "test#testmail.com"
It does sound like a very stupid question, but I just don't find a way to read out the Email Adress inside the Controller. Can someone give me a hint?
In your Application_Model_Post class, you would create an accessor method in order to get the private value.
So create a method like this.
public function getEmail(){
return $this->_email;
}
To be honest, I think you will already have these methods if you are using a datamapper correctly.
When you get data from Model/db by fetchAll, eg:
$result = $this->fetchAll($select);
you can
$result->toArray();
//access like array
$result[0]->some_col_1;
$result[0]->some_col_2;
$result[1]->some_col_1;
$result[1]->some_col_2;
...
one of these should work:
if $details returns an array: $email = $details['email']; or =$details[0]['email'];
if $details returns an object: $email = $details->email;