troelskn / laravel-fillable-relations

Provides HasFillableRelations trait to Eloquent models
64 stars 23 forks source link

No query results for model #25

Open ahoiroman opened 4 years ago

ahoiroman commented 4 years ago

Hello everybody,

I am trying to implement this package. Unfortunately I am getting this error:

No query results for model [App\PositionProductCategory]

once I am running

   new PositionProduct($position['product']);

This is the trace:

array:43 [
  0 => array:5 [
    "file" => "/home/vagrant/code/vendor/troelskn/laravel-fillable-relations/src/Eloquent/Concerns/HasFillableRelations.php"
    "line" => 113
    "function" => "firstOrFail"
    "class" => "Illuminate\Database\Eloquent\Builder"
    "type" => "->"
  ]
  1 => array:5 [
    "file" => "/home/vagrant/code/vendor/troelskn/laravel-fillable-relations/src/Eloquent/Concerns/HasFillableRelations.php"
    "line" => 78
    "function" => "fillBelongsToRelation"
    "class" => "App\PositionProduct"
    "type" => "->"
  ]
  2 => array:5 [
    "file" => "/home/vagrant/code/vendor/troelskn/laravel-fillable-relations/src/Eloquent/Concerns/HasFillableRelations.php"
    "line" => 88
    "function" => "fillRelations"
    "class" => "App\PositionProduct"
    "type" => "->"
  ]
  3 => array:5 [
    "file" => "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php"
    "line" => 173
    "function" => "fill"
    "class" => "App\PositionProduct"
    "type" => "->"
  ]
  4 => array:5 [
    "file" => "/home/vagrant/code/app/Http/Controllers/OfferApiController.php"
    "line" => 311
    "function" => "__construct"
    "class" => "Illuminate\Database\Eloquent\Model"
    "type" => "->"
  ]
  5 => array:5 [
    "file" => "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Support/Traits/EnumeratesValues.php"
    "line" => 202
    "function" => "App\Http\Controllers\{closure}"
    "class" => "App\Http\Controllers\OfferApiController"
    "type" => "->"
  ]
  6 => array:5 [
    "file" => "/home/vagrant/code/app/Http/Controllers/OfferApiController.php"
    "line" => 359
    "function" => "each"
    "class" => "Illuminate\Support\Collection"
    "type" => "->"
  ]
  7 => array:3 [
    "function" => "update"
    "class" => "App\Http\Controllers\OfferApiController"
    "type" => "->"
  ]
  8 => array:3 [
    "file" => "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Routing/Controller.php"
    "line" => 54
    "function" => "call_user_func_array"
  ]
  9 => array:5 [
    "file" => "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php"
    "line" => 45
    "function" => "callAction"
    "class" => "Illuminate\Routing\Controller"
    "type" => "->"
  ]
  10 => array:5 [
    "file" => "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Routing/Route.php"
    "line" => 239
    "function" => "dispatch"
    "class" => "Illuminate\Routing\ControllerDispatcher"
    "type" => "->"
  ]
  11 => array:5 [
    "file" => "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Routing/Route.php"
    "line" => 196
    "function" => "runController"
    "class" => "Illuminate\Routing\Route"
    "type" => "->"
  ]
  12 => array:5 [
    "file" => "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Routing/Router.php"
    "line" => 685
    "function" => "run"
    "class" => "Illuminate\Routing\Route"
    "type" => "->"
  ]
  13 => array:5 [
    "file" => "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php"
    "line" => 128
    "function" => "Illuminate\Routing\{closure}"
    "class" => "Illuminate\Routing\Router"
    "type" => "->"
  ]
  14 => array:5 [
    "file" => "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php"
    "line" => 41
    "function" => "Illuminate\Pipeline\{closure}"
    "class" => "Illuminate\Pipeline\Pipeline"
    "type" => "->"
  ]
  15 => array:5 [
    "file" => "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php"
    "line" => 167
    "function" => "handle"
    "class" => "Illuminate\Routing\Middleware\SubstituteBindings"
    "type" => "->"
  ]
  16 => array:5 [
    "file" => "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php"
    "line" => 44
    "function" => "Illuminate\Pipeline\{closure}"
    "class" => "Illuminate\Pipeline\Pipeline"
    "type" => "->"
  ]
  17 => array:5 [
    "file" => "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php"
    "line" => 167
    "function" => "handle"
    "class" => "Illuminate\Auth\Middleware\Authenticate"
    "type" => "->"
  ]
  18 => array:5 [
    "file" => "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php"
    "line" => 59
    "function" => "Illuminate\Pipeline\{closure}"
    "class" => "Illuminate\Pipeline\Pipeline"
    "type" => "->"
  ]
  19 => array:5 [
    "file" => "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php"
    "line" => 167
    "function" => "handle"
    "class" => "Illuminate\Routing\Middleware\ThrottleRequests"
    "type" => "->"
  ]
  20 => array:5 [
    "file" => "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php"
    "line" => 103
    "function" => "Illuminate\Pipeline\{closure}"
    "class" => "Illuminate\Pipeline\Pipeline"
    "type" => "->"
  ]
  21 => array:5 [
    "file" => "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Routing/Router.php"
    "line" => 687
    "function" => "then"
    "class" => "Illuminate\Pipeline\Pipeline"
    "type" => "->"
  ]
  22 => array:5 [
    "file" => "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Routing/Router.php"
    "line" => 662
    "function" => "runRouteWithinStack"
    "class" => "Illuminate\Routing\Router"
    "type" => "->"
  ]
  23 => array:5 [
    "file" => "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Routing/Router.php"
    "line" => 628
    "function" => "runRoute"
    "class" => "Illuminate\Routing\Router"
    "type" => "->"
  ]
  24 => array:5 [
    "file" => "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Routing/Router.php"
    "line" => 617
    "function" => "dispatchToRoute"
    "class" => "Illuminate\Routing\Router"
    "type" => "->"
  ]
  25 => array:5 [
    "file" => "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php"
    "line" => 165
    "function" => "dispatch"
    "class" => "Illuminate\Routing\Router"
    "type" => "->"
  ]
  26 => array:5 [
    "file" => "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php"
    "line" => 128
    "function" => "Illuminate\Foundation\Http\{closure}"
    "class" => "Illuminate\Foundation\Http\Kernel"
    "type" => "->"
  ]
  27 => array:5 [
    "file" => "/home/vagrant/code/vendor/fideloper/proxy/src/TrustProxies.php"
    "line" => 57
    "function" => "Illuminate\Pipeline\{closure}"
    "class" => "Illuminate\Pipeline\Pipeline"
    "type" => "->"
  ]
  28 => array:5 [
    "file" => "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php"
    "line" => 167
    "function" => "handle"
    "class" => "Fideloper\Proxy\TrustProxies"
    "type" => "->"
  ]
  29 => array:5 [
    "file" => "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php"
    "line" => 21
    "function" => "Illuminate\Pipeline\{closure}"
    "class" => "Illuminate\Pipeline\Pipeline"
    "type" => "->"
  ]
  30 => array:5 [
    "file" => "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php"
    "line" => 167
    "function" => "handle"
    "class" => "Illuminate\Foundation\Http\Middleware\TransformsRequest"
    "type" => "->"
  ]
  31 => array:5 [
    "file" => "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php"
    "line" => 21
    "function" => "Illuminate\Pipeline\{closure}"
    "class" => "Illuminate\Pipeline\Pipeline"
    "type" => "->"
  ]
  32 => array:5 [
    "file" => "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php"
    "line" => 167
    "function" => "handle"
    "class" => "Illuminate\Foundation\Http\Middleware\TransformsRequest"
    "type" => "->"
  ]
  33 => array:5 [
    "file" => "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php"
    "line" => 27
    "function" => "Illuminate\Pipeline\{closure}"
    "class" => "Illuminate\Pipeline\Pipeline"
    "type" => "->"
  ]
  34 => array:5 [
    "file" => "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php"
    "line" => 167
    "function" => "handle"
    "class" => "Illuminate\Foundation\Http\Middleware\ValidatePostSize"
    "type" => "->"
  ]
  35 => array:5 [
    "file" => "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php"
    "line" => 63
    "function" => "Illuminate\Pipeline\{closure}"
    "class" => "Illuminate\Pipeline\Pipeline"
    "type" => "->"
  ]
  36 => array:5 [
    "file" => "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php"
    "line" => 167
    "function" => "handle"
    "class" => "Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode"
    "type" => "->"
  ]
  37 => array:5 [
    "file" => "/home/vagrant/code/app/Http/Middleware/SetLocale.php"
    "line" => 20
    "function" => "Illuminate\Pipeline\{closure}"
    "class" => "Illuminate\Pipeline\Pipeline"
    "type" => "->"
  ]
  38 => array:5 [
    "file" => "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php"
    "line" => 167
    "function" => "handle"
    "class" => "App\Http\Middleware\SetLocale"
    "type" => "->"
  ]
  39 => array:5 [
    "file" => "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php"
    "line" => 103
    "function" => "Illuminate\Pipeline\{closure}"
    "class" => "Illuminate\Pipeline\Pipeline"
    "type" => "->"
  ]
  40 => array:5 [
    "file" => "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php"
    "line" => 140
    "function" => "then"
    "class" => "Illuminate\Pipeline\Pipeline"
    "type" => "->"
  ]
  41 => array:5 [
    "file" => "/home/vagrant/code/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php"
    "line" => 109
    "function" => "sendRequestThroughRouter"
    "class" => "Illuminate\Foundation\Http\Kernel"
    "type" => "->"
  ]
  42 => array:5 [
    "file" => "/home/vagrant/code/public/index.php"
    "line" => 55
    "function" => "handle"
    "class" => "Illuminate\Foundation\Http\Kernel"
    "type" => "->"
  ]
]

This is the relation in the model:

 public function category()
    {
        return $this->belongsTo(PositionProductCategory::class);
    }

And of course PositionProductCategory does exist.

Maybe the problem is, that my model do not extend Model but Product and ProductCategory?


class PositionProduct extends Product
{
    use ProductTrait, HasFillableRelations;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable
        = [
              ...
        ];

    protected $fillable_relations = ['category'];
ahoiroman commented 4 years ago

I checked out the line in the trace:

 if (!$attributes instanceof Model) {
            $entity = $relation->getRelated()
                ->where($attributes)->firstOrFail();
        }

In this case !$attributes instanceof Model returns false and this is the reason why it fails.

troelskn commented 4 years ago

Can you post the full example (at least the model classes and the db-migrations)? Also, what version on Laravel are you using?

ahoiroman commented 4 years ago

Hi.

thanks for your response. If you need something more, please let me know.

PositionProduct.php

<?php

namespace App;

use App\Traits\ProductTrait;
use LaravelFillableRelations\Eloquent\Concerns\HasFillableRelations;

/**
 * Class PositionProduct
 *
 * @package App
 */
class PositionProduct extends Product
{
    use ProductTrait, HasFillableRelations;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable
        = [
            'title',
            'description',
            'price',
            'setup',
            'vat',
            'recurring',
            'contractperiods',
            'accountingperiods',
            'custom',
        ];

    protected $fillable_relations = ['category'];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts
        = [
            'contractperiods'   => 'object',
            'accountingperiods' => 'object',
            'vat'               => 'integer',
            'price'             => 'float',
            'setup'             => 'float',
            'recurring'         => 'boolean',
            'custom'            => 'boolean',
            'description'       => 'string',
        ];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function category()
    {
        return $this->belongsTo(PositionProductCategory::class);
    }

}
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePositionProductsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('position_products', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->boolean('custom');
            $table->bigInteger('category_id')->nullable();
            $table->string('title');
            $table->longText('description')->nullable();
            $table->float('price');
            $table->float('setup');
            $table->float('vat');
            $table->boolean('recurring');
            $table->json('accountingperiods')->nullable();
            $table->json('contractperiods')->nullable();
            $table->string('slug', 150)->nullable();
            $table->softDeletes();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('position_products');
    }
}