CI_DB_mysql_result Object([conn_id] => Resource id #28[result_id] => Resource id #33[result_array] => Array ()[result_object] => Array()[custom_result_object] => Array()
[current_row] => 0 [num_rows] => 1[row_data] =>)
here is my model page
public function getProductsListByCategory1($limit, $start)
{
$this->db->limit($limit, $start);
$this->db->select('image_path,tagline,category_id,product_id');
$this->db->where('category_id','1');
$query = $this->db->get("tb1_bl_products order by tagline");
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$data[] = $row;
}
return $data;
}
else
{
return array();
}
}
use $query->result_array() rather than $query->result()
Related
[submit function][1]error message while insertingfunction for inserting tagserror while inserting into database many tags using foreign key .I am getting problem in array , it is unable to insert many tags
public function uploadSubmit(UploadRequest $request)
{
$product = new Product();
$product->name = $request['name'];
$product->save();
//$product = Product::create($request->all());
foreach ($request->photos as $photo) {
$filename = $photo->store('photos');
ProductsPhoto::create([
'product_id' => $product->id,
'filename' => $filename
]);
}
$tags = array();
$tags = array($request['tagname[]']);
foreach ($tags as $tag) {
ProductTag::create([
'tag_id' => $product->id,
'tagname' => $tag,
]);
}
$message = 'Upload successful!';
return redirect('/upload')->with('msg' , $message);
}
[Environment & details:[][2]][sql error 3]
Ok - so after learning a bit more about your current setup I think you would benefit from re-structuring it to the following:
Model Product to have tags() relationship method:
public function tags(): MorphToMany
{
return $this->morphToMany(Tag::class, 'taggable');
}
Add intermediate table taggable:
php artisan make:migration create_taggables_table --create=taggables
class CreateTaggablesTable extends Migration
{
public function up()
{
Schema::create('taggables', function (Blueprint $table) {
$table->unsignedInteger('tag_id');
$table->unsignedInteger('taggable_id');
$table->string('taggable_type');
});
}
public function down()
{
Schema::dropIfExists('taggables');
}
}
Update your controller to add new tags, then associate them with product:
public function uploadSubmit(UploadRequest $request)
{
$product = new Product;
$product->name = $request->name;
$product->save();
$photos = collect($request->photos)->map(function($photo) use ($product) {
return [
'product_id' => $product->id,
'filename' => $photo->store('photos')
];
})->toArray();
ProductsPhoto::insert($photos);
$tagsInUse = Tag::whereIn('name', $request->tagname);
if (count($request->tagname) > $tagsInUse->count()) {
$newTags = collect($request->tagname)->diff($tagsInUse->pluck('name')->values())->map(function (string $tag) {
return ['name' => $tag];
});
Tag::insert($newTags->toArray());
$tagIds = Tag::whereIn('name', $request->tagname)->pluck('id')->values();
} else {
$tagIds = $tagsInUse->pluck('id')->values();
}
$product->tags()->attach($tagIds);
return redirect('/upload')->with('msg' , 'Upload successful!');
}
And here's a test for this setup:
public function test()
{
factory(Tag::class)->create(['name' => 'one']);
factory(Tag::class)->create(['name' => 'two']);
factory(Tag::class)->create(['name' => 'three']);
$this->assertEquals(
['one', 'two', 'three'],
Tag::all()->pluck('name')->toArray()
);
$requestTags = new Collection(['one', 'three', 'four', 'five']);
$inUse = Tag::whereIn('name', $requestTags)->pluck('name')->values();
$newTags = collect($requestTags)->diff($inUse)->map(function(string $tag) {
return ['name' => $tag];
});
Tag::insert($newTags->toArray());
$this->assertEquals(
['one', 'two', 'three', 'four', 'five'],
Tag::all()->pluck('name')->toArray()
);
$product = factory(Product::class)->create(['name' => 'Bike']);
$this->assertEmpty($product->tags);
$tagIds = Tag::whereIn('name', $requestTags)->pluck('id')->values();
$product->tags()->attach($tagIds);
$this->assertEquals(
['one', 'three', 'four', 'five'],
$product->fresh('tags')->tags->pluck('name')->toArray()
);
}
Reference: https://laravel.com/docs/5.6/eloquent-relationships#many-to-many-polymorphic-relations
I have to lots of paginations one lot is for my user's results and the other is for my question results.
When I am on link 3 on my question results it also switches the results on users list pagination.
Question if I click on the questions pagination links how can I make sure it does not affect the results of the user's list.
I have tried this Best way to do multiple pagination on one page in codeigniter does not work
As you can see in image below because I am on questions list page 3 it has effected the users list
<?php
class Dashboard extends MY_Controller {
public $data = array();
public function __construct() {
parent::__construct();
$this->load->model('user/user_model');
$this->load->model('forum/question_model');
$this->load->library('pagination');
}
public function index() {
$this->data['title'] = 'Dashboard';
$this->data['is_logged'] = $this->session->userdata('is_logged');
$config1['base_url'] = base_url('dashboard/');
$config1['total_rows'] = $this->user_model->total_users();
$config1['per_page'] = 2;
$config1['uri_segment'] = 2;
$config1['num_links'] = 200;
$config1['use_page_numbers'] = FALSE;
$config1['prefix'] = 'u_';
$pagination1 = new CI_Pagination();
$pagination1->initialize($config1);
$this->data['pagination1'] = $pagination1->create_links();
$page_segment1 = explode('_', $this->uri->segment(2));
$page1 = ($this->uri->segment(2)) ? $page_segment1[1] : 0;
$this->data['users'] = array();
$users = $this->user_model->get_users($config1['per_page'], $page1);
foreach ($users as $user) {
$this->data['users'][] = array(
'user_id' => $user['user_id'],
'username' => $user['username'],
'status' => ($user['status']) ? 'Enabled' : 'Disabled',
'warning' => '0' . '%',
'date' => date('d-m-Y H:i:s A', $user['date_created_on']),
'href' => site_url('user/profile/') . $user['user_id']
);
}
// Questions Pagination & Results
$config2['base_url'] = base_url('dashboard/');
$config2['total_rows'] = $this->question_model->total_questions();
$config2['per_page'] = 2;
$config2['uri_segment'] = 2;
$config2['num_links'] = 200;
$config2['use_page_numbers'] = FALSE;
$config2['prefix'] = 'q_';
$pagination2 = new CI_Pagination();
$pagination2->initialize($config2);
$this->data['pagination2'] = $pagination2->create_links();
$page_segment2 = explode('_', $this->uri->segment(2));
$page2 = ($this->uri->segment(2)) ? $page_segment2[1] : 0;
$this->data['questions'] = array();
$questions = $this->question_model->get_questions($config2['per_page'], $page2);
foreach ($questions as $question) {
$this->data['questions'][] = array(
'user_id' => $question['user_id'],
'title' => $question['title']
);
}
$this->data['navbar'] = $this->load->view('common/navbar', $this->data, TRUE);
$this->data['header'] = $this->load->view('common/header', $this->data, TRUE);
$this->data['footer'] = $this->load->view('common/footer', '', TRUE);
$this->load->view('common/dashboard', $this->data);
}
}
Question Model
<?php
class Question_model extends CI_Model {
public function get_questions($limit, $start) {
$this->db->select('*');
$this->db->from('questions q');
$this->db->join('users u', 'u.user_id = q.user_id', 'left');
$this->db->limit($limit, $start);
$query = $this->db->get();
return $query->result_array();
}
public function total_questions() {
return $this->db->count_all("questions");
}
}
Users Model
<?php
class User_model extends CI_Model {
public function get_users($limit, $start) {
$this->db->select('u.status, u.date_created_on, ud.*');
$this->db->from('users u', 'left');
$this->db->join('users_userdata ud', 'ud.user_id = u.user_id', 'left');
$this->db->limit($limit, $start);
$query = $this->db->get();
return $query->result_array();
}
public function total_users() {
return $this->db->count_all("users");
}
}
I get an error 'Trying to get property of non-object' in my service provider when I used Auth :
public function boot()
{
$roles = DB::table('folders')->orderBy('folder_id', 'desc')->where('level', 0)->get();
if(Auth::user()->level == 1){
$roles1 = DB::table('folders')->orderBy('folder_id', 'asc')->where('level', 1)->get();
}else{
$user = DB::table('folder_permissions')->where('user_id', Auth::user()->id)->get();
foreach($user as $u){
$roles1 = DB::table('folders')->orderBy('folder_id', 'asc')->where('level', 1)->get();
}
}
$treeFolder = DB::table('folders')->where('level', 0)->get();
if(!empty($treeFolder)){
foreach($treeFolder as $folders){
$arrayCategories[$folders->folder_id] = array("parent_id" => $folders->parent, "name" => array("fname" => $folders->folder_name, "id" => $folders->folder_id));
}
}else{
$arrayCategories = FALSE;
}
view()->share(['folder' => $roles, 'prime_folders' => $roles1, 'treeView' => $arrayCategories]);
}
I already called 'use Illuminate\Support\Facades\Auth;', but nothing happen.
Can somebody help me ?
it seems that, Auth is loading before everything get ready. you just follow this, it works
view()->composer('*', function ($view)
{
$roles = DB::table('folders')->orderBy('folder_id', 'desc')->where('level', 0)->get();
if(Auth::user()->level == 1){
$roles1 = DB::table('folders')->orderBy('folder_id', 'asc')->where('level', 1)->get();
}else{
$user = DB::table('folder_permissions')->where('user_id', Auth::user()->id)->get();
foreach($user as $u){
$roles1 = DB::table('folders')->orderBy('folder_id', 'asc')->where('level', 1)->get();
}
}
$treeFolder = DB::table('folders')->where('level', 0)->get();
if(!empty($treeFolder)){
foreach($treeFolder as $folders){
$arrayCategories[$folders->folder_id] = array("parent_id" => $folders->parent, "name" => array("fname" => $folders->folder_name, "id" => $folders->folder_id));
}
}else{
$arrayCategories = FALSE;
}
//if this line doesn't work then.... see below line after this coming up line
$view->share(['folder' => $roles, 'prime_folders' => $roles1, 'treeView' => $arrayCategories]);
$view->with('folder', $roles)->with('prime_folders',$roles1)->with('treeView',$arrayCategories);
});
My controller code
function edtpost($id)
{
$this->load->model('post');
$data = $this->post->edt_post($id);
$this->load->model('category');
$data['catname'] = $this->category->retrivecat();
$this->load->view('dashboard/edit_post',$data);
}
my model code
model post
public function edt_post($id)
{
$query = $this->db->get_where('post', array('id' => $id));
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
return $row;
}
}
return false;
}
category model
public function retrivecat()
{
$query = $this->db->get('category');
foreach ($query->result() as $row)
{
$data[] = $row->catname;
}
return $data;
}
in controller & this code $data['catname'] = $this->category->retrivecat();
I have one error:
Cannot use object of type stdClass as array
Just change your that line with following :-
$data->catname = $this->category->retrivecat();
http error occured while calling data from model using function
model
public function getProductCombo() {
$q = $this->db->get_where('products', array('type' => 'combo'));
if ($q->num_rows() > 0) {
foreach (($q->result()) as $row) {
$data[] = $row;
}
return $data;
}
}
controller
function sets() {
$this->sma->checkPermissions();
$this->load->helper('security');
$this->data['error'] = (validation_errors() ? validation_errors() :
$this->session->flashdata('error'));
// problem in this line also
$this->data['showcombo'] = $this->load->sales_model->getComboProduct();
$bc = array(array('link' => base_url(),
'page' => lang('home')),
array('link' => site_url('sales'),
'page' => lang('products')),
array('link' => '#', 'page' => "sets")
);
$meta = array('page_title' => "Add Sets", 'bc' => $bc);
$this->page_construct('sales/sets', $meta, $this->data);
}
First of all, No need to include the curly braces for $q->result
foreach ($q->result as $row)
{
$data[] = $row;
}
No need to use validation_errors in your php file.You can directly load your form page.Use validation_errors() in view page.
In your Controller, do this
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
Then in your formpage you can echo
<?php echo validation_errors(); ?>
change this line to
$this->data['showcombo'] = $this->load->sales_model->getComboProduct();
this
$this->data['showcombo'] = $this->load->sales_model->getProductCombo();
Because your
model name is
public function getProductCombo()
{
}
Firstly you load model in controller. And then called function, which you have defined in model..
$this->load->model('sales_model','sales'); // sales is alias name of model name
$this->data['showcombo'] = $this->sales->getComboProduct();