Laravel Eloquent 'belongs to' relationship books and its category - php

I'm trying to print all books' name in 'books' table and its category associated. But it doesn't show any content nor error messages.
Model::Book.php
class Book extends Eloquent{
protected $table ='books';
public function bookCat()
{
return $this->belongsTo('BookCategory');
}
}
Model::BookCategory.php
class BookCategory extends Eloquent{
protected $table ='book_categories';
}
Controller::route.php
Route::get('/', function()
{
$books = Book::all();
return View::make('books')
->with('books', $books);
});
View::books.blade.php
#foreach ($books as $book)
<li>{{ $book->book_name }}</li>
- <small>{{ $book->bookCat }}</small>
#endforeach
Table::books
(int)id, (string)book_name, (int)category_id
Table::book_categories
(int)id, (string)category

Add the relationship to BookCategory.
class BookCategory extends Eloquent{
protected $table ='book_categories';
public function books() {
return $this->hasMany('Book');
}
}
Your local key doesn't match the model names, either, so you would need to specify that in the relation:
class Book extends Eloquent{
protected $table ='books';
public function bookCat()
{
return $this->belongsTo('BookCategory', 'category_id');
}
}
You may also want to eager-load the categories in your controller, as your query is for the books:
$books = Book::with('bookCat')->get();

Related

Laravel Invalid argument supplied for foreach

Model Doctor
class Doctor extends Model
{
public function addresses() {
return $this->belongsTo(Doctor::class);
}
}
Model Address
class Address extends Model
{
public function doctors() {
return $this->hasMany(Address::class);
}
}
DoctorsController
class DoctorsController extends Controller
{
public function index()
{
$doctors = Doctor::with('addresses')->get();
return view('doctors.index',compact('doctors'));
}
}
Blade
#foreach($doctors as $doctor)
{{ $doctor->name }}
#foreach($doctor->addresses as $address)
{{ $address->city }}
#endforeach
#endforeach
I have an error
Invalid argument supplied for foreach()
I tried to make a relation between Doctor and Address, but it doesn't work. If i try dd($doctor->addresses) i have null.
You reference the same class in your relations ("Doctor belongs to Doctor"), that probably cannot work.
Try:
class Doctor extends Model
{
public function addresses() {
return $this->hasMany(Address::class);
}
}
class Address extends Model
{
public function doctors() {
return $this->belongsTo(Doctor::class);
}
}
does it make sense that a doctor has many addresses and a address has many doctors? basing on your models you do have a many to many relationship between doctors and addresses?
Why not you can do it this way. a doctor has many addresses? so one to many relationship
then your models would be like this way.
Doctor model
class Doctor extends Model
{
public function addresses() {
return $this->hasMany('App\Address','DoctorId');// you need to indicate the foreign key if you didn't follow the laravel naming convention
}
}
address model
class Address extends Model
{
public function doctor() {
return $this->hasOne('App\Doctor','DoctorId');// you need to indicate the foriegn key if you didn't follow the Laravel naming convention
}
}
you controller
class DoctorsController extends Controller
{
public function index()
{
$doctors = Doctor::all();//or Doctor::where('something','=','value')->get();
return view('doctors.index',compact('doctors'));
}
}
your view
#foreach($doctors as $doctor)
{{ $doctor->name }}
#foreach($doctor->addresses as $address)
{{ $address->city }}
#endforeach
#endforeach

Laravel Many to Many relationship gets partial results

I have Course and Category Models with many to many relationship between them. So I created CategoryCourse Model to decompose relationship. Here are my three models:
class CategoryCourse extends Model
{
private $foreignkeys = [
'$category_id','$course_id'
];
public function categories()
{
return $this->belongsToMany('App\Category');
}
public function courses()
{
return $this->belongsToMany('App\Course');
}
}
class Category extends Model
{
protected $title = ['title'];
public function categorycourse()
{
return $this->hasMany('App\CategoryCourse');
}
}
class Course extends Model
{
protected $fillable = [
'title', 'desc'
];
public function categorycourse()
{
return $this->hasMany('App\CategoryCourse');
}
}
In my controller I have method as follows:
public function getCoursesByCategory(Request $request, $id)
{
$id = Hashids::decode($id);
$categories = Category::findOrFail($id[0]);
$courses = $categories->categorycourse()->get();
return view('courses',compact('courses'));
}
I have three tables in database, they are categories, courses and category_courses. My view is displaying results from category_courses but not results from courses. I am learning laravel. Can any one please help? I want to display all courses that belong to a category, in my view.
Here is my view code:
#foreach($courses as $course)
{{$course->title}}
#endforeach
You have to change your code to:
class Category extends Model
{
protected $title = ['title'];
public function courses()
{
return $this->belongsToMany('App\Course', 'category_course', 'category_id', 'course_id');
}
}
The relationship should be in Category only.
And then you call:
$courses = $categories->courses;

How do display a variable from a databse with through Relational Models?

I have a list of books and a list of authors. I made the Model relations, so that my Book Model says, that books->belongTo('Author') and my Author Model says, that authors->hasMany('Book').
So normally I could access the a variable through this:
$books = Book::all();
And then in the view:
#foreach($books as $book)
<div>{{$book->id}}</div>
<div>{{$book->title}}</div>
<div>{{$book->authors->firstname}}</div>
#endforeach
But this does not work. I get the error message: Trying to get property of non-object
So here are my files:
My Models:
Book.php
class Book extends \Eloquent {
protected $guarded = [];
public function religions()
{
return $this->belongsTo('Religion');
}
public function branches()
{
return $this->belongsTo('Branch');
}
public function authors()
{
return $this->belongsTo('Author');
}
public function quotes()
{
return $this->hasMany('Quote');
}
public function chapters()
{
return $this->hasMany('Chapter');
}
}
Author.php
class Author extends \Eloquent {
protected $guarded = [];
public function books()
{
return $this->hasMany('Book');
}
public function quotes()
{
return $this->hasMany('Quote');
}
public function branches()
{
return $this->belongsTo('Branch');
}
public function religions()
{
return $this->belongsTo('Religion');
}
}
Then come my controller:
ReligionBranchBookController
class ReligionBranchBookController extends \BaseController {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index($religionId, $branchId)
{
//
// $books = Book::where('religion_id', $religionId)->where('branch_id', $branchId)->get();
$books = Book::all();
$authors = Author::all();
// dd($books->toArray());
return View::make('books.index')
->with('religionId', $religionId)
->with('branchId', $branchId)
->with('books', $books)
->with('authors', $authors);
}
}
My Views:
index.blade.php
#extends('layout.main')
#section('content')
<h1>Books List!!</h1>
<table>
<tr>
<th>ID</th>
<th>Title</th>
<th>Author</th>
</tr>
#foreach($books as $book)
<tr>
<td>{{$book->id}}</td>
<td>{{$book->title}}</td>
<td>{{$book->authors->firstname}}</td>
</tr>
#endforeach
</table>
#stop
I know that it should normally work, I rebuild it with only books and authors, and it works fine there.
So, does anyone have an idea, where I am going wrong?
Thanks,
George
In your Book model change the authors method to author like this:
public function author()
{
return $this->belongsTo('Author');
}
Because, one book belongs to one author according to your relationship and when you call authors the belongsTo->getResults() get called and runs the wrong query so authors get null and you get the error. Things to remember:
For hasOne and belongsTo relationship use singular form of relationship method, i.e, author not authors.
hasOne or belongsTo returns a single model object.
For hasMany and belongsToMany relationship use plural form of relationship method, i.e, authors not author.
hasMany and belongsToMany returns a collection of model objects.

laravel where on relationship table

I am using laravel, I have a main table with 'projects' and a table with 'users', an intermediate table linking the two as a many-to-many relationship.
Now I want to display a list of projects, but if they are over 'projects.max_people' then they should be hidden from this list, how do I do this in laravel?
The problem is im 'get()'ing the users in the view, after I did Project::get()... how do I add this where condition?
You should create a new object called ProjectUser or so. In this object should contain a 'project_id' and 'user_id'.
Then, your Project model would look something like this:
<?php
class Project extends \Eloquent
{
protected $table = 'projects';
public function projectUsers(){
$this->hasMany('ProjectUser', 'project_id');
}
}
The User class:
<?php
class Project extends \Eloquent
{
protected $table = 'projects';
public function projectUsers(){
$this->hasMany('ProjectUser', 'user_id');
}
}
The ProjectUser class:
class ProjectUser extends \Eloquent
{
protected $table = 'project_users';
public function project(){
$this->belongsTo('Project'`enter code here`, 'project_id');
}
public function user(){
$this->belongsTo('User', 'user_id');
}
}
Now, you can get the users of a project like so:
$projects = Project::with('projectUsers.user')->get();
When looping through these objects you can access the user like this:
#foreach($projects as $project)
#foreach($project->projectUsers as $projectUser)
{{ $projectUser->user->id }}
#endforeach
#endforeach

Get value from view

I have two table User & Article the relationship between tables are
Model:
class Article extends Eloquent {
public static $table = 'article';
public function User()
{
return $this->has_one('user', 'id');
}
and
class User extends Eloquent {
public static $table = 'user';
public function Article()
{
return $this->belongs_to('article', 'id_user');
}
I want to get name value from User directly on Article view but don't works with error
Trying to get property of non-object
My Controller:
public function action_index()
{
$Article = Article::order_by('id')->paginate(10);
return View::make('article.index')->with('$articles', $Article);
}
My View:
#foreach ($articles->results as $Arti)
<tr>
<td>{{$Arti->id}}</td>
<td>{{$Arti->tag}}</td>
<td>{{$Arti->user->name }}</td> <------ ERROR
<td>{{$Arti->content}}</td>
<td>{{$Arti->date}}</td>
<td>
Have a look at the below, a few things are different to yours...
Article belongs_to User (not has_one)
User has_many Article (not belongs_to)
Your relationships should be named in lowercase, plural for has_many (i.e. articles or user)
Your relationship subjects should be class names (i.e. Article or User)
Foreign keys should be name relationship_id, i.e. user_id
Add ::with() to your query to eager load relationships
When you paginate you need to access ->results in your view
class Article extends Eloquent {
// 3: lowercase 'user'
public function user()
{
// 1: Article belongs to User
// 4: class name 'User'
// 5: Foreign key on article table is user_id
return $this->belongs_to('User');
}
}
// models/user.php
class User extends Eloquent {
// 3: lowercase plural 'articles'
public function articles()
{
// 2: User has many Articles
// 4: class name 'Article'
return $this->has_many('Article');
}
}
// controllers/articles.php
class Article_Controller extends Base_Controller {
public $restful = true;
public function get_index()
{
// 6: Eager load the user relationship, ::with('user')
$articles = Article::with('user')->order_by('id')->paginate(10);
return View::make('articles.index', compact('articles'));
}
}
// views/articles/index.blade.php
// 7: access $articles->results from the paginator
#foreach ($articles->results as $article)
<h1>{{ $article->title }}</h1>
<p>By {{ $article->user->name }}</p>
#endforeach
{{ $articles->links() }}

Categories