Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

laravel yajra datatable equivalent relation with multiple database search not working #3205

Open
manishjesani912 opened this issue Dec 16, 2024 · 2 comments

Comments

@manishjesani912
Copy link

manishjesani912 commented Dec 16, 2024

Manager.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Manager extends Model
{
    protected $connection = 'moodlemanager';
    protected $table = 'managers';
    protected $fillable = ['moodle_id'];

    public function moodleUser()
    {
        return $this->hasOne('App\MoodleUser', 'id', 'moodle_id');
    }
}

MoodleUser.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class MoodleUser extends Model
{
    protected $connection = 'moodle';
    protected $table = 'mdl_user';
    protected $fillable = ['username', 'firstname', 'lastname'];

    public function Manager()
    {
        return $this->hasOne('App\Manager', 'id', 'moodle_id');
    }
}

DashboardController

<?php

namespace App\Http\Controllers\Dashboard;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use App\Manager;
use App\MoodleUser;

class DashboardController extends Controller
{

   //Datatable ajax response function
    public function index(Request $request)
    {
        $data = Manager ::select'*')
            ->with(['MoodleUser ']);

         return Datatables::of($data)
                   ->editColumn('MoodleUser ', function ($data) {
                      $actions = $data->MoodleUser->userName;
                      return $actions;
                  })
                 ->make(true);

    }
}

I have two models with different database connection. I load datatable with relation it's working fine with loading but when search it's not working got error SQLSTATE[42S02]: Base table or view not found: 1146 Table

Please give me any suggestion.

Thanks.

  • Operating System
  • PHP Version 8.1
  • Laravel Version 9
  • Laravel-Datatables Version 10
@razorext2
Copy link

check the variable name in

MoodleUser.php

it must be $connection, not $conection

@yajra
Copy link
Owner

yajra commented Jan 8, 2025

Searching on two different connections is fully not supported especially if they point to different databases.

However, if the 2 databases have read access between each other, you can use a query builder.

For example in Oracle, you can query 2 different DB/Schema and table by including the schema name in the SQL.

select leads.id, leads.name, users.name user
from
  db1.users,
  db2.leads
where
  leads.user_id = users.id

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants