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.
Related
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?
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
I'm trying to reference the Requests class in Laravel, I've tried so many fixes with the keyword "use" but each time I keep getting Reflection exception
that says app\path\specified doesn't exist. I'm confused.
Here is my code:`
<?php
namespace App\Http\Controllers;
//namespace App\Http\Request;
//use Illuminate\Http\Requests;
//use app\Http\Requests\ContactFormRequest;
use App\Message;
use App\Mail\SendMessage;
use Session;
//use App\Requests;
class AboutController extends Controller
{
public function create()
{
return view ('about.contact');
}
public function store(App\Requests\SendMessageRequest $request)
{
$message = $request->message;
Mail::to('myemail')
->send(new SendMessage($message, $request->email,$request->name));
THE REQUESTS CLASS
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class SendMessageRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return false;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
//
'name' => 'required',
'email' => 'required|email',
"message" => 'required',
];
}
}
The commented line(//) are what I've tried
SendMessageRequest is the name of my Request class.
Sorry, I canĀ“t comment your post. However can you also send the SendMessageRequest Class? Is that a subclass of the Request in Laravel?
I need to override the default Profile model. I have managed to add the fields i need but there is something i am missing since. On insert and update these fields are not getting update to the database.
I have created the necessary migrations so i have these fields in the database already
What am i missing> see below my app/models/Profile.php
<?php
namespace app\models;
/**
* Description Profile
*
* This form #overrides dektrium\user\models\Profile
*/
use dektrium\user\models\Profile as BaseProfile;
use yii\web\UploadedFile;
use Yii;
use dektrium\user\models\User;
class Profile extends BaseProfile {
/**
* public variables to be added to the model
*/
public $profile_pic;
public $expertise_id;
public $country_id;
public function rules() {
$rules = parent::rules();
$rules['profile_pic'] = ['profile_pic', 'file'];
$rules['expertise_id'] = ['expertise_id', 'integer'];
$rules['country_id'] = ['country_id', 'integer'];
return $rules;
}
/**
* #inheritdoc
*/
public function attributeLabels() {
$labels = parent::attributeLabels();
$labels['profile_pic'] = \Yii::t('user', 'Profile Picture');
$labels['bio'] = \Yii::t('user', 'Biography');
$labels['expertise_id'] = \Yii::t('user', 'Expertise');
$labels['country_id'] = \Yii::t('user', 'Country');
return $labels;
}
}
First thing, remove this lines:
public $profile_pic;
public $expertise_id;
public $country_id;
If you already added those fields in the table, you dont need to declare them. As you can see, none of the others properties are being declared either. This is already being done by extending the model from ActiveRecord and declaring the tableName
In my application, there are custom configs and I want to get them into the model.
I read about one way, but it can not perform:
namespace Core\Model;
use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\TableGateway\Feature\FeatureSet;
use Zend\Db\TableGateway\Feature\GlobalAdapterFeature;
use Zend\Db\Sql\Delete,
Zend\Db\Sql\Insert,
Zend\Db\Sql\Update,
Zend\Db\Sql\Select;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class BaseModel extends AbstractTableGateway implements ServiceLocatorAwareInterface
{
protected $serviceLocator;
public function setServiceLocator(ServiceLocatorInterface $serviceLocator) {
$this->serviceLocator = $serviceLocator;
}
public function getServiceLocator() {
return $this->serviceLocator;
}
public function __construct()
{
$this->featureSet = new FeatureSet();
$this->featureSet->addFeature(new GlobalAdapterFeature());
$this->initialize();
}
}
In the model I prescribe
$config = $this->getServiceLocator()->get('config');
or
$config = $this->getServiceLocator();
but the result = NULL
Who can tell what I'm doing wrong?
You have to create instances of your classes that extend BaseModel using the ServiceManager. If you use new, then you have to set the ServiceManager yourself.