The table does not exist: {{%active_record}} - yii2

I am new to yii and learing generating the CRUD operation through gii first i create a country table and generate the model and controller and views using gii i am get following error
Invalid Configuration – yii\base\InvalidConfigException The table does not exist: {{%country_search}}

Add this to CountrySearch class:
/**
* #inheritdoc
*/
public static function tableName()
{
return 'country';
}

Just adding to temirbek answer:
file models/Country.php
<?php
namespace app\models;
use yii\db\ActiveRecord;
class Country extends ActiveRecord
{
public static function tableName()
{
return 'country';
}
}

Related

How would i orderBy my query based on values from a diffrent tabel that it is in a relationship with

i have 2 tabels one being vehicles and another being road tax.
my 'vehicles' tabel has an id & registration field which is in relationship with my 'road tax' tabel which has id, vehicle_id, vaild from & an expires field. i have a one to many relationship as my vehicles will have had many years history of when i taxed them
i need the most simple way to list all my vehicles in order of which will need to be re-taxed first.
the closest i have is getting my vehicles to list when the tax is due to expire. i am really strugling to get them in the order i need. i have a basic understanding of php and mysql so hoping someone can shine a light on where i need to focus. i thought i could either just orderBy the expires colum, just like how i can successfully orderBy registration. is this because my expires field originate from a realtionship table?
controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Road_tax;
use App\Models\Vehicle;
use Carbon\Carbon;
class DashboardController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Contracts\Support\Renderable
*/
public function Index()
{
$road_taxes = Vehicle::with('latest_Road_Tax')->get()
return view('dashboard.index', compact('road_taxes'));
}
}
Vehicle Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Vehicle extends Model
{
public function Road_taxes()
{
return $this->hasMany(Road_tax::class);
}
public function latest_Road_Tax()
{
return $this->hasOne(Road_tax::class)->latest("expires");
}
}
View
#foreach($road_taxes as $road_tax)
<div class="dashboard-item-title">
<h6 style="font-weight:600; margin-bottom:0px;">{{$road_tax->registration}}</h6>
<span class="dashboard-item-body" style="margin-top:-10px;">
<small style="font-weight:300; color:grey;">Tax expires for this vehicle on</small>
<small style="font-weight:300"> | {{$road_tax->latest_Road_Tax->expires}}</small>
</span>
</div>
#endforeach
You can do is a with() method and pass as a query builder.
So basically you only need 1 relationship vehicle->hasMany(Road_tax::class)
Your model should be:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Vehicle extends Model
{
public function road_taxes()
{
return $this->hasMany(Road_tax::class);
}
}
So if you want every vehicle to list their latest road tax
You can query this using with()
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Vehicle;
use Carbon\Carbon;
class DashboardController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Contracts\Support\Renderable
*/
public function Index()
{
$road_taxes = Vehicle::with([
'road_taxes' => function ($query) {
$query->lastest()->limit(1);
}
])->get();
return view('dashboard.index', compact('road_taxes'));
}
}
This method will list 1 road taxes associated to the vehicle
EDITED
This is the where you only get the vehicle with road road taxes
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Vehicle;
use Carbon\Carbon;
class DashboardController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Contracts\Support\Renderable
*/
public function Index()
{
$road_taxes = Vehicle::with([
'road_taxes' => function ($query) {
$query->lastest()->limit(1);
}
])->has('road_taxes')->get();
return view('dashboard.index', compact('road_taxes'));
}
}

How to check input data in YII2 for REST API?

How to check input data in YII2 for REST API?
Here's how it's done in a non-REST API:
Controller
<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
use app\models\Index__GET;
class SiteController extends Controller
{
public function actionIndex($ch_name_url = null)
{
$model = new Index__GET();
$model->ch_name_url = $ch_name_url;
if($model->validate()){
return $this->render('index');
}
}
}
Model
<?php
namespace app\models;
use Yii;
use yii\base\Model;
class Index__GET extends Model
{
public $ch_name_url;
public function rules()
{
return [
['ch_name_url', 'trim'],
['ch_name_url', 'required'],
];
}
}
And now in the controller call $model->validate() for data validation. How to do validation incoming data in the REST API, using yii\rest\Controller and yii\rest\ActiveController?
I try but data validation fails:
I want a GET request to include two required fields.
But if I use /users/123 I will receive data, while I should not receive it, because of the model [['id', 'ch_name_url'], 'required'],.
Me need /users?id=123&ch_name_url=myname
Controller
namespace app\controllers;
use yii\rest\ActiveController;
class IndexController extends ActiveController
{
public $modelClass = 'app\models\Index__GET';
}
Model
<?php
namespace app\models;
use Yii;
use yii\db\ActiveRecord;
class Index__GET extends ActiveRecord
{
public $id;
public $ch_name_url;
public $email;
public static function tableName()
{
return 'user';
}
public function fields()
{
return ['id', 'ch_name_url', 'email'];
}
public function rules()
{
return [
[['id', 'ch_name_url'], 'required'],
];
}
}
Just create a controller extending from \yii\rest\ActiveController, then validate will run automatically. Do something like this:
namespace app\controllers;
use yii\rest\ActiveController;
class IndexController extends ActiveController
{
public $modelClass = 'app\models\Index__GET';
}
$model->validate() is called by default when you call $model->save(), but if you need to validate a model in an action, do it like you did on your question example code.
Just remember that the actions from REST are used a bit different from normal call, where actionIndex usually is not needed.
For more information, follow the original docs: REST Quick Start

Fetch data in codeigniter construct function

I try to fetch data from database in codeigniter construct function
but getting undefined method error
controller-
class Welcome extends CI_Controller
function __construct()
{
parent::__construct();
$this->load->model('Login');
$this->Login->getadminnav();
}
}
model-
class Login extends CI_Model
{
public function adminnav()
{
$query="SELECT * from adminnav where status='1'";
$query->row_array();
return true;
}
}
You got to load the model you are using first:
$this->load->model('login_model');
$this->Login->getadminnav();
And remember to rename the model to Login_model
Try these pieces of code.
Controller:
class Welcome extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('Login');
$this->Login->adminnav();
}
}
Model:
class Login extends CI_Model{
public function adminnav(){
$this->load->database();
$sql = "SELECT * from adminnav where status='1'";
$result = $this->db->query($sql);
return $result;
}
}
I am seeing in your code that-
In controller you didn't start the second bracket after controller name.
You defined the function name as "adminnav" in model but called it as "getadminnav" in Controller
You didn't load the database in model (If you already loaded it in autoload.php then here is not needed)
Anyway please let me know whether your problem is solved or not.
you are calling getadminnav() but in your model the method name is adminnav.and change model name Login to Login_model.
try this:
class Welcome extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('Login_model');
$this->Login->adminnav();
}
}
// model
class Login_model extends CI_Model{
public function adminnav()
{
$query="SELECT * from adminnav where status='1'";
$query->row_array();
return true;
}
}

Laravel 5.6 controller not loading Model

I am using couchbase DB in my project. I have a controller and created a Model. My Controller is not recognizing the Model. The code looks like below:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Item;
class ItemController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$users = \DB::connection('couchbase')->table('metis-dev')->get();
}
}
Model Code is below:
namespace App;
//use Illuminate\Database\Eloquent\Model;
use Mpociot\Couchbase\Eloquent\Model as Eloquent;
class Item extends Eloquent
{
//protected $connection = 'couchbase';
protected $table = 'item';
}
protected $guarded = []; use that in your model.

Laravel seeding tables in second database

connection('mysql2') is my (working) second database connection.
When I migrate first, connection('mysql2') is working like expected, the table is created.
Schema::connection('mysql2')->create('brands', function(Blueprint $table)
{
//...
});
But when I try to seed tables in my second database:
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
use App\Brands;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
Model::unguard();
$this->call('BrandsTableSeeder');
$this->command->info("Brands table seeded.");
}
}
class BrandsTableSeeder extends Seeder
{
public function run()
{
DB::connection('mysql2')->table('brands')->delete();
Brands::connection('mysql2')->create(['brand' => 'test']);
}
}
I got:
[BadMethodCallException]
Call to undefined method Illuminate\Database\Query\Builder::connection()
Problem with your code is you have used Connection() method with Eloquent(not DB), Eloquent doesn't have connection() method.
You can use on() method with model(Eloquent) to specify connection
$user = User::on('mysql2')->create(['brand' => 'test']);
reference http://laravel.com/docs/4.2/eloquent#basic-usage
or
instead of writing on('mysql2') everywhere
you can write following code in model
protected $connection = 'mysql2';
and now seed is written as
class BrandsTableSeeder extends Seeder
{
public function run()
{
Brands::truncate();
Brands::create(['brand' => 'test']);
}
}