My model Dispatch has a field invoice_id.
It is a foreign key so i can get the invoice details using the below code:
protected $fillable = [
'id',
'truck_no',
'driver_name',
'driver_phone',
'gps_details',
'invoice_id',
];
public function invoice(){
return $this->belongsTo('App\Invoice')->select('id','invoice_no','permit_id');
}
Now I want to get the value permit_id from invoice() so i can use it to get the details of the Permit.
permit_id = id of Permit model
So I use the below code to get the permit data.
public function permit(){
return $this->hasOne('App\Permit','id',$this->invoice()->permit_id);
}
Update:
My Invoice Model has :
class Invoice extends Model
{
protected $fillable = [
'id',
'invoice_no',
'invoice_date',
'permit_id',
];
public function permit(){
return $this->hasOne('App\Permit', 'id', 'permit_id');
}
}
My Permit Model has:
class Permit extends Model
{
protected $fillable = [
'permit_type',
'permit_no',
'application_no',
'supply_unit',
'supply_unit_id' ,
];
public function supplyunit(){
return $this->hasOne('App\SupplyUnit','id','supply_unit_id');
}
}
And as per suggestions i have added below code in my Dispatch Model:
class Dispatch extends Model
{
protected $fillable = [
'id',
'truck_no',
'driver_name',
'driver_phone',
'gps_details',
'invoice_id',
];
public function invoice(){
return $this->hasOne('App\Invoice','id','invoice_id');
}
public function permit(){
return $this->hasOne('App\Permit','id','permit_id');
}
}
But it doesn't work. What should i do to achieve the above? Is there any other solutions please suggest.
Assuming each invoice has one permit, your relationship definition should look like this:
public function permit(){
return $this->hasOne('App\Permit', 'id', 'permit_id');
}
Edit: If invoice belongs to permit, which is the inverse, your relationship would look like this instead:
public function permit(){
return $this->belongsTo('App\Permit', 'permit_id');
}
Edit: Based on your updated question, I think you got the relationship definitions a bit wrong. The following should work:
Since you have an invoice_id column in App\Dispatch, it means that each App\Dispatch belongs to an invoice.
In App\Dispatch, your relationship definition should be as follows:
public function invoice() {
return $this->belongsTo('App\Invoice');
}
// permit does not belong to `App\Dispatch` as a direct relationship
// it should be removed
In App\Invoice, your relationship definition should be as follows:
public function dispatch() {
return $this->hasOne('App\Dispatch');
}
public function permit() {
return $this->belongsTo('App\Permit');
}
In App\Permit, your relationship definition should be as follows:
public function invoice() {
return $this->hasOne('App\Invoice');
}
To then retrieve the permit id from an Invoice model, you would do
$invoice->permit->id;
Change this line
return $this->belongsTo('App\Invoice')->select('id','invoice_no','permit_id');
To
return $this->belongsTo('App\Invoice');
And add the following code on Invoice
public function permit(){
return $this->belongsTo('App\Permit');
}
And you can access as
Dispatch::find($id)->invoice->permit->id;
Or if you want all the information
Dispatch::find($id)->invoice->permit;
Related
I have a table like this:
Basically this table is named favourite_products and contains product ids that are added as favourite for users.
Now I wanted to get a collection of most added product from this table.
So in this case, a product with an id of 10 would be on top of the collection.
But I don't know how to get this collection ordering by from most repeated product id (prd_id)...
Here is the Model:
class FavouriteProduct extends Model
{
protected $table = 'favourite_products';
protected $fillable = ['usr_id','prd_id'];
public function user()
{
return $this->belongsTo(User::class, 'usr_id');
}
public function product()
{
return $this->belongsTo(Product::class, 'prd_id');
}
}
UPDATE #1:
Product.php Model:
public function favouritees()
{
return $this->belongsToMany(User::class, 'favourite_products', 'prd_id', 'usr_id');
}
I think the following code solve your problem:
$most_liked_products = DB::table('favourite_products')
->select(DB::raw('count(prd_id) as total'), id)
->groupBy('total')
->orderByDesc('total')
->get();
Please try it and give your feedback
try use this
public function example()
{
$data=FavouriteProduct::orderBy('prd_id', 'ASC')->get();
dd($data);
}
Hi I simply want to get permissions of the role, I am trying following
$r = Role::find(1);
dd($r->permissions);
The above script does not return any permission however you can see there is data in the below tables. I also tried following but no luck
$role = Role::with('permissions')->where('id', 1)->first();
I have data in the table as you can see
Table:tes_permissions
Table: tes_roles
Table: tes_permission_role
And following are Models
class Permission extends Model
{
protected $table = 'tes_permissions'
public function roles()
{
return $this->belongsToMany('App\Role');
}
}
And
class Role extends Model
{
protected $table = 'tes_roles';
public function permissions() {
return $this->belongsToMany('App\Permission', 'tes_permission_role', 'permission_id', 'role_id');
}
}
Can someone kindly guide me what can be the issue, I would appreciate.
You mixed the order of properties in the belongsToMany(). The third argument is specifying the ID for the model defining the relationship. So change to the following:
public function permissions() {
return $this->belongsToMany('App\Permission', 'tes_permission_role', 'role_id', 'permission_id');
}
And on the Permission model, also define it to be sure.
public function roles()
{
return $this->belongsToMany('App\Role', 'tes_permission_role', 'permission_id', 'role_id');
}
I have 3 tables with this order:
School it has -> id
SchoolSemester it has -> school_id
SemesterClass it has -> semester_id
Now I am trying to make relation between School and SemesterClass in order to get list of classes of each school as well as name of schools in each class.
based on documentation i have this relationships:
school model
class School extends Model
{
public function semesters() {
return $this->hasMany(SchoolSemester::class);
}
public function classes() {
return $this->hasManyThrough(SemesterClass::class, SchoolSemester::class);
}
}
SchoolSemester model
class SchoolSemester extends Model
{
public function school() {
return $this->belongsTo(School::class);
}
public function classes() {
return $this->hasMany(SemesterClass::class, 'semester_id', 'id');
}
}
SemesterClass model
class SemesterClass extends Model
{
public function school() {
return $this->hasOneThrough(School::class, SchoolSemester::class, 'school_id', 'id');
}
public function semester() {
return $this->belongsTo(SchoolSemester::class, 'semester_id', 'id');
}
}
Controller
public function show($id)
{
$class = SemesterClass::with(['school', 'teacher', 'teacher.user', 'students'])->findOrFail($id);
dd($class);
//return view('admin.Classes.show', compact('class'));
}
Results
Any idea?
Your intermediate table is school_semester, laravel will find the foreign_key school_semester_id by default, however, your foreign_key is semester_id, so you need to specify the foreign_key in hasManyThrough:
public function classes() {
return $this->hasManyThrough(
SemesterClass::class,
SchoolSemester::class
'school_id', // Foreign key on school_semesters table...
'semester_id', // Foreign key on classes table...
'id', // Local key on schools table...
'id' // Local key on school_semesters table...
);
}
And change your hasOneThrough code like this:
public function school() {
return $this->hasOneThrough(
School::class,
SchoolSemester::class,
'id',
'id',
'semester_id',
'school_id' );
}
Another Solution:
Because the reverse is just the concatenation of two BelongsTo relationships, so you can just put the belongsTo in SchoolSemester and School Model.
And get the relationship like this:
SemesterClass::with(['semester.school'])
Or you can define a mutator in SemesterClass Model:
protected $appends = ['school'];
public function getSchoolAttribute() {
return $this->semester->school;
}
This question was already asked here but it received no answer. Now I face the same problem but in laravel 5.4. I have a model Book, a model ReadingSession and a model Comment. A book has many reading sessions and has many comments but the reading session can also have comments. So I have my relations defined like this:
Book.php
protected $with = [
'author',
'readingSessions',
'userRating',
'ratings',
'comments'
];
public function users()
{
return $this->belongsToMany(User::class, 'user_book');
}
public function author()
{
return $this->belongsTo(Author::class);
}
public function allReadingSessions()
{
return $this->hasMany(ReadingSession::class);
}
public function readingSessions()
{
return $this->hasMany(ReadingSession::class)
->where('user_id', Auth::user()->id);
}
public function ratings()
{
return $this->hasMany(Rating::class);
}
public function userRating()
{
return $this->hasMany(Rating::class)
->where('user_id', Auth::user()->id);
}
public function comments()
{
return $this->morphMany('App\Models\Comment', 'commentable');
}
ReadingSession.php
protected $with = ['comments'];
public function user()
{
return $this->belongsTo(User::class);
}
public function book()
{
return $this->belongsTo(Book::class);
}
public function comments()
{
return $this->morphMany('App\Models\Comment', 'commentable');
}
Comment.php
public function commentable()
{
return $this->morphTo();
}
These seems to create an infinite loop. Can anyone hint me on what I'm doing wrong?
The main reason you might have an infinite loop there is if you are trying to load automatically a relationship that in turn tries to do the same with the previous model.
Putting it into an example:
Book.php
protected $with = [
'author',
];
public function author()
{
return $this->belongsTo(Author::class);
}
Author.php
protected $with = [
'books',
];
public function books()
{
return $this->hasMany(Book::class);
}
In this case, every time you fetch an author it will fetch automatically his books that in turn will try to fetch the author and on and on...
One other thing that might happen and it's harder to realize is when using the $appends property on some accessors. If you are trying automatically had a variable into a model through the $appends and if that accessor fetches a relation or uses a relation in some way you might get an infinite loop again.
Example:
Author.php
protected $appends = [
'AllBooks',
];
public function books()
{
return $this->hasMany(Book::class);
}
public function getAllBooksAttribute() {
return $this->books->something...
}
In this case, every time the app tries to resolve your Author model it will fetch the books, that in turn will fetch the Author, that in turn will fetch the books again and on and on...
From your snippets, is not clear what is causing the problem but this answer might give some leads where to search for it.
To solve it, you might remove the relation from the $with and load it manually: $author->load('books') or Author::with('books')->where...
You can also load a relation of a relation in this way, for example: $author->load('books', 'books.comments') or Author::with('books', 'books.comments')->where...
It all comes down what you are trying to achieve. So you have to evaluate what and what not you should auto-load.
Be careful when loading automatically relations on your models and when adding accessors to $appends, especially if they use relations. It is an awesome feature but can bite hard sometimes.
I am trying to assign users as moderators to a forum category. At the moment, I am only trying to display route where a user can add moderators subreddit/{id}/moderators and display the subreddit name.
For that, I am getting No query results for model [App\Subreddit]
I must've messed up the relations between the tables somewhere.
My tables
#users: id, name, email...
#subreddits: id, name, user_id...
#moderators: id, user_id, subreddit_id
Routes.php
Route::get('subreddit/{id}/moderators', [
'as' => 'moderators',
'uses' => 'ModeratorsController#create'
]);
Moderator.php Model
class Moderator extends Model
{
protected $table = 'moderators';
protected $fillable = ['user_id', 'subreddit_id'];
public function subreddit() {
return $this->belongsToMany('App\Subreddit');
}
public function user() {
return $this->belongsTo('App\User');
}
}
In User Model, I have a hasManyThrough
public function moderators() {
return $this->hasManyThrough('App\Moderator', 'App\Subreddit');
}
In Subreddit Model
public function moderators() {
return $this->hasMany('App\Moderator');
}
And then in ModeratorsController I have the following create() method
public function create(Moderator $moderator, Subreddit $subreddit, User $user)
{
$subreddit = Subreddit::with('user')->findOrFail($subreddit->id);
return view('user/moderators')->with('subreddit', $subreddit)->with('user', $user)->with('moderator', $moderator);
}
If I change findOrFail to firstOrFail it will get me the first subreddit in the database, but I don't want the first, I want the exact one I'm trying to add moderators to.
class User extends Model{
public function canModerate(){ //name this as you wish
return $this->belongsToMany('App\Subreddit','moderators');
}
public function subreddits(){
return $this->hasMany('App\Subreddit');
}
}
class Subreddit extends Model{
public function moderators(){ // name this as you wish
return $this->belongsToMany('App\User','moderators');
}
public function creator(){
return $this->belongsTo('App\User');
}
}
remove id from moderators table and you wont need the Moderator class.
Additional documentation can be found here.
Edit RouteServiceProvider::boot method and add the following line:
$router->model('subreddit', 'App\Subreddit');
Documentation can be found here.
Your route should then look like:
Route::resource('subreddit.moderator','ModeratorsController');
The URL should look like http://localhost/subreddit/{subreddit}/moderator/create
And finally the controller method should be:
public function create(Subreddit $subreddit)
{
$user = $subreddit->creator;
$moderators = $subreddit->moderators()->get();
return view('user/moderators')->with(compact('subreddit','user','moderators'));
}