I'm trying to count number of schedules on a homecourt on current timestamp, now I have tried hasManyThrough in laravel between homecourt, userhomecourt, schedule but got this error
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'timeFrom' in 'where clause' (SQL: select count(*) as aggregate from `tblHomeCourts` where `homeCourtId` = 1 and `timeFrom` <= 1496207469 and `timeTo` > 1496207469)
I have these relationship in HomeCourt model
public function playerCount()
{
return $this->hasManyThrough(Schedule::class,UserHomeCourt::class,'homeCourtId','userHomeCourtId','homeCourtId');
}
where am I wrong here? please correct it out.
class HomeCourt extends Model
{
protected $table = 'tblHomeCourts';
protected $primaryKey = 'homeCourtId';
protected $appends = ['homeCourtProfilePicUrl'];
public static $snakeAttributes = false;
protected $fillable = [
'homeCourtName',
'cityId',
'homeCourtProfilePic',
'gameType',
'courtType',
'numberOfCourts',
'lights',
'membershipStatus',
'dayCost',
'address',
'lat',
'long',
'userId',
'homeCourtStatus',
];
protected $hidden = ['homeCourtProfilePic', 'created_at', 'updated_at', 'homeCourtStatus', 'userId', 'cityId'];
public function getHomeCourtProfilePicUrlAttribute()
{
return ($this->attributes['homeCourtProfilePic'] != "" || $this->attributes['homeCourtProfilePic'] != null) ? Constant::HOMECOURT_PROFILE_URL . $this->attributes['homeCourtProfilePic'] : null;
}
/*relation for there now*/
public function user()
{
return $this->belongsTo(User::class, 'homeCourtId', 'homeCourtId');
}
/*player count*/
public function playerCount()
{
return $this->hasManyThrough(Schedule::class,UserHomeCourt::class,'homeCourtId','userHomeCourtId','homeCourtId');
}
}
Related
Using Lumen 5.1, I'd like to know how to to CRUD on a translatable field, in my case it's name.
This is migration file
class CreateDomainHolidays extends Migration
{
protected $table = 'domain_holidays';
protected $app_table = true;
public function up()
{
Schema::create($this->getTable(), function (Blueprint $table) {
$table->increments('id');
$table->integer('domain_id')->unsigned()->nullable();
$table->foreign('domain_id')->references('id')
->on(self::getTableName('domains'))->onDelete('cascade');
$table->string('name')->nullable()->default('')->translatable = true;
$table->dateTime('start_date');
$table->dateTime('end_date');
$table->tinyInteger('half_day_start')->default(0);
$table->tinyInteger('half_day_end')->default(0);
$this->parenttable = $table;
});
$this->createTableWithTranslation($this->parenttable);
}
public function down()
{
$this->dropTranslationTable($this->getTable());
Schema::drop($this->getTable());
}
}
This is my model
class Holiday extends BaseModel
{
protected $table = 'domain_holidays';
protected $app_table = true;
public $timestamps = false;
protected $translation = true;
public function translations()
{
return $this->hasMany('App\Models\General\HolidayTranslations', 'parent_id');
}
}
class HolidayTranslations extends BaseTranslationModel
{
protected $table = 'domain_holidays_i18n';
protected $primaryKey = 'translation_id';
}
}
domain_holidays contains
id
domain_id
start_date
end_date
half_day_start
half_day_end
domain_holidays_i18n contains
translation_id
parent_id
lang
name
Something like this is not working
public static function setHoliday($domainId, $name, $startDate, $endDate, $halfDayStart, $halfDayEnd)
{
Holiday::unguard();
return Holiday::create([
'domain_id' => $domainId,
'name' => $name,
'start_date' => $startDate,
'end_date' => $endDate,
'half_day_start' => $halfDayStart,
'half_day_end' => $halfDayEnd
]);
}
Postman would return an error
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name' in 'field list'
I have defined to not use timestamps, but still laravel forces to use timestaps... Im using laravel 5.6.
When I visit page for example - http://mypage/api/videos/21/comments
I get an error - "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'created_at' in 'order clause' (SQL: select * from videos_comments where videos_comments.video_id = ▶"
app/Providers/VideoComment.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class VideoComment extends Model
{
protected $table = 'videos_comments';
public $timestamps = false;
protected $fillable = [
'text', 'userid', 'date'
];
public function videos() {
return $this->belongsTo('App\Video', 'id', 'video_id');
}
public function member() {
return $this->belongsTo('App\Member', 'userid', 'member_id');
}
}
app/Providers/Video.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Cviebrock\EloquentSluggable\Sluggable;
class Video extends Model
{
protected $table = 'videos';
public $timestamps = false;
use Sluggable;
public function sluggable() {
return [
'slug' => [
'source' => 'title'
]
];
}
public function comments() {
return $this->hasMany('App\VideoComment', 'video_id', 'id');
}
public function member() {
return $this->belongsTo('App\Member', 'userid', 'member_id');
}
}
VideoCommentController.php function
public function index(Video $video) {
return response()->json($video->comments()->with('member')->latest()->get());
}
If you use latest() in your query then you have to add created_at in your db table, read more.
The latest and oldest methods allow you to easily order results by date. By default, the result will be ordered by the created_at column.
You can easily use orderBy and first() instead of latest() when you haven't created_at column.
I am trying to insert information in the database. And i have the error
column not found: Unknown column 'updated_at' in 'field list'
I donot have that column in my database.
model
<?php
namespace App;
class quiz extends Basemodel {
protected $primaryKey = 'qid';
protected $table = 'quiz';
protected $fillable = array('question', 'opt1', 'opt2','opt3','opt4','answer','category', 'level');
}
controller
public function store(Request $request)
{
$input =$request->all();
$q=new quiz();
$q->question=$input['question'];
$q->answer=$input['answer'];
$q->opt1=$input['opt1'];
$q->opt2=$input['opt2'];
$q->opt3=$input['opt3'];
$q->save();
}
set public $timestamp = false; in your model
Yes, as Adam mentioned,
Set public $timestamp = false; in your model
Or else, add this line in the Model file.
protected $hidden = ['updated_at'];
i'm developing an API but when I create the relations (Many to Many) and want to show in the index function I'm getting an error QueryException in Connection.php line 669 The error says:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'CTL_Tags.id' in 'on clause' (SQL: select `CTL_Tags`.*, `CTL_Resource_has_Tags`.`idResource` as `pivot_idResource`, `CTL_Resource_has_Tags`.`idTag` as `pivot_idTag` from `CTL_Tags` inner join `CTL_Resource_has_Tags` on `CTL_Tags`.`id` = `CTL_Resource_has_Tags`.`idTag` where `CTL_Resource_has_Tags`.`idResource` is null)
I believe my error is in my model because it's looking for id in CTL_Tags table when my id name in that table is idTag.
This is my CTL_Resource model
<?php
namespace Knotion;
use Illuminate\Database\Eloquent\Model;
class CTL_Resource extends Model {
public $timestamps = false;
protected $table = "CTL_Resource";
protected $hidden = [
'coachVisibility', 'thumbnail', 'tags', 'relatedTo',
'studentVisibility', 'isHTML','studentIndex', 'coachIndex',
'isURL', 'source', 'path', 'status', 'updateTime', 'isfolder',
'parentResource', 'idModifierUser'
];
protected $fillable = ['idResourceType','productionKey', 'tags', 'idCreatorUser', 'idModifierUser', 'idCreationCountry', 'title', 'description', 'URL', 'fileName', 'extension', 'quicktag', 'minimumAge', 'maximumAge', 'productionKey'];
public function creatorUser() {
return $this->belongsTo('Knotion\OPR_User', 'idCreatorUser');
}
public function creationCountry() {
return $this->belongsTo('Knotion\CTL_Country', 'idCreationCountry');
}
public function resourceType() {
return $this->belongsTo('Knotion\CTL_ResourceType', 'idResourceType');
}
public function quickTags() {
return $this->belongsToMany('Knotion\CTL_QuickTag', 'CTL_Resource_has_QuickTags', 'idResource', 'idQuickTag');
}
public function tags() {
return $this->belongsToMany('Knotion\CTL_Tag','CTL_Resource_has_Tags', 'idResource', 'idTag');
}
public function relatedTo() {
return $this->belongsToMany('Knotion\CTL_RelatedTo', 'CTL_Resource_has_RelatedTo', 'idResource', 'idRelatedTo');
}
}
and I just will show you the code of one of the relations
<?php
namespace Knotion;
use Illuminate\Database\Eloquent\Model;
class CTL_QuickTag extends Model {
protected $table = "CTL_QuickTags";
protected $fillable = ['name'];
protected $hidden = ['status', 'createTime', 'updateTime'];
public function resources() {
return $this->belongsToMany('Knotion\CTL_Resource', 'CTL_Resource_has_QuickTags', 'idResource', 'idQuickTag');
}
}
and this is my Controller
<?php
namespace Knotion\Http\Controllers;
use Illuminate\Http\Request;
use Knotion\Http\Requests;
use Knotion\Http\Requests\ResourcesRequest;
use Knotion\CTL_Resource;
use Knotion\CTL_Tag;
use Knotion\CTL_QuickTag;
use Knotion\CTL_RelatedTo;
use Knotion\CTL_ResourceType;
class ResourcesController extends Controller {
public function index(Request $request) {
$resources = CTL_Resource::paginate(10);
$resources->each(function($resources) {
$resources->tags;
$resources->quickTags;
$resources->relatedTo;
});
return response()->json(
$resources
);
I'll be so grateful who anyone can help me. Thank you so much.
Try to define
$primaryKey
in your model , with the correct column name
https://laravel.com/docs/5.2/eloquent#defining-models
I have 3 Models:
City:
protected $fillable = [
'name', 'latitude', 'longitude', 'code', 'country_id', 'status', 'weather_code',
];
public function translations() {
return $this->hasMany('App\Models\CityTranslation');
}
public function country() {
return $this->hasMany('App\Models\Country');
}
CityTranslation
protected $fillable = [
'name', 'lang', 'city_id',
];
public function city() {
return $this->hasOne('App\Models\City', 'id', 'city_id');
}
and Country
protected $fillable = [
'code', 'latitude', 'longitude', 'currency_id', 'timezone', 'dam_date', 'status',
];
public function city() {
return $this->hasMany('App\Models\City');
}
My problem is when a go through my CityTranslations and display the city name for the selected language i want also to show information about the city and its country.
There is no problem to call $cityTranslation->city->longitude, but when i call $cityTranslation->city->country->code it gives me a MySQL error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'countries.city_id' in 'where clause' (SQL: select * from `countries` where `countries`.`city_id` = 4439 and `countries`.`city_id` is not null)
How can I make recursive relations?
Try
$cityTranslation = \App\CityTranslation::with('city.country')->get();
Thats if you want to get all city Translation and it related city and country. The you can loop through and get the country code.
if you want to pick only one city Translation and it related item you can do
$cityTranslation = \App\CityTranslation::with('city.country')->find($id);
Change this (in your city model)
public function country() {
return $this->hasMany('App\Models\Country');
}
to
public function country() {
return $this->belongsTo('App\Models\Country');
}
Because country can have many cities and every city must belong to a country