I'm facing a problem, as to why the relation is not getting connected.
vendor.php
class Vendor extends Model
{
public function internets()
{
return $this->hasMany('App\Internet');
}
}
internets.php
class Internet extends Model
{
protected $with=['vendor','coba'];
public function vendor()
{
return $this->belongTo('App\Vendor');
}
}
any one can help me?
it will be belongsTo
return $this->belongsTo('App\Vendor');
Try something like this
In your vendor
class Vendor extends Model
{
protected $table = 'Vendor Table Name';
protected $primarykey = 'Your Vendor Table primary Key';
public function internets()
{
return $this->hasMany(App\Internet::class, 'relational key');
}
}
And In your internet
class Internet extends Model
{
protected $with=['vendor','coba'];
protected $table = 'Internet Table Name';
protected $primarykey = 'Internet table primary key';
public function vendor()
{
return $this->belongsTo(App\Vendor::class, 'relation key');
}
}
I hope this will solve your problem let me know if this do the trick
Related
Why set connection does not work in nested relation?
Follower.php
class Follower extends Model {
$connection = 'followers';
public function details() {
return $this->belongsTo(User::class, 'user_id');
}
}
User.php
class User extends Model {
$connection = 'users';
protected $withCount = ['notifications'];
public function notifications() {
return $this->setConnection('followers')->hasMany('App\Models\Notifications');
}
}
and the query:
Follower::query()->where('user_id', 1)->with('details')->get();
and it throws :
SQLSTATE[42S02]: Base table or view not found: 1146 Table
'users.notifications' doesn't exist (SQL: select ` ....
But when I try this it works very well
User::with('notifications')->find(1);
Update
Notification.php
class Notification extends Model
{
protected $fillable = [
'user_id',
'builder',
'notification_type',
'comment_id',
'read_at'
];
}
This is a known issue with laravel since 2018, for more information see this thread , also there is a open pull request to fix this issue and will get fixed till next release
for now you can use hoyvoy/laravel-cross-database-subqueries package
install package using this
composer require hoyvoy/laravel-cross-database-subqueries
Follower.php
use Hoyvoy\CrossDatabase\Eloquent\Model;
class Follower extends Model {
$connection = 'followers';
public function details() {
return $this->belongsTo(User::class, 'user_id');
}
}
User.php
use Hoyvoy\CrossDatabase\Eloquent\Model;
class User extends Model {
$connection = 'users';
protected $withCount = ['notifications'];
public function notifications() {
return $this->setConnection('followers')->hasMany('App\Models\Notifications');
}
}
for every model you have add the default protected $connection
We have the following class using $with:
class CargaHorasEmpleado extends Model
{
protected $table = "empleados_horas";
protected $with = ["tipoTarea", "proyecto", "empleado", "empleadoQueHizoLaCarga"];
public function tipoTarea()
{
return $this->belongsTo('App\TipoTarea', 'id_tipo_tarea', 'id')->withTrashed();
}
public function empleado()
{
return $this->belongsTo('App\Empleado', 'id_empleado', 'id')->withTrashed();
}
public function empleadoQueHizoLaCarga()
{
return $this->belongsTo('App\Empleado', 'id_empleado_cargo_hs', 'id')->withTrashed();
}
public function proyecto()
{
return $this->belongsTo('App\Proyecto', 'id_proyecto', 'id')->withTrashed();
}
}
This is the class TipoTarea
namespace App;
use Illuminate\Database\Eloquent\Model;
class TipoTarea extends Model
{
protected $table = 'tipos_tareas';
public $timestamps = false;
protected $fillable = [
'titulo', 'descripcion'
];
}
Thep page throws the error: "Call to undefined relationship [tipoTarea] on model [App\CargaHorasEmpleado]". That's the only relationship that's not working. The others are fine. What's wrong?
Well, isn't the relationship called "tipoTarea"? You wrote "tiposTarea"
The problem was that my class "TipoTarea" didn't use softdeletes. So the error was in using the "WithTrashed" method. The correct way is:
public function tipoTarea()
{
return $this->belongsTo('App\TipoTarea', 'id_tipo_tarea', 'id');
}
I have the models
class User extends Model
{
protected $connection = 'mysql';
public function areas()
{
return $this->belongsToMany(Area::class, 'user_areas');
}
}
class Area extends Model
{
protected $connection = 'oracle';
}
the user_areas table is searched on oracle, but this are on mysql.
How I can indicate the connection for the pivot table?
I found this partial solution and it has worked
class User extends Model
{
protected $connection = 'mysql'
public function areas()
{
$instance = new Area;
$instance->setConnection('mysql');
return new BelongsToMany($instance->newQuery(), $this, 'user_areas', 'user_id', 'area_id', 'areas');
}
}
I have 3 tables and these table are not according laravel
job_post Table
job_post_id
name
job_functional_area Table (has many with job_post table and belongsTo with functional_area)
job_functional_area_id
job_post_id
functional_area_id
functional_area
functional_area_id
name
now I want to fetch job_post functional area
Relation like this job_post > job_functional_area > functional_area
In cakephp we use contain in laravel i don't have an idea.
I have try Has Many Through but it will not work in this condition. Please help me
Models
class JobPost extends Model
{
protected $table = 'job_post';
protected $primaryKey = 'job_post_id';
}
class JobFunctionalArea extends Model {
protected $table = 'job_functional_area';
protected $primaryKey = 'job_functional_area_id';
}
class FunctionalArea extends Model {
protected $table = 'functional_area';
protected $primaryKey = 'functional_area_id';
}
Your models should look something like this.
class JobPost extends Model {
protected $table = 'job_post';
protected $primaryKey = 'job_post_id';
public function jobFunctionalAreas() {
return $this->belongsToMany('App\JobFunctionalArea');
}
}
class JobFunctionalArea extends Model {
protected $table = 'job_functional_area';
protected $primaryKey = 'job_functional_area_id';
public function jobPosts() {
return $this->hasMany('App\JobPost');
}
public function functionalArea() {
return $this->belongsTo('App\FunctionalArea');
}
}
class FunctionalArea extends Model {
protected $table = 'functional_area';
protected $primaryKey = 'functional_area_id';
public function jobFunctionalAreas() {
return $this->hasMany('App\JobFunctionalArea');
}
}
Also in the relations you can add your own custom $primaryKey.
I would like to do something like this.
Location::where('city', '=', 'Chicago')->chef();
With these relationships:
class Location extends Eloquent {
protected $table = 'locations';
public function chef() {
return $this->belongsTo('Chef');
}
}
class Chef extends Eloquent {
protected $table = 'chefs';
public function location() {
return $this->hasMany('Location');
}
}
This should work:
class Location extends Eloquent {
protected $table = 'locations';
public function chefs() {
return $this->belongsTo('Chef');
}
public function getAllChefsByCity($city)
{
$this->with('chefs')->where('city', $city)->get();
}
}
Then in your code:
$array = $location->getAllChefsByCity('Chicago');