Eloquent: Relationships - php

I have a big problem with the relation between two tables.
I want to link Subscription to Orders like this :
$subs = Subscription::first()->order->id
But nothing work i tried everything.
Subscription table :
Order table :
I tried this model but it don't work :
namespace App;
use Illuminate\Database\Eloquent\Model;
class Subscription extends Model
{
public $timestamps = false;
public function order()
{
return $this->hasOne('App\Order');
}
}
Order model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Order extends Model
{
public $timestamps = false;
public function payouts()
{
return $this->hasMany('App\Payout');
}
public function subscription()
{
return $this->belongsTo('App\Subscription');
}
}
error :
ErrorException: Trying to get property of non-object in /var/www/vhosts/trafficshield.tools/httpdocs/app/Http/Controllers/CampaignController.php:58
Line 58 : $order_id = Subscription::where('id', '=', 'GjhQpVSdQPirGmEEkJ6pGw')->first()->order->id;
Thank's for your

Because you are using a non autoincrement value as primary key you have to put this in your models:
public $incrementing = false;

Related

cannot retrieve Category model with related items

I have very strange error in my laravel website.
I try to retrieve Category model with related items, but looking at the error I see that laravel tries to retrieve items field from category model and of course it fails. But I am completely cannot understand why it happens, because I have the same code working well in other parts of my website.
routes/web.php
Route::name('gallery.')->prefix('gallery')->group(function () {
Route::get('/', 'GalleryController#index')->name('index');
Route::get('/{slug}', 'GalleryController#item')->name('item');
});
GalleryController
public function item($slug)
{
$category = $this->imageRepository->getCategoryWithPaginatedImages($slug, $perPage = 20);
return view('pages.gallery.item', compact('category'));
}
ImageRepository
namespace App\Repositories;
use Illuminate\Database\Eloquent\Collection;
use App\Models\ImageCategory as Model;
class ImageRepository extends CoreRepository
{
protected function getModelClass()
{
return Model::class;
}
public function getCategoryWithPaginatedImages($slug, $perPage = null)
{
$columns = ['id','title','slug','description','image','published','metatitle','metakey','metadesc'];
$result = $this
->startConditions()
->whereSlug($slug)
->select($columns)
->with('images:id,title,category_id,md,lg')
->firstOrFail()
->toArray();
$result = Arr::arrayToObject($result);
$result->items = collect($result->items)->mypaginate($perPage);
return $result;
}
}
Image
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Image extends Model
{
protected $guarded = [];
public function category() { return $this->belongsTo(ImageCategory::class); }
}
ImageCategory
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class ImageCategory extends Model
{
protected $guarded = [];
public $timestamps = false;
public function images() { return $this->hasMany(ImageCategory::class, 'category_id'); }
}
so when I hit gallery/slug then getCategoryWithPaginatedImages gives me following error
Illuminate\Database\QueryException
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'category_id' in 'field list' (SQL: select `id`, `title`, `category_id`, `md`, `lg` from `image_categories` where `image_categories`.`category_id` in (2))
I guess the relationship images on ImageCategory definition has an issue, should be as under
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class ImageCategory extends Model
{
protected $guarded = [];
public $timestamps = false;
public function images()
{
return $this->hasMany(Image::class, 'category_id');
}
}

Problem with Laravel Eloquent - relation not working

I'am beginner in Laravel. I have project in Laravel 5.8.
I have User model:
class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable;
use psCMS\Presenters\UserPresenter;
use scopeActiveTrait;
public static $roles = [];
public $dates = ['last_activity'];
// ...
public function scopeHistory()
{
return $this->hasMany('App\UserLoginHistory');
}
// ...
}
and UserLoginHistory:
class UserLoginHistory extends Model
{
protected $quarded = ['id'];
public $timestamps = false;
protected $fillable = ['user_id', 'date_time', 'ip'];
public function user()
{
return $this->belongsTo('App\User');
}
}
I want show user login history by this code:
User::history()->where('id', $idAdmin)->orderBy('id', 'desc')->paginate(25);
but it's not working.
This function not working - I haven't got results.
How can I fixed it?
First of all, you are defining your relationship as a scope (prefixing the relationship with the scope keyword). Try updating your model relationship to this:
public function history()
{
return $this->hasMany('App\UserLoginHistory');
}
Then, given your query, it seems that you want to get all the UserLoginHistory
records for a given User. You could accomplish this in two ways (at least).
From the UserLoginHistory model itself, constraining the query by the foreign key value:
$userId = auth()->id(); // get the user ID here.
$results = UserLoginHistory::where('user_id', $userId)->paginate(15);
// ^^^^^^^ your FK column name
From the User model using your defined relationship:
$userId = auth()->id(); // get the user ID here.
$results = User::find($userId)->history;
The downside of the second approach is that you'll need to paginate the results manually.
in your User model you should define your relation by this way :
public function history()
{
return $this->hasMany('App\UserLoginHistory');
}
then if you would like to select with history model you can do that with WhereHas() method :
User::whereHas(['history'=>function($q) use ($idAdmin) {
$q->where('id',$idAdmin)
}])->orderBy('id', 'desc')->paginate(25);
You must be do this changes
public function history()
{
return $this->hasMany('App\UserLoginHistory');
}
usage
$user = User::find($idAdmin);
$userHistories = $user->history()->latest()->paginate(25);
or get user with all history
User::with('history')->find($idAdmin);
// Post model
namespace App;
use App\User;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function categories()
{
return $this->belongsToMany('App\Category')->withTimestamps();
}
}
// Category model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
public function posts()
{
return $this->belongsToMany('App\Post')->withTimestamps();
}
}

Many To Many (Polymorphic) using the same model with different types

I have these 3 tables in the database:
I'm using Many To Many (Polymorphic) Eloquent relationship to connect the Models. The problem is that the Creadores table can be of type artista or autor in the Creaciones table.
Is it possible to tell Eloquent when to use artista or autor?
It works if I extend the Creador Model into 2 other Models: Artista and Autor. But when I want to show all the creaciones of a creador using the Creador Model, it's not possible because the Polymorphic relationship was created with the extended models.
Libro Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use ChrisKonnertz\BBCode\BBCode;
class Libro extends Model
{
protected $table = 'Libros';
// Return all the artists of the book
public function artistas()
{
return $this->morphedByMany('App\Creador', 'creador', 'creaciones');
}
// Return all the authors of the book
public function autores()
{
return $this->morphedByMany('App\Creador', 'creador', 'creaciones');
}
}
Creador Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Creador extends Model
{
protected $table = 'creators';
// Return all the books where author
public function autorLibros()
{
return $this->morphToMany('App\Libro', 'creador', 'creaciones');
}
// Return all the books where artist
public function artistaLibros()
{
return $this->morphToMany('App\Libro', 'creador', 'creaciones');
}
}
You might be better off just adding a type property to Creador with 'artista'/'autor' in it.
The polymorphic relationship can only take a single model.
So your code would then become:
public function creadors()
{
// Return a general relation for all 'creadores'.
return $this->morphedByMany(App\Creador::class, 'creador', 'creaciones');
}
public function artistas()
{
// Filter for 'artista's.
return $this->creadors()->where('type', 'artista');
}
public function autores()
{
// Filter for 'autor's.
return $this->creadors()->where('type', 'autor');
}
Solved it the following way. Changed the relation from a Polymorphic Many to Many to a normal Many to Many, adding withPivot and wherePivot.
Creador Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Creador extends Model
{
protected $table = 'creators';
public function libros()
{
return $this->belongsToMany('App\Libro', 'creaciones')->withPivot('creador_type');
}
// All books as an Artist
public function librosArtista()
{
return $this->libros()->wherePivot('creador_type', 1);
}
// All books as an Author
public function librosAutor()
{
return $this->libros()->wherePivot('creador_type', 2);
}
}
Libro Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use ChrisKonnertz\BBCode\BBCode;
class Libro extends Model
{
protected $table = 'libros';
public function creador()
{
return $this->belongsToMany('App\Creador', 'creaciones')->withPivot('creador_type');
}
// All book artists
public function artistas()
{
return $this->creador()->wherePivot('creador_type', 1);
}
// All book authors
public function autores()
{
return $this->creador()->wherePivot('creador_type', 2);
}
}
And when creating a attaching a Creador to a Libro:
$libro->artistas()->attach( $creador, [
'creador_type' => 1
]);

Laravel Eloquent: 3 table relationship

I'm new to Laravel-eloquent, I would like to translate this SQL to Eloquent mode:
select
fl.id, fut.id, fut.firebase_topic_id, ft.id, fl.created_at
from
firebase_logs fl,
firebase_user_topics fut,
firebase_topics ft
where
fl.id = fut.firebase_log_id
and
fut.firebase_topic_id = ft.id
and
fl.created_at between '2019-01-09 16:33:39' and '2019-01-09 16:33:41'
and
ft.id = 1
order by fl.created_at asc
Where:
Firebase_logs.id (1) -> Firebase_user_topics.firebase_log_id (N)
and
Firenase_user_topics.firebase_topic_id (N) -> Firebase_topics.id (1)
FirebaseLog.php:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class FirebaseLog extends Model
{
public $incrementing = false;
protected $primaryKey = 'id';
public function user_topics() {
//return $this->hasManyThrough(FirebaseTopics::class, FirebaseUserTopics::class);
return $this->hasMany(FirebaseUserTopics::class);
}
}
FirebaseUserTopics.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class FirebaseUserTopics extends Model
{
protected $table = 'firebase_user_topics';
public function log()
{
return $this->belongsTo(FirebaseLog::class);
}
public function topic()
{
return $this->belongsTo(FirebaseTopics::class);
}
}
FirebaseTopics.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class FirebaseTopics extends Model
{
protected $table = 'firebase_topics';
public function user_topics()
{
return $this->hasMany(FirebaseUserTopics, 'firebase_user_topics');
}
}
My Controller, works fine with this:
$a = FirebaseLog::with('user_topics')->whereBetween('created_at', array('2019-01-09 16:33:39', '2019-01-09 16:33:41'))->get();
return $a;
But I don't know how to connect to FirebaseTopics to continue building the code, some help will be appreciated.
EDITED ANSWER!
The solution of your problem is use the hasOne relation instead of belongsTo in your FirebaseUserTopics model. It must be following;
public function topic()
{
return $this->hasOne(FirebaseTopics::class, 'id', 'firebase_topic_id');
}
Because your FirebaseTopics model has not a relation with FirebaseUserTopics model. The "belongsTo" (that uses to make reverse a relation) search firebase_topic_id field in the firebase_topics table but this field has no in the firebase_topics table. That's why, you must be make to relation directly, not reverse.

Laravel 5.4 many to many relationships with foreign key

i'm using a data table with name auct_lots_full for my Lot.php model, where primary key is lot_id, in order everything to work i used Sofa/Eloquence extension, Mappable. So this is my model :
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Sofa\Eloquence\Eloquence;
use Sofa\Eloquence\Mappable;
class Lot extends Model
{
use Eloquence, Mappable;
protected $table = 'auct_lots_full';
protected $maps =[
'id' => 'lot_id',
];
public function scopeFilter($query, QueryFilter $filters)
{
return $filters->apply($query);
}
public function comments()
{
return $this->hasMany(Comment::class);
}
}
But he problem is that in some cases it keeps looking for id column as primary key. For example in LotsController.php i have this problem here :
public function show($id)
{
$lot = Lot::find($id);
return view('lots.show')->withLot($lot);
}
But i fix this problem with this solution:
public function show($id)
{
$lot = Lot::where('lot_id', $id)->first();
return view('lots.show')->withLot($lot);
}
But i understand that is just a solution for only this function...
So the same problem i have in CommentsController.php:
public function show()
{
$comments = Comment::orderBy('id', 'desc')->paginate(30);
return view('comments.browse', compact('comments'));
}
And i don't know how to fix it. Could any one explain me why is this happening? Is there a better way than use an extension? How i can fix this error in CommentsCotroller.php ?
This is the Comment.php model:
<?php
namespace App;
class Comment extends Model
{
public function lot()
{
return $this->belongsTo(Lot::class);
}
public function User()
{
return $this->belongsTo(User::class);
}
}
There is a primaryKey variable in your Model file which is id by default.
/**
* The primary key for the model.
*
* #var string
*/
protected $primaryKey = 'id';
If you override this variable in Lot model file. So your primary key will be lot_id instead of id as in default. Simply add this;
protected $primaryKey = 'lot_id';
So actually i find a proper way to do it with out Sofa/Eloquence extension, using not only foreign key but also a local key in many to many relationship. So this is the new code:
so for Lot.php i did this:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Lot extends Model
{
protected $table = 'auct_lots_full';
protected $primaryKey = 'lot_id';
public function scopeFilter($query, QueryFilter $filters)
{
return $filters->apply($query);
}
public function comments()
{
return $this->hasMany(Comment::class,'lot_id', 'lot_id');
}
}
Than i did same for the Comment.php model:
<?php
namespace App;
class Comment extends Model
{
public function lot()
{
return $this->belongsTo(Lot::class, 'lot_id', 'lot_id');
}
public function User()
{
return $this->belongsTo(User::class);
}
}
So what we see above, in Lot.php model, function comments i pass foreignKey: 'lot_id' in auct_lots_full table and localKey 'lot_id' in comments table witch refers to the auct_lots_full table. In Comment.php model wi did the same but in case instead of localKey it is ownerKey. Im a bad at explaining so i will attach some images to make sense.
Lot.php
Comment.php

Categories