ORM not working in Default UserController & User Model in laravel - laravel

I want to get User wise Role. here is I'm facing error ....
UserController.php ( user controller file )
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\UserRequest;
use App\Employee;
use App\Role;
use App\User;
use App\Site;
use App\Client;
use App\ProjectType;
use App\UserPermission;
use Auth;
use DB;
use App\Project;
class UsersController extends BaseController {
public function __construct() {
$this->isSetClientAndProjectType();
$data = User::with('Role')->first();
echo "<pre>";print_r(json_decode($data)); die;
}
}
User.php ( user model file )
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Authenticatable {
use SoftDeletes;
use Notifiable;
protected $fillable = [
'name', 'role_id', 'password', 'siteid', 'email', 'status', 'allowed_to_bypass_pm', 'allowed_to_bypass_admin'
];
protected $hidden = [
'password', 'remember_token',
];
// Get users roles
public function Role() {
return $this->hasMany('App\Role', 'role_id', 'id');
}
}
Error is
How can i solve this error?
Help me guys.
Thank You.

If a user has many "roles" it should be public function roles().
You have defined:
A single user has a role_id
Therefore you need:
If a user has a single role it would be:
public function role() {
return $this->belongsTo('App\Role');
}
The reverse on the Role model would be:
public function users() {
return $this->belongsToMany('App\User');
}
Since many users can have the same role.
Hope this helps.

You need to add belongsTo relationship
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Authenticatable {
use SoftDeletes, Notifiable;
protected $fillable = [
'name', 'role_id', 'password', 'siteid', 'email', 'status', 'allowed_to_bypass_pm', 'allowed_to_bypass_admin'
];
protected $hidden = [
'password', 'remember_token',
];
// Get user's role
public function role() {
return $this->belongsTo('App\Role');
}
}
Now fetch data
$user = User::with('role')->find(1);
$role = $user->role;

You need to make sure your table structure and foreign key references are compatible with the model relationship methods used.
For example, you have used "HasMany" relationship on User model. For that, you will have to make sure that each record/row in users table(User model) "has many" associated records/rows in roles table(Role model).
Here HasMany method assumes a foreign key "role_id" on roles table(Role Model). On not finding of which, it throws error.
You first need to take in consideration the table structure of roles and users table(User and Role model) as per your requirement, and than add model relationship methods accordingly.
It can be a bit tricky if you are using the methods for the first time, you can refer the laravel documentation for the same:
eloquent-relationships

Related

How to get the "catagory name" in Laravel?

How to get "category name" show in the product table not as "category_id"?
I already try to combine any solutions for this. But still can't solve this prob.
I'd love to hear other suggestions from the masters here.
Category Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Kategori extends Model
{
use HasFactory;
protected $casts = [
'updated_at' => 'datetime:d/m/Y, H:i:s'
];
public function Kategori()
{
return $this->hasMany('App\Models\Produk');
}
}
Product Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Produk extends Model
{
use HasFactory;
protected $casts = [
'updated_at' => 'datetime:d/m/Y, H:i:s'
];
public function Produk()
{
return $this->belongsTo('App\Models\Kategori', 'kategori_id');
}
}
Category Table
Product Table
Product Controller >>>> in my opinion may be my prob at here, but not so sure.
<?php
namespace App\Http\Controllers;
use App\Models\Produk;
use RealRashid\SweetAlert\Facades\Alert;
use Yajra\Datatables\DataTables;
use Illuminate\Http\Request;
class ProdukController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function json(){
return Datatables::of(Produk::all())->make(true);
}
public function index(){
return view('back.produk.show');
}
}
Pass the other model using your relationship
public function json(){
return Datatables::of(Produk::with('produk')->get())->make(true);
}
to which say that is named $produks, you can access it as
$produk->produk->nama;
As a side note, Do name your relationships the names of the other model.
for example, in the Kategori class, the relationship to Produk should be named produks (it is a hasMany relationship) as opposed to Kategori. Similarly, in the Produk class, the relationship to Kategori being named kategori() to which from the above answer you access it like
$produk->kategori->nama;
You should use:
public function json(){
return Datatables::of(Produk::with('Kategori')->all())->make(true);
}
I recommend to use camelCase for method names. I'm not sure how Datatables will handle this case.
In your ProdukController you need pass below code.
$result = Produk::with('Produk')->get();
When you dd($result);, you should see the related models in the relations array attribute.
To access the relations' properties from there, it is simply
$result->Produk->catagory_name

laravel one-to-many relationship is null

I have 3 models: User, Company and Branch.
In a blade file i want to be able to display the branch the company of the user belongs to.
In my opinion i should have the following relationships:
User -> Company : user belongsto a company and a company has many users , so this in a one-to-many relationship.
Company -> Branch: A company belongsto a branch and a branch can have many companies. So once again a one-to-many relationship.
I have foreign keys in the users table: company_id which references id on the company table.
Another FK in the company table: branch_id which references id on the branch table.
In my blade file want to display the branch name like this: {{ $user->company->branch->name }} where only name is a property. Company and branch are relationships.
My query looks like this:
$users = User::with(['company','company.branche' => function($q){
$q->select('name');
}])->inRandomOrder()->paginate(24);
<?php
namespace App;
use App\Events\GeneralEvent;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Nicolaslopezj\Searchable\SearchableTrait;
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable, SoftDeletes, HasRoles, SearchableTrait;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'gender', 'first_name','last_name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* #var array
*/
...
public function company()
{
return $this->belongsTo(Company::class,'company_id');
}
}
<?php
namespace App;
use App\Events\GeneralEvent;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Nicolaslopezj\Searchable\SearchableTrait;
class Company extends Model
{
use SoftDeletes, SearchableTrait;
...
public function branche()
{
return $this->belongsTo(Branche::class);
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Nicolaslopezj\Searchable\SearchableTrait;
class Branche extends Model
{
protected $fillable = [
'name'
];
protected $searchable = [
'columns' => [
'name' => 10,
],
];
public function companies()
{
return $this->hasMany(Company::class);
}
}
However when i dump the $user->company i get null. So adding the branch after that is pointless for now. When i dump the user the relation shows up but is null. I have no idea where i am going wrong. Can someone please help?
You have to include the id of the branch so that eloquent can link the branch and company
you can do like that
$users = User::with(['company','company.branche' => function($q){
$q->select('id','name');
}])->inRandomOrder()->paginate(24);
OR Like that
$users = User::with('company.branche:id,name')->inRandomOrder()->paginate(24);
So the problem was i had one of my foreign keys pointing to the wrong table. Doh.

Argument 1 passed to App\Http\Controllers\ApiController::showAll() must be an instance of Illuminate\Database\Eloquent\Collection

I want to to retrieve all buyers for a specific saller.When I remove pluck and others methods chaining after get method it's working. But is not exact thing that I want. How Can I solve this provlem?
(source: licdn.com)
<?php
namespace App\Http\Controllers\Seller;
use App\Http\Controllers\ApiController;
use App\Seller;
use Illuminate\Http\Request;
class SellerBuyerController extends ApiController
{
public function index(Seller $seller)
{
$buyers = $seller->products()
->whereHas('transactions')
->with('transactions.buyer')
->get()->pluck('transactions')
->collapse()->pluck('buyer')
->unique('id')
->values();
return $this->showAll($buyers);
}
protected function showAll(Collection $collection, $code = 200)
{
return $this->successResponse($collection, $code);
}
protected function successResponse($data, $code)
{
return response()->json($data, $code);
}
}
Seller model hasMany relation to products
<?php
namespace App;
use App\Scopes\SellerScope;
class Seller extends User
{
public function products()
{
return $this->hasMany(Product::class);
}
}
Product Model hasMany relation to transactions
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Product extends Model
{
use SoftDeletes;
protected $fillable = [
'name', 'description', 'quantity', 'status', 'image', 'seller_id',
];
public function transactions()
{
return $this->hasMany(Transaction::class);
}
}
Transaction Model and relation to buyer
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Transaction extends Model
{
use SoftDeletes;
protected $fillable = [
'quantity', 'buyer_id', 'product_id'
];
public function buyer()
{
return $this->belongsTo(Buyer::class);
}
}
You are missing an import at the top:
use Illuminate\Support\Collection;
otherwise it assumes Illuminate\Database\Eloquent\Collection is to be used.
And values() obviously returns the support collection, not an eloquent one.
If you have a relationship from Buyer to Transaction you can go at this from the other direction to get buyers. You also need to make sure there is a relationship from Product to Seller (make sure you have the inverse of every relationship setup)
Buyer::whereHas('transactions.products.seller', function ($query) use ($seller) {
$query->where('id', $seller->id); // might have to be a more specific key name
})->get();
You would end up with a Eloquent Collection of Buyers who have transactions including products from a particular seller.
you should put use Illuminate\Support\Collection; in folder traits over apiResponser.php

insert data to table pivot use form and show it index.blade.php in laravel 5.7

I have form create team.blade.php below this
Form Team index.blade.php
display the name of the one-team user with that user, and display the project that is being done by the user, and display the user as what (role).
the relationship of one user has many teams. and one team has many users. therefore, I choose many to many relations. but when I create team, I want to insert user_id and team_id in the pivot user_teams table as the relation table between user and team.
but when I tried to create team failed, he did not save data to the user_teams table.
and in the team index, he does not display the name of a team user with that user, and displays the project that is being done by the user, and displays the user as what.
my user models
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Presence;
use App\Models\Project;
use App\Productivity;
use App\Sick_leave;
use App\Annual_leave;
use App\Models\Team;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'password', 'role_id',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function role()
{
return $this->belongsTo(Role::class, 'role_id');
}
public function teams()
{
return $this->belongsToMany(Team::class, 'user_teams');
}
public function projects()
{
return $this->belongsToMany(Project::Class, 'user_projects');
}
}
Team Models
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\User;
use App\Role;
use Auth;
class Team extends Model
{
use SoftDeletes;
protected $table = 'teams';
protected $fillable = [
'id',
'project_id',
];
public function users()
{
return $this->belongsToMany(User::class, 'user_teams');
}
public function project()
{
return $this->belongsTo(Project::class);
}
public function role()
{
return $this->belongsTo(Role::class);
}
}
Project Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Project extends Model
{
use SoftDeletes;
protected $table = 'projects';
protected $fillable = [
'project_id',
'project_name',
'start_date',
'end_date',
'project_category',
];
public function users()
{
return $this->belongsToMany(User::class, 'user_projects');
}
public function team()
{
return $this->hasOne(Team::class);
}
}
UserTeam model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class UserTeam extends Model
{
use SoftDeletes;
protected $table = "user_teams";
public function team()
{
return $this->belongsTo(Team::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
}
Team Controller
public function index()
{
$users = auth()->user()->name;
$users = User::all();
return view ('teams.index', compact ('teams', 'users', 'projects'));
}
public function create()
{
$users = User::all();
$projects = Project::pluck('project_name', 'id');
return view ('teams.form', compact('projects', 'users'));
}
public function store(Request $request)
{
$team = Team::create($request->all());
$userIds = User::find(2);
$team->users()->attach($userIds);
return redirect()->route('team.create');
}
In user_teams has fields user_id and team_id. how do i overcome this??
You have to create the team and then attach the users that belong to it.
Example from the docs:
Attaching
Eloquent also provides a few additional helper methods to make working
with related models more convenient. For example, let's imagine a user
can have many roles and a role can have many users. To attach a role
to a user by inserting a record in the intermediate table that joins
the models, use the attach method:
$user = App\User::find(1);
$user->roles()->attach($roleId);
In your case:
Team Controller
public function store(Request $request)
{
$team = Team::create($request->except('user_id'));
$team->users()->attach($request->get('user_id', []));
return redirect()->route('team.create');
}

Models in laravel

I'm developing an app in laravel, specifically a social network.
After wasting some time stuck on an issue I found out I had two files wich responded to the user model.
One is namespaced under "appname" and the other under "appname\Models",
Adding the posts() method in the one under "appname" gave me an error where the method couldn't be found, so I assumed the one under "appname\Models" was the correct one. Although deleting the "User.php" under "appname" gives me a
Fatal error: Class 'Instyle\User' not found
error.
I'm sure I've misunderstood something along the lines I just can't point out where.
app\Models\User.php
namespace Instyle\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
protected $table = 'users';
protected $fillable = [
'username',
'email',
'password',
'first_name',
'last_name',
'location',
];
protected $hidden = [
'password',
'remember_token',
];
public function getName()
{
if($this->first_name && $this->last_name)
{
return "{$this->first_name} {$this->last_name}";
}
if ($this->first_name)
{
return $this->first_name;
}
return null;
}
public function getUsername()
{
return $this->username;
}
public function getFirstNameOrUsername()
{
return $this->first_name ?: $this->username;
}
public function getAvatarUrl()
{
$hash = md5(strtolower(trim($this->attributes['email'])));
return "http://www.gravatar.com/avatar/$hash?d=https://u.pomf.is/maqope.png";
}
public function posts()
{
return $this->hasMany('Instyle\Post');
}
}
app/User.php
<?php
namespace Instyle;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
public function posts()
{
return $this->hasMany('Instyle\Post');
}
}
app\post.php
<?php
namespace Instyle;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $fillable = ['body'];
// protected $appends = ['humanCreatedAt'];
public function user()
{
return $this->belongsTo('Instyle\User');
}
}
if your application name is Instyle, Instyle\User is the User.php inside the app folder not the User.php in app/Models/.
More info: learn more in psr namespacing and autoloading. check your compose.json . here it says that namespace Instyle is app/ folder and from here the namespace at the top of each file goes according to the absolute folder path.
"psr-4": {
"Instyle\\": "app/"
}
One more thing, If you are willing to use User.php anywhere else and thinking it for authentication purpose remember to change authentication configuration declared in config/auth.php accordingly
I would avoid to have both models with the same name because it may collide one with the other, although they are in different paths.
Make sure you are importing your custom models with the use path\to\your\namespace\Model; expression to allow the root path of your models to be located in a correct way.
May be helpful to see your Post and User models sample code to evaluate the error.
UPDATE: Why don't you try to generate only one User model and, the other data related with the User Profile, try to save in one One-to-One Relationship to a Profile table? i.e. firstname, lastname, location, twitter_account, facebook_account, phone_number, and so on could be stored in a User_Profile or Profile separated table, then you maintain your User table with the minimum required fields and delete the other one.

Resources