laravel-ecommerce-example icon indicating copy to clipboard operation
laravel-ecommerce-example copied to clipboard

Bad Method: App\Http\Controllers\Voyager\OrdersController::getRelationships

Open Boudani-Aziz opened this issue 5 years ago • 6 comments

When I go to the admin-panel and i try to show only one order, I get an error http://localhost/admin/orders/1

=> BadMethodCallException Method App\Http\Controllers\Voyager\OrdersController::getRelationships does not exist.

can someone help?

bad method

Boudani-Aziz avatar Oct 25 '19 11:10 Boudani-Aziz

Same problem

rikvermeulen avatar Jan 18 '20 16:01 rikvermeulen

For me aswell

lmasss avatar Jan 25 '20 11:01 lmasss

This problem also me!

Usmon avatar Mar 24 '20 04:03 Usmon

This is bc Voayger has been updated since the release of these videos.

You can insert this code:

class OrdersController extends VoyagerBaseController { //*************************************** // _____ // | __
// | |__) | // | _ / // | | \
// |_| _
// // Read an item of our Data Type B(R)EAD // //****************************************

public function show(Request $request, $id)
{
    $slug = $this->getSlug($request);

    $dataType = Voyager::model('DataType')->where('slug', '=', $slug)->first();

    $isSoftDeleted = false;

    if (strlen($dataType->model_name) != 0) {
        $model = app($dataType->model_name);

        // Use withTrashed() if model uses SoftDeletes and if toggle is selected
        if ($model && in_array(SoftDeletes::class, class_uses_recursive($model))) {
            $model = $model->withTrashed();
        }
        if ($dataType->scope && $dataType->scope != '' && method_exists($model, 'scope'.ucfirst($dataType->scope))) {
            $model = $model->{$dataType->scope}();
        }
        $dataTypeContent = call_user_func([$model, 'findOrFail'], $id);
        if ($dataTypeContent->deleted_at) {
            $isSoftDeleted = true;
        }
    } else {
        // If Model doest exist, get data from table name
        $dataTypeContent = DB::table($dataType->name)->where('id', $id)->first();
    }

    // Replace relationships' keys for labels and create READ links if a slug is provided.
    $dataTypeContent = $this->resolveRelations($dataTypeContent, $dataType, true);

    // If a column has a relationship associated with it, we do not want to show that field
    $this->removeRelationshipField($dataType, 'read');

    // Check permission
    $this->authorize('read', $dataTypeContent);

    // Check if BREAD is Translatable
    $isModelTranslatable = is_bread_translatable($dataTypeContent);

    // Eagerload Relations
    $this->eagerLoadRelations($dataTypeContent, $dataType, 'read', $isModelTranslatable);

    $view = 'voyager::bread.read';

    if (view()->exists("voyager::$slug.read")) {
        $view = "voyager::$slug.read";
    }

    return Voyager::view($view, compact('dataType', 'dataTypeContent', 'isModelTranslatable', 'isSoftDeleted'));
}

}

And it will work

rossi99 avatar Jul 12 '20 13:07 rossi99

I get this error: Method App\Http\Controllers\Voyager\OrdersController::eagerLoadRelations does not exist.

rpires-dev avatar Nov 12 '20 14:11 rpires-dev

This is bc Voayger has been updated since the release of these videos.

You can insert this code:

class OrdersController extends VoyagerBaseController { //*************************************** // _____ // | __ // | |__) | // | _ / // | |
// |_| _ // // Read an item of our Data Type B(R)EAD // //****************************************

public function show(Request $request, $id)
{
    $slug = $this->getSlug($request);

    $dataType = Voyager::model('DataType')->where('slug', '=', $slug)->first();

    $isSoftDeleted = false;

    if (strlen($dataType->model_name) != 0) {
        $model = app($dataType->model_name);

        // Use withTrashed() if model uses SoftDeletes and if toggle is selected
        if ($model && in_array(SoftDeletes::class, class_uses_recursive($model))) {
            $model = $model->withTrashed();
        }
        if ($dataType->scope && $dataType->scope != '' && method_exists($model, 'scope'.ucfirst($dataType->scope))) {
            $model = $model->{$dataType->scope}();
        }
        $dataTypeContent = call_user_func([$model, 'findOrFail'], $id);
        if ($dataTypeContent->deleted_at) {
            $isSoftDeleted = true;
        }
    } else {
        // If Model doest exist, get data from table name
        $dataTypeContent = DB::table($dataType->name)->where('id', $id)->first();
    }

    // Replace relationships' keys for labels and create READ links if a slug is provided.
    $dataTypeContent = $this->resolveRelations($dataTypeContent, $dataType, true);

    // If a column has a relationship associated with it, we do not want to show that field
    $this->removeRelationshipField($dataType, 'read');

    // Check permission
    $this->authorize('read', $dataTypeContent);

    // Check if BREAD is Translatable
    $isModelTranslatable = is_bread_translatable($dataTypeContent);

    // Eagerload Relations
    $this->eagerLoadRelations($dataTypeContent, $dataType, 'read', $isModelTranslatable);

    $view = 'voyager::bread.read';

    if (view()->exists("voyager::$slug.read")) {
        $view = "voyager::$slug.read";
    }

    return Voyager::view($view, compact('dataType', 'dataTypeContent', 'isModelTranslatable', 'isSoftDeleted'));
}

}

And it will work

Added these lines of code:

$order = Order::find($id);
$products = $order->products;

return Voyager::view($view, compact('dataType', 'dataTypeContent', 'isModelTranslatable', 'products'));
public function show(Request $request, $id) {
    $slug = $this->getSlug($request);

    $dataType = Voyager::model('DataType')->where('slug', '=', $slug)->first();

    $isSoftDeleted = false;

    if (strlen($dataType->model_name) != 0) {
        $model = app($dataType->model_name);

        // Use withTrashed() if model uses SoftDeletes and if toggle is selected
        if ($model && in_array(SoftDeletes::class, class_uses_recursive($model))) {
            $model = $model->withTrashed();
        }
        if ($dataType->scope && $dataType->scope != '' && method_exists($model, 'scope'.ucfirst($dataType->scope))) {
            $model = $model->{$dataType->scope}();
        }
        $dataTypeContent = call_user_func([$model, 'findOrFail'], $id);
        if ($dataTypeContent->deleted_at) {
            $isSoftDeleted = true;
        }
    } else {
        // If Model doest exist, get data from table name
        $dataTypeContent = DB::table($dataType->name)->where('id', $id)->first();
    }

    // Replace relationships' keys for labels and create READ links if a slug is provided.
    $dataTypeContent = $this->resolveRelations($dataTypeContent, $dataType, true);

    // If a column has a relationship associated with it, we do not want to show that field
    $this->removeRelationshipField($dataType, 'read');

    // Check permission
    $this->authorize('read', $dataTypeContent);

    // Check if BREAD is Translatable
    $isModelTranslatable = is_bread_translatable($dataTypeContent);

    // Eagerload Relations
    $this->eagerLoadRelations($dataTypeContent, $dataType, 'read', $isModelTranslatable);

    $view = 'voyager::bread.read';

    if (view()->exists("voyager::$slug.read")) {
        $view = "voyager::$slug.read";
    }
        
    // updated
    $order = Order::find($id);
    $products = $order->products;

    return Voyager::view($view, compact('dataType', 'dataTypeContent', 'isModelTranslatable', 'products'));
}

whoami15 avatar Dec 18 '20 18:12 whoami15