How to use table without model in hasMany - laravel

is that possible to use direct table name in hasMany. for example
public function Permissions()
{
return $this->hasMany('TABLE NAME');
}
I have table named PERMISSION but don't have a model of this table, but i want to use this table in hasmany.

Relations in Laravel are a part of Eloquent ORM which is built on the use of models.
So, no, it is not possible to use relations without making models for them.

Just create a model for that table
php artisan make:model Permisson
then add the table name to the model class
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Permisson extends Model
{
protected $table = "table_name";
}
and add the relation
public function permissions()
{
return $this->hasMany(Permisson::class)
}
I hope it helps

Related

one to one relationship between two table in laravel by controllers

I have a table user table and want to join address table with user table primary key which is foreign key in address table.
Please let me know how I can perform one to one relationship between both of these table by controllers in Laravel and display them in view.
You can simply achieve this by adding an eloquent relationship in Model files.
User.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* Get the phone associated with the user.
*/
public function address()
{
return $this->hasOne(Address::class);
}
}
Address.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Address extends Model
{
/**
* Get the user that owns the phone.
*/
public function user()
{
return $this->belongsTo(User::class);
}
}
Note: Both of the above models will assume by default user_id as a foreign key in the Address table.
To access this relationship in Controller,
$address = User::find($user_id)->address;
To access this relationship in View,
{{ $user->address->pincode }}
Since this is a Duplicate post, you can refer to this
Display in VIEW of One to One relation in Laravel Relationship

Is there a way to specify the pivot table for a has many relationship in Laravel?

I have a Company model that has a relationship with users. I have a pivot table called game_user generated with https://github.com/laracasts/Laravel-5-Generators-Extended by doing php artisan make:migration:pivot companies users . I have the following migration
public function up()
{
Schema::create('company_user', function (Blueprint $table) {
$table->unsignedBigInteger('company_id')->unsigned()->index();
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$table->unsignedBigInteger('user_id')->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->primary(['company_id', 'user_id']);
});
}
On my Company Model I made a employees function for the relationship
<?php
namespace App\Models;
use App\User;
use Illuminate\Database\Eloquent\Model;
class Company extends Model
{
public function employees()
{
return $this->hasMany(User::class);
}
}
And on my CompanyController I have the following
<?php
namespace App\Http\Controllers;
use App\Models\Company;
use Illuminate\Http\Request;
class CompanyController extends Controller
{
public function index($id)
{
return Company::find($id);
}
public function employees($id)
{
return Company::find($id)->employees;
}
}
But when requesting to it from postman with a get request.
http://localhost/laravel_applications/myapi/public/api/company/1/employees
I get a 500 internal server error. Shouldn't Laravel automatically detect the table? I'm quite sure it isn't detecting or even looking for my pivot table.
This is my route, I tested it by returning as string and that worked just fine. So the route is working.
Route::get('company/{id}/employees', 'CompanyController#employees');
Method hasMany should be used for one-to-many relation. You need to call belongsToMany method for a many-to-many relation.

I am trying to make Multi Level Category using laravel

I am trying to make Multi-Level Category using laravel but I am facing this error: How to fix this error?
Illuminate\Database\QueryException
SQLSTATE[42S02]: Base table or view
not found: 1146 Table 'storykadb.category_navmenus' doesn't exist
(SQL: select * from category_navmenus where p_id = 0)
Model
namespace App;
use Illuminate\Database\Eloquent\Model;
class category_navmenu extends Model
{
public function childs(){
return $this->hasMany('App\category_navmenu','p_id');
}
}
Route
Route::get('test',function(){
return App\category_navmenu::with('childs')->where('p_id',0)->get();
});
Laravel Eloquent is taking the wrong table name, either you can change your Model name and table name according to Laravel's naming convention or add in the $table property in your model, like this :
namespace App;
use Illuminate\Database\Eloquent\Model;
class category_navmenu extends Model
{
public $table = "category_navmenu";
public function childs(){
return $this->hasMany('App\category_navmenu','p_id');
}
}
Because Laravel will find your table by changing your classname to lowercase underscore and plural. Just like:
CategoryNavmenu will become category_navemenus.
You have no specifies the table name of the protected $table. So laravel will find the table by default rule.
You can put this line in your model:
protected $table = 'category_navemenu';
so laravel can find your table.
But the important thing is plz change your class name to Upper Camel Case。This is a normal grammatical norms.
Just Change your model file name to CategoryNavmenu.php and classname to CategoryNavmenu.

Hasmany with child of child in laravel 5.5

I have 3 tables.
shops
shop_foods
foods
I need to fetch foods table data when I create hasmany relation with shops_food and store.
$this->hasMany('App\Diet\ShopFood', 'shop_id', 'id');
Please, show your code so we can know what are you trying to do.
But what I can see here is that you have a wrong relationship.
Why are you assigning hasMany in a Many to Many relationship?
In your Shop Model you can make a foods relationship with:
$this->belongsToMany('App\Diet\Food);
Then you can retreive you food when calling
$shop->foods
And the shop_foods with the Pivot property
If i understand correctly you want to get foods when you call shops->shop_foods. if is that
//first you call your shops as you want.
Shop::with(['shops_food' => function($query){
//the 'shops_food' relationship should be called within an array
//this way you could query the relationship as the eloquent model.
//that way you could call the 'foods' relationship inside the shops_food relationship.
$query->with('foods')
}])
...
Note that you must have the relationship declared in shop and shop_foods models
lets your models are like these
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Shop extends Model
{
//
public function shops_food()
{
//shop_id is the foreing key inside your shop_foods table
return $this->hasMany('App\ShopFood','shop_id');
}
....
}
then ShopFood Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class ShopFood extends Model
{
//
public function foods()
{
//shop_food_id is the foreing key inside your foods table
return $this->hasMany('App\Food','shop_food_id');
}
....
}
This reads as if you want
public function foods() {
$this->hasMany('App\Diet\Food');
}
in your ShopFood model and in your Shop model
public function shopfoods() {
$this->hasMany('App\Diet\ShopFood')->with('foods');
}
You can also make 2 separate relations in the Shop model:
public function shopfoods() {
$this->hasMany('App\Diet\ShopFood');
}
public function shopfoodsWithFoods() {
$this->hasMany('App\Diet\ShopFood')->with('foods');
}
So that way you can use whatever you need at that moment.
But the whole thing is really not clear...
I am not even sure how the 3 table are connected, so the hasMany are just guesses.
Nevertheless you can just go with the "with" function.
PS
There is also the possibility to just declare
protected $with = ['foods'];
in your ShopFood model, if you ALWAYS want those 2 connected. It's all in the documentation.

Laravel model: instance or relationship?

I'm not a professional programmer, so I don't know that I'm describing this very well.
Eloquent relationships are established in the model, using syntax and functions such as ... - >belongsTo.. etc.
Behind these models, are tables in my database.
In my (laravel) application, I have a logged in user who needs certain information about other users. At the end of the day, they're all just users, persisting in the user's table.
So when I use a relationship to another object, (e.g. car) all is good. When I try use a relationship to another user I get errors like Cannot redeclare class App\Models\User.
I think I'm misunderstanding something here.
I get the feeling maybe I should be 'instantiating' another version of my User (as 'manager') ... But do I really need to? It's more of a lookup than anything else. I'm not sure I would even know how to do that.
Some pointers please?
It sounds like you created two distinct "User" models:
// /app/User.php:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
// ...
public function user() {
return $this->hasOne('App\Models\User');
}
}
// /app/models/User.php:
<?php namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
// ...
public function user() {
return $this->belongsTo('App\User');
}
}
Instead you want to have a single class which belongs to itself:
// /app/User.php:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
// ...
public function parent() {
return $this->belongsTo('App\User');
}
public function children() {
return $this->hasMany('App\User');
}
}
Then in your database make sure that the users table has a user_id property (edit database/migrations/2014_10_12_000000_create_users_table.php):
$table->integer('user_id')->unsigned()->nullable();
$table->foreign('user_id')->references('id')->on('users');
Now you can attach users to one another:
<?php
$manager = new User();
$employeeOne = new User();
$employeeTwo = new User();
$manager->children()->saveMany([
$employeeOne,
$employeeTwo
]);
dd( $employeeTwo->parent->name ); // Manager's name

Resources