codeigniter 4 mysqli_sql_exception #1064 in view - codeigniter

It's very strange for me. Because in codeigniter 4 one of my function is working in controller but not working in view. I am getting my data's from database in controller, but when I am trying to get same data from view it showing mysqli_sql_exception #1064.
Example codes:
In Controller (MyData.php)
function get_data($status){
return $this->Data->get_data($status);
}
function view_data(){
return view('user/view-data',['data'=>$this])
}
In Model (MyData.php)
function get_data($status="approved"){
return $this->select()->where('status',$status)->get()->getResultArray();
}
In View (MyData.php)
$datas = $data->get_data('approved');
Error
mysqli_sql_exception #1064
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '*, *, *

I believe your view_data function should be
function view_data(){
return view('user/view-data',['data'=>$this->get_data('approved')])
}
Then in view just loop through the $data array
Views are places that you MUST NOT need to execute sql queries. You need to pass only data (objects, arrays, string, int, etc.) to views.

Related

Laravel6 WhereHas Error 500 when using AJAX

im new to Laravel and facing an interesting Issue right now in my App.
I have 3 tables.
Producers
id
producer_name
Types
id
type_name
Models
id
model_name
device_type_id
device_producer_id
Within my Producers Model I have defined the follwing Filter method:
public function scopeFilterByType($query, $type_id)
{
$query->whereHas('models', function($q) use $type_id { $q->where('device_type_id', $type_id );});
}
Using Tinker I can do the following:
App\DeviceProducer::filterByType(3)->get()
And get full response with my Producers associated to my given type.
I created an Function so when a user select a device type Ajax will load all Producers from this type.
public function reqProducer(Request $request)
{
$producers = DeviceProducer::filterByType($request->type_id)->get();
return response()->json( $producers );
}
But when AJAX is calling my endpoint it gets HTTP500 error.
I figured out when using a request without WhereHas for example:
$producers = DeviceProducer::where('id', $request->producer_id)->get();
It just works fine and I get my results. So it seems have to do something with "WhereHas". I know I could Solve this by first asking Models Table and the creating an Foreach loop. But I this solution would be less readable then my first attempt.
Does anyone has an suggestion what im doing wrong or is it just like there is noch AJAX support for WhereHas querys?
Kind regards
Mike
I think this is your issue use $type_id
Please fix as
public function scopeFilterByType($query, $type_id)
{
$query->whereHas('models', function($q) use ($type_id) { $q->where('device_type_id', $type_id );});
}

how to load different data tables in different tabs?

i am using laravel data tables to generate different data table on different tabs. but it runs only query of first data table rendered to the page.i already have attached scope to data table to generate different results for different condition
i have attached ajax() method to the data table that are not displayed in the beginning of the page
controller file
public function index(UpcomingReservationDataTable $UpcomingReservationDataTable)
{
return $UpcomingReservationDataTable->render('admin.reservations.index');
}
public function getAllReservationTable(ReservationDataTable $ReservationDataTable){
return $ReservationDataTable->render('admin.reservations.index');
}
public function getPastReservationDataTable(PastReservationDataTable $PastReservationDataTable){
return $PastReservationDataTable->addScope(new PastReservationScope)->render('admin.reservations.index');
}
pastreservationdatatable query
public function query(Reservation $model)
{
return $model->newQuery()->where('pickup_date','<',date('Y-m-d'))->orderBy('id','desc')->with('customer','package','vehicle');
}
You can show first table in load tab and next change it by ajax.
You must write a function in javascript. This function should take a value. This value contains your query information. When this is clicked on any tab, this function must be executed with that value. The function of this function is to send information to the server, receive and display. Did you do these things?
The next issue is when the Ajax data is sent, if the server path is wrong, you will encounter an error. To view errors on your browser, press the combination keys CTR + Shift + i and in the menu at the top of the window, select the console to display the errors.

upload a row to database table in laravel 4.2

Use Illuminate\Http\Request;
public function registerAStudent(Request $request)
{
Student::Create($request->all());
}
I want to upload a row to the database table.
Form data is stored in the request variable and using Student model.
but can't update the database I'm getting following error:
ErrorException (E_RECOVERABLE_ERROR) HELP Argument 1 passed to
AdminController::registerAStudent() must be an instance of
Illuminate\Http\Request, none given
please give me a proper way to do it.

Laravel 5.4 Getting Call to a member function fill() on null

I am trying to save image on database to be used as background for the website. The problem is, I am getting Call to a member function fill() on null
Here is what I did
https://paste.laravel.io/ZoWBM
This simply means there is no any record in SiteSettings model with name_setting = $key.
You should always do a check like this:
if (is_null($siteSettingsUpdate)) {
// There is no such record in DB.
}

View Same user_id

//Anyone can help to create a view data with same id? it is a multiple viewing.
this is my Controller. i dont khow apply in Model and View
function Get_Pitch($id){
$this->load->model('users_model');
$data['query'] = $id;
$this->load->view('view_pitch', $data);
}
Example this is my url "http://localhost/SMS_System/home/sample/102"
in my database is
id=1 name=erwin user_id=102
id=2 name=flores user_id=102
id=3 name=sample user_id=202
how to view the same user_id?
First of all with what you've supplied your URL won't work, you aren't following the normal conventions for CI so it won't know where to look. I am assuming your controller is called sample then you need to tell the application which function you're calling in that controller, finally URL names should be lower case so I changed that, so your URL should read:
"http://localhost/SMS_System/home/sample/get_pitch/102"
Also you need to get your data from a model, you loaded the model then didn't use it. The line after loading the model calls a function from that model and passes it the id you got from your url. Notice the if not isset on the id, this ensures that if someone goes to that page without the id segment there are no errors thrown from the model having a missing parameter, it will just return nothing, that is handled in the view.
Controller:
function get_pitch($id){
//the following line gets the id based on the segment it's in in the URL
$id=$this->uri_segment(3);
if(!isset($id))
{
$id = 0;
}
$this->load->model('users_model');
$data['query'] = $this->users_model->getUserData($id);
$this->load->view('view_pitch', $data);
}
Your model takes the id passed from the controller and uses that to retrieve the data from the database. I normally create the array I am going to return as an empty array and handle that in the view, this makes sure you get no errors if the query fails. The data then returns to the controller in the last line and is passed to the view in your load view call.
Model:
function getUserData($id)
{
$this->db->where('id',$id);
$result = $this->db->get('users') //assuming the table is named users
$data = array(); //create empty array so we aren't returning nothing if the query fails
if ($result->num_rows()==1) //only return data if we get only one result
{
$data = $result->result_array();
}
return $data;
}
Your view then takes the data it received from the model via the controller and displays it if present, if the data is not present it displays an error stating the user does not exist.
View:
if(isset($query['id']))
{
echo $query['id']; //the variable is the array we created inside the $data variable in the controller.
echo $query['name'];
echo $query['user_id'];
} else {
echo 'That user does not exist';
}

Resources