Column not found: 1054 Unknown column countries - laravel

Error like that...How can i solve that?
Country database
This is country model
This is Challenge database
This is Challenge model
#foreach ($challenges as $challenge)
<tr>
<td>{{$challenge->c_sport_id}}</td>
<td>{{$challenge->c_gender}}</td>
<td>{{$challenge->c_location}}
<td>{{$challenge->countries->country_name}}</td>
</tr>
#endforeach

change your relation
<?php
class Challenge extends Model
{
public function country()
{
return $this->belongsTo(\App\Country::class, 'c_country_id', 'id');
}
}

The issue in your Challenge Model. You have country id in your challenge model. So you should use relationship belongsTo instead of hasOne. So change your Challenge Model relationship like this:-
<?php
class Challenge extends Model
{
public function country()
{
return $this->belongsTo(\App\Country::class, 'c_country_id', 'id');
}
}
This is the correct one.

Related

Laravel Eloquent Relationship has the same foreign key on the same table

I'm a beginner in Laravel and currently trying to create a ticketing system. My tickets table has two columns which both reference id from the users table namely user_id and handled_by. I am trying to eager load the full_name of handled_by but it is throwing an error "Trying to get property 'full_name' of non-object". I would like to know how to properly create an eloquent relationship between handled_by and tickets so I could display the handled_by's full_name. Below are snippets of my code.
User Model:
public function tickets(){
return $this->hasMany('\App\Ticket');
}
public function handledBy(){
return $this->hasMany('\App\User', 'handled_by', 'id');
}
Ticket Model:
public function user(){
return $this->belongsTo('\App\User');
}
public function handledBy(){
return $this->belongsTo('\App\User', 'handled_by', 'id');
}
View:
#foreach($tickets as $ticket)
<td class="align-middle text-center">{{ $ticket->handled_by->full_name}}</td>
#endforeach
Controller:
$tickets = Ticket::all()->where('status_id', '1');
return view('admin.open_tickets', compact('tickets'));
In your User model, change \App\User to \App\Ticket, and change the method name from handlerBy to handlers:
public function handlers(){
return $this->hasMany('\App\Ticket', 'handled_by', 'id');
}
And call it by method's name instead of underline case name:
{{ $ticket->handledBy->full_name}}

creating query from other tables that has the ID of the primary table in laravel

Apology for the title. I'm really not sure how to name the title correctly base from my situation. I'm new in coding that's why I am not familiar with proper terminologies.
below are the tables I am working on right now.
I am displaying the details from loan_application table. I can able to include the loan_durations and users in my #foreach but I realized I need to include also the SUM of the AMOUNT from loan_interests table and the SUM of AMOUNT from LOAN PENALTIES which gives me an headache because I can't pull them out.
LOAN APPLICATION MODEL
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class LoanApplication extends Model
{
public function user()
{
return $this->hasOne('App\User', 'id','user_id');
}
public function loanDuration()
{
return $this->hasOne('App\LoanDuration', 'id','loan_duration_id');
}
public function interest()
{
return $this->belongsTo('App\LoanInterest','loan_id', 'id');
}
public function penalty()
{
return $this->belongsTo('App\LoanPenalty','loan_id', 'id');
}
}
LOAN INTEREST MODEL
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class LoanInterest extends Model
{
public function loanInterest()
{
return $this->belongsTo('App\LoanApplication','loan_id', 'id');
}
}
LOAN PENALTY MODEL
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class LoanPenalty extends Model
{
public function loanPenalties()
{
return $this->belongsTo('App\LoanApplication','loan_id', 'id');
}
}
for my controller
public function collectorMembers($id)
{
$collectormember = CollectorMember::where('collector_id',$id)->get();
return view('dashboard.collector-members.collector-borrowers-list', compact('collectormember'));
}
This gives this result
the CollectorMember gets the info on this table
Can you help me please? thanks a lot in advance!
There is a way to accomplish what you're after, but first I think you need to revisit your schema and relationships.
Foreign key columns should be of the same type as the referenced column. For example loan_id on the loan_interests should be int(11) just like the id column on the loan_applications table
I think you may be confusing relationship types. For example a User hasMany LoanApplications and a LoanApplication belongsTo a User. A LoanApplication hasMany LoanPenaltys and hasMany LoanInterests. A LoanPenalty and a LoanInterest both belongsTo a LoanApplication
The user_id column on the loan_penalities table is redundant because a LoanPenalty belongsTo a LoanApplication and a LoanApplication belongsTo a User
I'd recommend storing currency amounts in cents and using unsigned integers as the column type (e.g. for interest_amount)
Consider the following schema (some columns not shown):
Then consider the following models with relationships:
class User extends Model {
public function loanApplications()
{
return $this->hasMany(LoanApplication::class);
}
public function collectors()
{
return $this->belongsToMany(Collector::class);
}
}
class Collector extends Model {
public function users()
{
return $this->belongsToMany(User::class);
}
}
class LoanApplication extends Model {
public function user()
{
return $this->belongsTo(User::class);
}
public function loanDuration()
{
return $this->belongsTo(LoanDuration::class);
}
public function loanInterests()
{
return $this->hasMany(LoanInterest::class, 'loan_id');
}
public function loanPenalties()
{
return $this->hasMany(LoanPenalty::class, 'loan_id');
}
}
class LoanDuration extends Model {
public function loanApplications()
{
return $this->hasMany(LoanApplication::class);
}
}
class LoanInterest extends Model {
public function loanApplication() {
return $this->belongsTo(LoanApplication::class, 'loan_id');
}
}
class LoanPenalty extends Model {
public function loanApplication()
{
return $this->belongsTo(LoanApplication::class, 'loan_id');
}
}
Then to list all loan applications in a Resource Controller:
class LoanApplicationController extends Controller {
public function index()
{
$loan_applications = LoanApplication
::with(['user', 'loanInterests', 'loanPenalties'])
->get();
$loan_applications = $loan_applications->map(function ($loan_application) {
$loan_application->loan_penalities_sum = $loan_application->loanPenalties->sum('penalty_amount_cents');
$loan_application->loan_interests_sum = $loan_application->loanInterests->sum('interest_amount_cents');
return $loan_application;
});
return view('dashboard.loan-applications.index', compact('loan_applications'));
}
}
And in your dashboard.loan-applications.index blade template:
<table>
<tr>
<th>Username</td>
<th>Total Interest</td>
<th>Total Penalty</td>
</tr>
#foreach ($loan_applications as $loan_application)
<tr>
<td>{{$loan_application->user->username}}</td>
<td>{{$loan_application->loan_interests_sum}}</td>
<td>{{$loan_application->loan_penalties_sum}}</td>
</tr>
#endforeach
</table>
Note the above example does not include pagination; all resources are loaded at once.
The above example also assumes there should be a many-to-many relationship between collectors and users, but I would imagine a Collector should be related to the loan_applications table, not to a User.

Trying to get property 'name' of non-object?

Hi Guys i am new to laravel and i am trying to retrieve category names for each formation and i am getting this error
Trying to get property 'name' of non-object (0)
The "name" i am trying to retrieve is the name of category that my formation belongs to.
This is My Category Model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
function formations()
{
return $this->hasMany('App\Formation');
}
protected $fillable =['name','description'];
}
And This is My Formation Model
class Formation extends Model
{
function category()
{
return $this->belongsTo('App\Category',"category_id");
}
protected $fillable =['name','price','durations','category_id'];
}
And here is my view page where i am trying to retrieve the category name
#foreach($formations as $formation)
<td>{{$formation->name}}</td>
<td>{{$formation->price}}</td>
<td>{{$formation->category->name}}</td>
td>{{$formation->durations}}</td>
#endforeach
This is My Controller
public function index()
{
//$formations = Formation::all();
$formations = Formation::with('category')->paginate('4');
$categories = Category::all();
return view('formation.index',compact('categories','formations'));
}
Any Suggestion about where i might be wrong ? And thanks in Advance !
This error usually comes when you have removed one or more row from database, but trying to access that row with an id. Make sure that you have all the category in the category table with the id which is mentioned in the formation table category_id field.
In your view
#foreach($formations as $formation)
<td>{{$formation->name}}</td>
<td>{{$formation->price}}</td>
<td> {{ $formation->categories()->pluck('name')->implode(' ') }}</td>
<td>{{$formation->durations}}</td>
#endforeach
Try changing the relationship
class Formation extends Model
{
function category()
{
return $this->belongsTo('App\Category',"id","category_id"); //if your categories id is id
}
protected $fillable =['name','price','durations','category_id'];
}
In your view
#foreach($formations as $formation)
<td>{{$formation->name}}</td>
<td>{{$formation->price}}</td>
<td>{{$formation->category->name}}</td>
<td>{{$formation->durations}}</td>
#endforeach
The Problem was that i have deleted a row from my category table in database that's why the name wasn't recognized.

Laravel One To Many Relationship

I have an Author model and Blog model.
I also set up a table with the author_id and blog_id. The table is called "author_blog".
Below is how I am defining my relationships:
Author Model:
public function blogs()
{
return $this->hasMany('App\Blog', 'author_id');
}
Blog Model:
public function author()
{
return $this->belongsTo('App\Author', 'blog_id');
}
In my view I am trying to do $blog->author->first_name. The first name is a column on the Authors table.
I keep getting
trying to get property on non-object.
Any ideas?
UPDATE:
I have removed the intermediate table and instead put an "author_id" on my blogs table and shortened it as such.
Author Model:
public function blogs()
{
return $this->hasMany('App\Blog');
}
Blog Model:
public function author()
{
return $this->belongsTo('App\Author');
}
In my BlogController, when I define the view, I grab a collection of blogs via $blogs = Blog::all();
In my view I just loop through the blogs to show each individually like...
#foreach ($blogs as $blog)
<div>{{ $blog->author->first_name }}</div>
#endforeach
For one to many relationship you don't need another table to handle your foreign key.
Just add author_id foreign key with your blogs table. then define your relations
like below,
Author Model:
public function blogs()
{
return $this->hasMany('App\Blog', 'author_id');
}
Blog Model:
public function author()
{
return $this->belongsTo('App\Author', 'author_id');
}
view:
before that make sure your query return a valid collection.
#foreach ($blogs as $blog)
<div>#if( $blog->author) {{ $blog->author->first_name }} #endif </div>
#endforeach
For further more details : https://laravel.com/docs/5.8/eloquent-relationships#one-to-many
with belongsToMany relation you can define your middle table(author_blog)
In Author model:
public function blogs()
{
return $this->belongsToMany(Blog::class,'author_blog','author_id','blog_id');
}
author_blog => middle table
author_id => foreignPivotKey
blog_id => relatedPivotKey

Laravel eloquent one to many relationship data not showing

i have two tables,
Student table
which have column (id, family_id,name,class,section)
Family table
which have column (family_id, mobile_no, profession)
--
i have created two models.
student model:
class student extends Model
{
}
and
family model
class family extends Model
{
public function student()
{
return $this->hasMany('App\student');
}
}
--
i am able to show all data from student table,
my controller is:
public function index()
{
$finddata = student::orderBy('id', 'asc')->get();
return view('students.index')->with('finddata', $finddata); }
--
what i tried
in family model:
return $this->hasMany('App\students');
what i want;
i want to connect family model with student model..
index page will have all students name only. which i have already done
when i click on student name it should show, all information about particular student, and his family information. right now it's only showing information from student table.
In the student model:
class student extends Model
{
public function family(){
return $this->belongsTo(Family::class,'family_id');
}
}
Then you can access family information in Blade by:
{{$student->family->mobile_no}}
{{$student->family->profession}}
You should better use artisan commands to generate models and controllers, so that you get automatic templates that follow naming conventions, such as uppercase Class names.
You should try this
class student extends Model
{
public function familyId(){
return $this->belongsTo(Family::class,'family_id');
}
}
when you click on student name it will redirect to student/{id}
route
Route::get('student/{id}', 'StudentController#show');
student controller
public function show(Request $request, $id)
{
$student = student::find($id);
return view('student.show')->with('student', $student);
}
student.show.blade file
Mobile no: {{ $student->familyId->mobile_no }}
Profession: {{ $student->familyId->profession }}

Resources