0
votes

I have two Models,Product&Pdetail, all thing work good ,hasMany and belongsTo work too and I can check and create userdetail record via relations in tinker but when I put my models in app\Models folder I can't get relationships between my models,I changed my name space in all {models}.php to App\Models too.I checked in tinker too,can't get hasMany relations in tinker or controllers, for example in tinker

$product=App\Product::find (3);
$pdetail= new App\Pdetail;
$pdetail->body='some text about it';
$product->pdetail(s)->save($pdetail);    //(s)for hasOne or hasMany based on models

This works good But after changing location :

$product=App\models\Product::find (3);
$pdetail= new App\Models\Pdetail;
$pdetail->body='Some text about it';
$product->pdetail(s)->save($pdetail);

Returned 'bad method call', I changed my name space in model files too(namespace App\Mpdels) Can anyone help? my product.php model:

    namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    //
    public function pdetail ()
    {
        return $this->hasOne('App\Models\Pdetail');
    }

and Pdetail.php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Pdetail extends Model
{
    //
    public function product ()
    {
        return $this->belongsTO('App\Models\Product');
    }
}

in tinker

$product=App\Models\Product::first(); => App\Models\Product {#768 id: 1, name: "HTC 10", price: "11111", avatar: "index1.jpg", pcount: "7", cat_id: 2, created_at: "2018-01-19 20:14:54", updated_at: "2018-01-19 20:14:54", } $pdetail=new App\Models\Pdetail; => App\Models\Pdetail {#755} $product->pdetail(); BadMethodCallException with message 'Call to undefined method Illuminate\Database\Query\Builder::pdetail()'

1
show us your model??Sohel0415
Have you tried clearing the cache etc?Andy Holmes
In your second example you are using & instead of $.Marwelln
@Marwelln with those eagle eyes, far too early for me hahahaAndy Holmes
Bad method call can be about your routing... except what @Marwelln says about using of & instand of $ ...Goms

1 Answers

0
votes

thanks to @Sohel0415 i tied this commands 3-4 times and problem solved, don't know why in first use didn't work.

 composer dump-autoload
 cache:clear
 php artisan view:clear
 php artisan config:clear

thanks a lot