z-song / laravel-admin

Build a full-featured administrative interface in ten minutes
https://laravel-admin.org
MIT License
11.13k stars 2.81k forks source link

hasMany relation not working for Show Method #3107

Closed RNKushwaha closed 5 years ago

RNKushwaha commented 5 years ago

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 fromdeposits_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 commented 5 years ago

Fixed in #3158 68dbf8a Thanks for @kingofzihua

reals79 commented 5 years ago

+1

stale[bot] commented 5 years ago

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.

vonsogt commented 5 years ago

Okay this ISSUE fix my problem

Thanks alot