SQLSTATE[42S22]: Column not found: 1054 Laravel - php

I would like to get many "badges" to one "company"
I have 3 tables companies, badgy, badgy_company as pivot table.
What should I try/do? Can someone give me a hint or something?
company.blade.php
#foreach($listings->badgy as $type)
<span class="label label-default">{!! $type->title !!}</span>
#endforeach
Badgy.php
class Badgy extends Model{
protected $table = 'badgy';
public function badgy()
{
return $this->hasMany(Company::class);
}
If I remove protected $table = 'badgy'; I get error:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'MYDATABASE.badgies' doesn't exist
Company.php
public function badgy()
{
return $this->belongsToMany(Badgy::class);
}
In page controllers I try:
$listings = Company::find($id);
$listings = Company::query()->get();
If I need to provide any more info, please, just ask.

You are not following the Laravel naming conventions. As a result, the default values used for relations by the framework don't work. You will have to set them manually as follows:
class Badgy
{
protected $table = 'badgy'; // Laravel default would be 'badgies'
public function companies()
{
return $this->belongsToMany(Company::class, 'badgy_company', 'category_id', 'company_id');
}
}
class Company
{
protected $table = 'companies'; // optional as the default is the same
public function badgies()
{
return $this->belongsToMany(Badgy::class, 'badgy_company', 'company_id', 'category_id');
}
}
Please also have a look at another answer of mine where I explain some important pieces regarding relationships and naming conventions. Because in a perfect scenario you would have the following tables and columns:
companies:
- id
- name
badgies:
- id
- title
badgy_company:
- id
- badgy_id
- company_id
Which would allow your models to look like this:
class Badgy
{
public function companies()
{
return $this->belongsToMany(Company::class);
}
}
class Company
{
public function badgies()
{
return $this->belongsToMany(Badgy::class);
}
}

Related

Laravel hasMany and belongsToMany relationship

I have struggled with my Laravel relationships.
So I have 3 tables:
Users
Countries
countries_user
As you can see relation is countries_user. Now every time gives error like:
Illuminate\Database\QueryException: SQLSTATE[42S22]: Column not found:
1054 Unknown column 'countries.user_id' in 'where clause' (SQL: select
from countries where countries.user_id = 1 and countries.user_id is not null) in file
C:\xampp\htdocs\gManager\vendor\laravel\framework\src\Illuminate\Database\Connection.php
on line 671
I understand the problem is that it's looking in countries and not in countries_user. How to define where I want to search the relation?
Here is my User model
public function countries()
{
return $this->hasMany('App\Models\Countries');
}
And my Countries Model
public function users()
{
return $this->belongsToMany(User::class);
}
Try specifying the table name
public function users()
{
return $this->belongsToMany(User::class, 'countries_user');
}
And the inverse relation should also be belongsToMany
public function countries()
{
return $this->belongsToMany(Countries::class, 'countries_user');
}
And also specify the table property on Countries model
class Countries extends Model
{
protected $table = 'countries';
//...
}
The countries relationship should be belongsToMany too.
Instead of
public function countries()
{
return $this->hasMany('App\Models\Countries');
}
put
public function countries()
{
return $this->belongsToMany('App\Models\Countries');
}

Rretrieving data from foreign key, One to One Relationship. Laravel

I am trying to retrieve value from foreign key table with one to one relation. I have defined two models:
1. Blog
class Blog extends Model
{
//
protected $table = 'blogs';
public function blog_category()
{
return $this->hasOne('App\Blog_Category');
}
}
2. Blog Category
class Blog_Category extends Model
{
//
protected $table=('blogs_categories');
public function blog()
{
return $this->belongsTo('App\Blog');
}
}
I have got blogs_categoryid in blogs table that has been referenced to id from blogs_categories table.
I have tried following:
{{$blog->blogs_categoryid->category}}
But it is showing "trying to get property of non-object". What is going wrong here? Can anyone help me?
Blog model content should be:
class Blog extends Model
{
protected $table = 'blogs';
public function blog_category()
{
return $this->hasOne('App\Blog_Category', 'blogs_categoryid');
}
}
Then use with when you want it to query relation table:
$blog = Blog::with('blog_category')->get();
$categoryObject = $blog->blog_category;
$categoryId = $categoryObject->id;

My Laravel relationship problems

I'm trying to do browser game like Tribal Wars in Laravel.
I want to get building level by using $wioska->buildings->Tartak->level, but something not working:
This is my Building model:
class Building extends Model
{
protected $table = 'budynki';
public function Tartak(){
return $this->hasOne('App\Tartak');
}
}
Wioska (village) model:
class Wioska extends Model
{
protected $fillable = ['name', 'user_id'];
protected $table = 'wioski';
public function user(){
return $this->belongsTo('App\User');
}
public function buildings(){
return $this->hasOne('App\Building');
}
}
And this is my Tartak model:
class Tartak extends Model
{
protected $table = 'budynki';
public function level(){
$u = Auth::user();
$id = $u->wioska->id;
return DB::table('budynki')->where('wioska_id', $id)->first();
}
}
Migration "budynki":
public function up()
{
if(!Schema::hasTable('budynki')) {
Schema::create('budynki', function (Blueprint $table) {
$table->integer('town_hall')->default(1);
$table->integer('iron')->default(0);
$table->integer('wood')->default(0);
$table->integer('stone')->default(0);
$table->integer('bread')->default(0);
$table->integer('wioska_id');
$table->foreign('wioska_id')->references('id')->on('wioski');
});
}
}
1) It's always good to check for a null entity before trying to call its methods. Example, if $wioska->buildings is null or wioska has no buildings, or buildings have no Tartak, then the rest of the line will throw errors.
2) level() is a method and since its not an authentic Laravel relationship, you will need to use it as a method, example - $wioska->buildings->Tartak->level()
level is not property as per your model, so you have to try as below
$wioska->buildings->Tartak->level()
Now I've got
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'budynki.building_id' in 'where clause' (SQL: select * from budynki where budynki.building_id is null and budynki.building_id is not null limit 1) error
I just want to get tartak level from budynki table: https://i.imgur.com/zoTx5tE.png .

Laravel Defining Relationships

I have one table named Content which is a master table like
Content : id content_name created_at updated_at
and another table Course like
Course table have many content_id
Course : id content_id course_name created_at updated_at
I have created relation like this.
Content Model
class Content extends Eloquent {
protected $table = 'contents';
protected $guarded = array('id');
public function course()
{
return $this->belongsTo('Course');
}
}
Course Model
class Course extends Eloquent {
protected $table = 'courses';
protected $guarded = array('id');
public function content()
{
return $this->hasMany('Content');
}
}
When i am fething the data like this
$courses=Course::find(1)->content;
It throws error like
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'contents.course_id' in 'where clause' (SQL: select * from contents where contents.course_id = 1)
I am unable to rectify the problem in relations as I am new to laravel.
Close, but you have your relationships backwards. The table that has the foreign key is the one that belongsTo the other one. In this case, your course table has the foreign key content_id, therefore Course belongs to Content, and Content has one or many Courses.
class Content extends Eloquent {
public function course() {
return $this->hasMany('Course');
}
}
class Course extends Eloquent {
public function content() {
return $this->belongsTo('Content');
}
}
in your migrations(create_courses_table) , make sure to set the foreign key like that ,
$table->integer('content_id)->unsigned()->index();
$table->foreign('content_id')
->references('id')
->on('contents')
->onDelete('cascade');
I don't really understand your table design, maybe it should be something like this.
Taking your statement: "Course table have many content_id". I perceive that you are saying that 1 course can have multiple content, is that right? If yes, you might want to change your table design to something like below
Course
======
id
course_name
created_at
updated_at
Content
=======
id
course_id (set this as FK)
content_name
created_at
updated_at
migration code for content
public function up()
{
Schema::create('content', function(Blueprint $table)
{
$table->engine = 'InnoDB';
$table->increments('id');
$table->integer('course_id')->unsigned();
$table->string('content_name');
});
Schema::table('content',function($table)
{
$table->foreign('course_id')->references('id')->on('course')->onDelete('cascade');
});
}
Then in your model
class Course extends Eloquent
{
protected $table = 'course';
public function content()
{
return $this->hasMany('content', 'course_id', 'id');
}
}
class Content extends Eloquent
{
protected $table = 'content';
public function course()
{
return $this->belongsTo('course', 'course_id', 'id');
}
}
Then to access your data via eager loading
$course = Course::with('content')->get();
OR
$content = Content::with('course')->get();
This is about determining associations. Your associations should be:
Content
Content has many courses
class Content extends Eloquent {
protected $table = 'contents';
protected $guarded = array('id');
public function courses()
{
return $this->hasMany('Course');
}
}
Course
The course belongs to content.
class Course extends Eloquent {
protected $table = 'courses';
protected $guarded = array('id');
public function content()
{
return $this->belongsTo('Content');
}
}
So you can do query association.
For finding content -> courses:
$courses = Content::find(1)->courses;
For finding course -> content:
$content = Course::find(1)->content;

Laravel Eager Loading One to Many Relationship

Good day, I am having this problem where I am trying to retrieve the record from 2 tables
class Leads extends Eloquent
{
public function leademail()
{
return $this->hasMany('LeadDetailEmails');
}
}
class LeadDetailEmails extends Eloquent
{
public function lead()
{
return $this->belongsTo('Leads');
}
}
class Category extends BaseController
{
public function fetchRecord()
{
$result = Leads::with('leademail')->get();
}
}
leadDetailEmails has foreign key set in the migration file
$table->foreign('lead_id')->references('id')->on('leads')->onDelete('cascade');
I am getting this error:
"message":"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'lead_detail_emails.leads_model_id' in 'where clause' (SQL: select * from `lead_detail_emails` where `lead_detail_emails`.`leads_model_id` in (1, 2, 3))"
What can I do to fix the error? also, if there will be a third table(LeadDetailContact), how can I eager loading the table?
thanks.
You should try specifying your table name for LeadDetailEmails in the following way:
class LeadDetailEmails extends Eloquent
{
protected $table = 'leadDetailEmails';
public function lead()
{
return $this->belongsTo('Leads');
}
}
And if you have more relations you want to get with eager loading you can do it this way:
$result = Leads::with('leademail', 'leadcontact')->get();

Categories