I already tried several things but I can't get this to work. I want to be able to make something like this {{ $user->city->name }}
My user model:
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
public $timestamps = false;
protected $table = 'users';
protected $fillable = ['id_city', 'name', 'email', 'password', 'admin'];
protected $hidden = ['password', 'remember_token'];
public function city()
{
return $this->belongsTo('App\City');
}
}
And this is my City model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class City extends Model
{
public $timestamps = false;
protected $table = 'cities';
protected $fillable = ['name', 'slug'];
public function users(){
return $this->hasMany('App\User');
}
}
And I'm trying to use {{ $user->city->name }} on my view but it doesn't work, it returns an error ErrorException: Trying to get property of non-object (View: .../views/app/text.blade.php).
What should I do?
Within your belongsTo relationship, Eloquent tries to match city_id as the foreign key by default as you don't pass the second argument.
However, according to your fillable attributes, what you have as the foreign key is actually id_city.
For the User model,
public function city()
{
return $this->belongsTo('App\City', 'id_city');
}
For the City model,
public function users(){
return $this->hasMany('App\User', 'id_city', 'id');
}
Related
there is a (card model) bleongsToMany contacts, and there is a (contact model) is bleongsToMany cards, I want when (card) get updated his children in the middle table in the relation became removed.
I'm trying to do this by using (the observer), I did all steps correctly but the issue is:
when (card model) updated his children still exist in the middle table of the relationship, I want to remove them when any update on the card.
Route:
Route::get('/test',function(){
$card = \App\Models\Card::find(1);
$card-> update([
'name' => 'firstCard1',
'user_name' =>1,
]);
return true;
});
Observer:
<?php
namespace App\Observers;
use App\Models\Card;
class CardObserver
{
public function updated(Card $card)
{
$card -> contact()-> delete();
}
}
Card Model:
<?php
namespace App\Models;
use App\Observers\CardObserver;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Card extends Model
{
use HasFactory;
protected $fillable = [
'name',
'user_id ',
];
public $timestamps = true;
protected $hidden = ['pivot'];
public function contact()
{
return $this->belongsToMany(Contact::class, 'card_contacts', 'card_id');
}
public function connection()
{
return $this->belongsTo(Connection::class, '');
}
protected static function boot()
{
parent::boot();
Card::observe(CardObserver::class);
}
}
Contact Model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Contact extends Model
{
use HasFactory;
protected $fillable = [
'provider_id',
'user_id',
'contact_string'
];
public $timestamps = true;
protected $hidden = ['pivot'];
public function card(){
return $this->belongsToMany(Card::class,'card_contacts','contact_id');
}
public function user(){
return $this->belongsTo(User::class,'user_id');
}
public function provider(){
return $this->belongsTo(Provider::class,'provider_id');
}
}
Card_contact Model (the pivot table):
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class CardContact extends Model
{
use HasFactory;
protected $fillable = [
'card_id',
'contact_id',
];
public $timestamps = true;
}
any help please?
I'm using laravel 5.4, have two models ParentAccount and ChildAccount,
A parent has many childs
Parent
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class ParentAccount extends Model
{
//
public $timestamps = false;
protected $table = 'parent_accounts';
protected $fillable = [
'name', 'account_id'
];
public function childs()
{
return $this->hasMany('App\ChildAccount','account_id', 'parent_id');
}
}
child
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class ChildAccount extends Model
{
//
public $timestamps = false;
protected $table = 'child_accounts';
protected $fillable = [
'name', 'account_id','parent_id'
];
}
When i use echo ParentAccount::find(1)->childs();
i get an error Call to a member function childs() on null although all parents have children
Note: child has parent_id where it's the account_id in the parent
in parent model
public function childs(){
return $this->hasMany(ChildAccount::class, 'parent_id' , 'account_id');
}
in child model
public function parent(){
return $this->belongsTo(ParentAccount::class, 'parent_id' , 'account_id');
Edited Code
public function childs()
{
return $this->hasMany('App\ChildAccount', 'parent_id','account_id');
}
The relation function must be in the format
public function post()
{
return $this->belongsTo('App\Post', 'foreign_key', 'other_key');
}
First you have to add the primaryKey property then change the relation parameters and add the revers relation to the ChildAccount :
ParentAccount :
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class ParentAccount extends Model
{
protected $primaryKey = 'account_id';
public $timestamps = false;
protected $table = 'parent_accounts';
protected $fillable = [
'name', 'account_id'
];
public function childs()
{
// 'foreign_key', 'local_key'
return $this->hasMany('App\ChildAccount', 'parent_id', 'account_id');
}
}
ChildAccount :
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class ChildAccount extends Model
{
protected $primaryKey = 'account_id';
public $timestamps = false;
protected $table = 'child_accounts';
protected $fillable = [
'name', 'account_id','parent_id'
];
public function parent() {
// foreign_key, 'other_key'
return $this->belongsTo('App\ParentAccount', 'parent_id', 'account_id');
}
}
With doing that you can get the childs :
ParentAccount::find(1)->childs;
You got Call to a member function childs() on null because ParentAccount::find(1) return null. Make sure you have ParentAccount with id=1 in your DB.
You need to change keys order like this:
public function childs()
{
return $this->hasMany('App\ChildAccount', 'parent_id', 'account_id');
}
I'm trying to implement a belongsToMany relationship, where I need to list products and right after the name of the same, the relationship is made between 3 tables, order, description and order_product.
As you can realize the field appears empty.
This is my belongsToMany relation:
Description Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Order;
class Description extends Model
{
protected $table = 'erp_product_description';
protected $primaryKey = 'erp_productid';
protected $fillable = ['erp_name'];
public $timestamps = false;
public function product()
{
return $this->belongsTo('App\Product', 'erp_productid','erp_productid');
}
public function order()
{
return $this->belongsToMany('App\Order', 'erp_order_product', 'erp_productid', 'erp_createdid');
}
}
Order Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Order extends Model
{
protected $table = 'erp_order';
protected $fillable = ['erp_createdid'];
public function description()
{
return $this->belongsToMany('App\Description', 'erp_order_product', 'erp_createdid', 'erp_productid');
}
}
I used the Laravel 'DebugBar' plugin to see which query is running and returned this:
select `erp_product_description`.*, `erp_order_product`.`erp_createdid` as `pivot_erp_createdid`, `erp_order_product`.`erp_productid` as `pivot_erp_productid` from `erp_product_description` inner join `erp_order_product` on `erp_product_description`.`erp_productid` = `erp_order_product`.`erp_productid` where `erp_order_product`.`erp_createdid` is null
Why my query received null in value? Any suggestion?
I am trying to create a relationship between Player and Roleplay and its returning null. I know for a fact it should be working because the following code works perfectly:
Roleplay::find(Auth::user()->id);
And returns the correct data, a full array of the correct data.
When trying to access it this way:
Auth::user()->roleplay->user_id;
It doesn't work, can someone help me find out why?
How do you know its empty?
Because {{var_dump(Auth::user()->roleplay)}} in blade view returns EMPTY
When using it the view I also get a undefined error.
Primary key of roleplay table (srp_user_statistics) is user_id, and the primary key of player table (users) is id
here is the code:
Player:
<?php
namespace App\Database\Frontend\User;
use Hash;
use Eloquent;
use \Illuminate\Auth\Authenticatable;
use \Illuminate\Contracts\Auth\Authenticatable as Authentication;
class Player extends Eloquent implements Authentication
{
use Authenticatable;
protected $primaryKey = 'id';
protected $table = 'users';
public $timestamps = false;
protected $fillable = [];
public function setPasswordAttribute($value)
{
$this->attributes['password'] = Hash::make($value);
}
public function setUsernameAttribute($value)
{
return $this->attributes['username'] = $value;
}
public function roleplay()
{
return $this->hasOne('App\Database\Frontend\User\Roleplay', 'user_id');
}
}
Roleplay:
use Eloquent;
class Roleplay extends Eloquent
{
protected $primaryKey = 'user_id';
protected $table = 'srp_user_statistics';
public $timestamps = true;
protected $fillable = [];
public function user()
{
return $this->belongsTo('App\Database\Frontend\User\Player', 'user_id', 'id');
}
public function government_role()
{
return $this->belongsTo('App\Database\Frontend\Roleplay\GovernmentRole', 'government_id');
}
}
I thinks you should add 'id' to hasOne() in the User model
public function roleplay()
{
return $this->hasOne('App\Database\Frontend\User\Roleplay', 'user_id', 'id');
}
And remove 'id' from belonsTo() in Roleplay model.
Side notes
This working
Roleplay::find(Auth::user()->id);
Is not a guarantee your relationships are set properly. All it does is
Roleplay::find(1); //$user->id returns an integer.
My query is like this :
<?php
public function getListReviews()
{
$reviews = Review::where('user_id', auth()->user()->id)
->get();
return $reviews;
}
From the query, it can get all review data by id
I want get user photo, store photo and product photo
I want get it use Eloquent: Relationships
How can I get it with Eloquent: Relationships?
My review model is like this :
<?php
namespace App;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
use Jenssegers\Mongodb\Eloquent\HybridRelations;
class Review extends Eloquent
{
use HybridRelations;
protected $connection = 'mongodb';
protected $fillable = ['user_id', 'user_name', 'product_id', 'product_name', 'store_id', 'store_name', 'invoice_number', 'rating', 'comments', 'created_at', 'updated_at'];
public function user()
{
return $this->belongsTo(User::class);
}
}
My user model is like this :
<?php
namespace App;
...
class User extends Authenticatable
{
...
protected $fillable = ['name', 'email', 'password', 'birth_date', 'mobile_number', 'photo'];
public function store()
{
return $this->hasOne(Store::class);
}
}
My store model is like this :
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Store extends Model
{
protected $fillable = ['user_id', 'name', 'address', 'phones', 'total_product', 'photo'];
public function products()
{
return $this->hasMany(Product::class);
}
}
My product model is like this :
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $fillable = ['store_id','category_id','name', 'photo','description'];
public function store()
{
return $this->belongsTo(Store::class);
}
}
If you want to find out product and store from review model then add two more methods to Review model as below.
Edit App\Review.php
//You already have key to to find store i.e. store_id
public function store()
{
return $this->belongsTo(Store::class);
}
//You already have key to to find product i.e. product_id
public function product()
{
return $this->belongsTo(Product::class);
}
then execute your query as below
$reviews = Review::where('user_id', auth()->user()->id)
->with('store')
->with('product')
->with('user')
->get();
and you can access them as below,
Iterate through $reviews object as $review
$review->store->photo;
$review->product->photo;
$review->user->photo;