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

hasMany relation not working for Show Method #3107

Closed
RNKushwaha opened this issue Mar 19, 2019 · 4 comments
Closed

hasMany relation not working for Show Method #3107

RNKushwaha opened this issue Mar 19, 2019 · 4 comments
Labels

Comments

@RNKushwaha
Copy link

  • Laravel Version: 5.8.4
  • PHP Version: 7.2.15
  • Laravel-admin: 1.6.10

Description:

I am following http://laravel-admin.org/docs/#/en/model-show for showing model hasMany records but its giving error-

QueryException In Connection.php line 664 :
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'from deposits_maturities where deposits_maturities.deposit_id = ? and dep' at line 1 (SQL: select from deposits_maturitieswheredeposits_maturities.deposit_id= 34 anddeposits_maturities.deposit_idis not null anddeposits_maturities.deleted_at` is null limit 20 offset 0)

Steps To Reproduce:

app/Admin/Controllers/DepositsController.php

    protected function detail($deposits)
    {
        $show = new Show(Deposits::findOrFail($deposits));

        $show->id('ID');
        $show->user_id('User')->as(function ($user_id) {
            return Users::where('id', $user_id)->first()->name ?? null;
        });
        $show->plan_id('Plan')->as(function ($plan_id) {
            return Plans::where('id', $plan_id)->first()->plan_name ?? null;
        });
        $show->type('Plan Type')->as(function ($type) {
            return \App\Helpers\PlanTypes::getType($type);
        });
        $show->duration();
        $show->invest_period_type()->as(function ($invest_period_type) {
            return \App\Helpers\PlanTypes::investmentPeriod($invest_period_type);
        });
        $show->amount();
        $show->profit('Profit %')->label();
        $show->status('Status')->as(function ($status) {
            return AdminController::getStatusDeposit($status, false);
        });
        $show->divider();
        $show->created_at('Created')->as(function ($created_at) {
            return AdminController::formatDateCreated($created_at);
        });
        $show->created_at()->as(function ($created_at) {
            return AdminController::formatDateTimeCreated($created_at);
        });
        $show->updated_at()->as(function ($updated_at) {
            return AdminController::formatDateTimeCreated($updated_at);
        });

        $show->maturities(function ($maturities) {
            $maturities->resource('/admin/maturities');

            $maturities->amount();
            $maturities->status();
            $maturities->created_at();
            $maturities->updated_at();
        });

        $show->panel()
            ->tools(function ($tools) {
                $tools->disableEdit();
            });
        
        return $show;
    }

app/Admin/Models/Deposits.php

<?php

namespace App\Admin\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Deposits extends Model
{
    use SoftDeletes;

    public function plan()
    {
        return $this->belongsTo('App\Admin\Models\Plans', 'plan_id', 'id');
    }

    public function user()
    {
        return $this->belongsTo('App\Admin\Models\Users');
    }

    public function payment()
    {
        return $this->belongsTo('App\Admin\Models\PaymentMethods', 'payment_type', 'id');
    } 

    public function maturities()
    {
        return $this->hasMany('App\Admin\Models\DepositsMaturities', 'deposit_id', 'id');
    }

}

app/Admin/Models/DepositsMaturities.php

<?php

namespace App\Admin\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class DepositsMaturities extends Model
{
    use SoftDeletes;

    public function deposits()
    {
        return $this->belongsTo('App\Admin\Models\Deposits', 'id', 'deposit_id');
    }
}
@ZealousLyon
Copy link

ZealousLyon commented Mar 22, 2019

Fixed in #3158 68dbf8a
Thanks for @kingofzihua

@reals79
Copy link

reals79 commented Mar 23, 2019

+1

@stale
Copy link

stale bot commented Jun 1, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jun 1, 2019
@stale stale bot closed this as completed Jun 8, 2019
@vonsogt
Copy link
Contributor

vonsogt commented Jul 31, 2019

Okay this ISSUE fix my problem

Thanks alot

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

No branches or pull requests

4 participants