in my model function i have a query like this:
function update_single($table,$data=array(),$id)
{
if($id!=0)
{
$this->db->trans_start()
->where('id',$id)
->update($table,$data)
->trans_complete();
return TRUE;
}
else
{
return FALSE;
}
}
and i get error message
Fatal error: Call to a member function where() on a non-object in /Applications/MAMP/htdocs/asset/application/models/history/history_model.php on line 1149
According to codeigniter API, trans_start and trans_complete function don't return database object so chaining is not working you have to separate their calls.
$this->db->trans_start();
$this->db->where('id',$id)
->update($table,$data);
$this->db->trans_complete();
Related
so I have a controller which returns a view with a DB Collection which I have a repository,
now, in case I have a request I want to return an array - json and not a view
any ideas if this is the correct approach?
public function index(
Request $request,
CampaignPerformanceRepository $campaignPerformanceRepository
) {
$data = $campaignPerformanceRepository->getDataByPeriod($request);
if ($request->all()) {
return $campaignPerformanceRepository->getDataByPeriod($request);
}
return view('reports.campaign-performances', compact('data'));
}
I do something similar in cases where I may want to return a view or just json based on the request. There's an easy helper function request()->wantsJson() which you can use.
$data = $campaignPerformanceRepository->getDataByPeriod($request);
if ($request->wantsJson()) {
return $campaignPerformanceRepository->getDataByPeriod($request);
}
return view('reports.campaign-performances', compact('data'));
enter image description here
Im new in Laravel i don't how to figure out the mistake
here is my laravel code:
public function login(Request $request)
{
$gen_user = GenUser::where('username',$request->username)->where('confirmed',0)->first();
if($gen_user){
return response()->json(3);+6
}
if($this->auth->attempt($request->only('username','password'))){
$gen_user = GenUser::join('gen_people','gen_users.gen_person_id','=','gen_people.id')
->where('gen_users.id',\Auth::user()->id)
->select(['gen_people.first_name','gen_people.middle_name','gen_people.last_name','gen_people.first_name','gen_users.id','gen_users.username','gen_users.confirmed'])
->first();
// dd($gen_user);
$gen_user->name = $gen_user->first_name." ".$gen_user->middle_name." ".$gen_user->last_name;
if($gen_user->confirmed == 1){
return response()->json($gen_user);
}elseif($gen_user->confirmed == 2){
return response()->json(2);
}else{
return response()->json(3);
}
}else{
return response()->json(false);
}
}
Can someone help me...thanks in advance...
When you use this keyword you mean refer to exactly current class.but in your that controller there is not any auth method or property.
Create authentication(or if you use passport it make it automaticly) and migrate.
After that you can use
Auth::attempt(['username'=>request('username'),'password'=>request('password')])
i want to pass langguageid session from laravel to my vue component.
how i can do it?
i try like this, but not working.
my laravel session function
public function getLanguageSession($langid)
{
if (Session::has('LangSession')) {
Session::forget('LangSession');
$LangSession = Session::put('LangSession', $langid);
$getLangSession = Session::get('LangSession');
} else {
$LangSession = Session::put('LangSession', $langid);
$getLangSession = Session::get('LangSession');
}
return redirect('/');
}
and then i call it to vue function
public function getLanguageStringArray(Request $request, $langid)
{
$getLangSession = Session::get('LangSession');
dd($getLangSession);
}
this code return me null value.
You should not return json data from a controller using dd($getLangSession);, this will give you a totally different output that cannot be parsed by Vue. You have to return the data as a json result to make sure Vue can read this:
public function getLanguageStringArray(Request $request, $langid)
{
$getLangSession = Session::get('LangSession');
return repsonse()->json($getLangSession);
}
SELECT * FROM image;
I am using that query to get data from image table, when i checked it using this query directly into mysql commandline it showed 3 result like this
3 rows in set (0.00 sec);
but, when i try to fetch it from codeigniter using query or active_record, it just show me 1 result. This is my code
class Produk_model extends CI_Model{
function __construct(){
parent::__construct();
}
function get_phome(){
$this->db->select('nama_produk, nama_seo, nama_kategori, seo_kategori, nominal_harga');
$this->db->join('kategori','kategori.id_kategori=produk.id_kategori');
$this->db->join('harga','harga.id_produk=produk.id_produk');
$q=$this->db->get('produk');
if($q->num_rows()){
return $q;
}
}
function get_all_pimage(){
$q=$this->db->get('image');
if($q->num_rows()>0){
return $q;
}
}
function get_by_kategori($kategori_name=''){
$this->db->select('nama_kategori, seo_kategori, nama_produk, nama_seo, nominal_harga, url_img');
$this->db->join('kategori', 'kategori.id_kategori=produk.id_kategori');
$this->db->join('harga','harga.id_produk=produk.id_produk');
$this->db->join('image','image.id_produk=produk.id_produk');
$this->db->where('seo_kategori',$kategori_name);
$q=$this->db->get('produk');
if($q->num_rows()){
return $q;
}
}
function get_by_produk($produk_name=''){
$this->db->select('produk.id_produk, nama_produk, nama_seo, short_desc, min_beli, nominal_harga');
$this->db->join('produk_detil','produk_detil.id_produk=produk.id_produk');
$this->db->join('harga','harga.id_produk=produk.id_produk');
$this->db->where('nama_seo', $produk_name);
$q=$this->db->get('produk');
if($q->num_rows()){
return $q;
}
}
function get_img_by_produk($produk_id=''){
$this->db->select('url_img');
$this->db->where('id_produk', $produk_id);
$q=$this->db->get('image');
if($q->num_rows()){
return $q;
}
}
}
i am trying to use get_all_pimage() with the function below from my controller
function home(){
$this->load->model('produk_model');
$data['konten']=$this->produk_model->get_phome()->result();
$data['img']=$this->produk_model->get_all_pimage()->row_array();
$this->load->view('frontend/home', $data);
}
but it just showed me 1 result, like this :
Array
(
[id_image] => 2
[id_produk] => PRD002
[seo_image] => pastel-1
[url_img] => pastel-1.jpg
)
i am confused, why it can be happened. because i don't know where is the mistake . Thank for your helping
Change function to..
function get_all_pimage(){
$q=$this->db->get('image');
if($q->num_rows()>0){
return $q->result_array();
}
}
Take row->Array() off your call...like so..
$data['img']=$this->produk_model->get_all_pimage();
Then foreach...
foreach($data['img'] as $val){
//display you data
}
I'm using the count_all_results() function to return a user's number of languages spoken. But when I try to pass the number to the view, I keep getting a php undefined variable (for $lang_cnt). Below is my code:
Model
function countLanguages($id) {
$this->db->where('user_id', $id)->from('languages');
return $this->db->count_all_results();
}
Controller
function showLangCount() {
$data['lang_cnt'] = $this->language_model->countLanguages($id);
$this->load->view('lang_view', $data);
}
View
<p>This user speaks <?php echo $lang_cnt; ?> languages.</p>
One problem is that your model function takes two arguments:
function countLanguages($id, $cnt_languages)
But when you call it you are only passing one argument:
$this->language_model->countLanguages($cnt_languages);
And an even bigger problem, as Rocket points out, is that countLanguages doesn't return anything. Try this:
function countLanguages($id) {
$this->db->where('user_id', $id)->from('languages');
return $this->db->count_all_results();
}
Always check your model functions if they return value or not. Try this:
function showLangCount() {
if($this->language_model->countLanguages($id))
{
$data['lang_cnt'] = $this->language_model->countLanguages($id);
}
else
{
$data['lang_cnt'] = NULL;
}
$this->load->view('lang_view', $data);
}
Its better to use:
return $query->num_rows();
to return the number of rows effected...