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'];
Related
Mates, im getting error sqlstate 42s22 columnactivity_post_commentsnot found 1054 unknown column in 'field list'
But this column name exists in my database.
I have checked my database and i have checked for spelling mistakes but im still getting this error
public function storecomments(Request $request, $id)
{
// Create Comment
$activity_post = new ActivityPost;
$activity_post->activity_post_comments = $request->input('activity_post_comments');
$activity_post->activity_post_id = $id;
$activity_post->user_id = auth()->user()->id;
$activity_post->save();
return redirect()->back()->with('success', 'Comment Added');
}
Not sure why im getting this error so i cant save this values in my database
Model:
class ActivityPostComment extends Model
{
use SoftDeletes;
// Table Name
protected $table = 'activity_post_comments';
// Primary Key
public $primaryKey = 'id';
// Timestamps
public $timestamps = true;
/**
* The attributes that should be mutated to dates.
*
* #var array
*/
protected $dates = ['deleted_at'];
public function post(){
return $this->belongsTo('App\ActivityPost');
}
}
As per above code that you have posted . your calling wrong model your model class name is
ActivityPostComment
while you are using
ActivityPost
public function edit($id){
$guests=Guest::with('programs', 'specialties')->find($id);
return view('editform',compact('guests'));
}
public function update(Request $request, $id)
{
$guestupdate = Guest::find($id);
$guestupdate->programs()->update($request->all());
$guestupdate->specialties()->update($request->all());
return redirect('/home/show'.$id)->with('alert', 'You have successfully updated');
}
class Guest extends Model
{
protected $primaryKey = 'guest_id';
protected $table = 'guests';
protected $fillable =
['guest_fname','guest_lname','profession','mobile','work_phone',
'current_job','previous_job','work_address','DOB','DD','program_name','specialty_name'];
public function programs()
{
return $this->belongsToMany(Program::class, 'guest_show', 'guest_id', 'program_id')
->withPivot('subject', 'show_date');
}
public function specialties()
{
return $this->belongsToMany(Specialization::class, 'guest_speciality', 'guest_id', 'speciality_id');
}
class Program extends Model
{
protected $table = 'programs';
protected $primaryKey = 'program_id';
protected $fillable = ['program_name','subject','show_date'];
public function guests()
{
return $this-
>belongsToMany(Guest::class,'guest_show','program_id','guest_id')
->withPivot('subject', 'show_date');
}
}
class Specialization extends Model
{
protected $table = 'specialties';
protected $primaryKey = 'specialty_id';
protected $fillable = ['specialty_name'];
public function guests()
{
return $this-
>belongsToMany(Guest::class,'guest_speciality','speciality_id','guest_id');
}
This is my code of controller and model to edit data(just show by guest_id) then I would to update request all field from many to many relationship.
The edit URL true but when I click button to update data appear this problem column not found:1054 Unknown column '_token' in 'field list' (SQL: update what's solve?
This is a known error and can be found here. I think your error comes from $request->all() which includes _token coming from csrf_field()
So try changing $request->all() to $request->except(['_token']); in your update function. Your new update function should look like below
public function update(Request $request, $id) {
$guestupdate = Guest::find($id);
$guestupdate->programs()->update($request->except(['_token']));
$guestupdate->specialties()->update($request->except(['_token']));
return redirect('/home/show'.$id)->with('alert', 'You have successfully updated');
}
For more study Laravel HTTP Requests
Another issue might be the syntax in ->withPivot, withPivot accepts array so change your code to below:
public function programs() {
return $this->belongsToMany(Program::class, 'guest_show', 'guest_id', 'program_id')
->withPivot(['subject', 'show_date']);
}
I'm trying to create a league table in Laravel but I'm running into some issues with guess what, relationships, again. They never seem to work for me in Laravel. It's like they hate me.
I have a modal for matches
<?php
namespace App\Database;
use Illuminate\Database\Eloquent\Model;
class Match extends Model
{
protected $primaryKey = 'id';
protected $table = 'matches';
public $timestamps = false;
protected $guarded = ['id'];
}
And a modal for teams, but with a matches() function
<?php
namespace App\Database;
use Illuminate\Database\Eloquent\Model;
class Team extends Model
{
protected $primaryKey = 'id';
protected $table = 'teams';
public $timestamps = false;
protected $guarded = ['id'];
public function matches() {
return $this->hasMany('App\Database\Match', 'team_one_id, team_two_id');
}
}
I think the issue comes with team_one_id, team_two_id as the teams primary key could be in either one of them columns for the other table. When calling count() on matches() it throws an error.
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'matches.team_one_id, team_two_id' in 'where clause' (SQL: select count(*) as aggregate from matches where matches.team_one_id, team_two_id = 1 and matches.team_one_id, team_two_id is not null)
can you try this syntax
return $this->hasMany('modelPath', 'foreign_key', 'local_key');
Does Match table have a column maned 'team_id'?
because it's the default naming convention in the laravel docs for mapping the tables.
if you do have the column and populate the data you can just remove the foreign & local keys from matches() relationship. you don't need it. Laravel will automatically map it for you.
if you do not have the 'team_id' on Matches table please add the column and add the respective team ids for matches.
<?php
namespace App\Database;
use Illuminate\Database\Eloquent\Model;
class Team extends Model
{
protected $primaryKey = 'id';
protected $table = 'teams';
public $timestamps = false;
protected $guarded = ['id'];
public function matches() {
return $this->hasMany('App\Database\Match');
}
}
This way you can implement it, Add these relationship and a method in Team Model
public function homeMatches() {
return $this->hasMany('App\Database\Match', 'team_one_id');
}
public function awayMatches() {
return $this->hasMany('App\Database\Match', 'team_two_id');
}
public function matches() {
return $this->homeMatches->merge($this->awayMatches);
}
Now Fetch the data
$team = Team::find(1);
$matches = $team->matches(); //now it will fetch all matches for both columns
If you want to fetch matches as attributes then you can add one method
in your Team model
public function getMatchesAttribute()
{
return $this->homeMatches->merge($this->awayMatches);
}
Now you can fetch the matches as $matches = $team->matches;
Here is the difference
$team->matches returns Illuminate\Database\Eloquent\Collection
And
$team->matches() returns Illuminate\Database\Eloquent\Relations\{Relation Name}
You can't use matches in Eager loading like Team::with('matches') because matches is not a relationship and that causing your Error. What you can do is add homeMatches and awayMatches in eager loading and then call $team->matches().
$teams = Team::with('homeMatches', 'awayMatches')->get();
$teams->each(function ($team) {
print_r($team);
print_r($team->matches());
});
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'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