Laravel有Undefined Constant' App \ App \ projects

时间:2018-03-08 09:24:23

标签: laravel laravel-5 eloquent laravel-5.6

我创建了一个名为company and projects的模型,我需要在公司下面展示项目。该公司显示正常,但当我向模型添加项目关系时,它显示错误。我是Laravel的新手。

  

未定义的常量' App \ App \ projects' (视图:   C:\ XAMPP \ htdocs中\测试驱动\资源\意见\企业\ show.blade.php)   C:\ XAMPP \ htdocs中\测试驱动\应用\ company.php

模型

use Illuminate\Database\Eloquent\Model;

class company extends Model
{
    protected $fillable = [
        'name',
        'description',
        'user_id'
    ];

    public function user()
    {
        return $this->belongsTo(App\User);
    }

    public function projects()
    {
        return $this->hasMany(App\projects);
    }
}

show.blade.php

@extends('layouts.app')
@section('content')
    <div class="container">
        <!-- Jumbotron -->
        <div class="jumbotron">
            <h1>{{$company->name}}</h1>
            <p class="lead">{{$company->description}}</p>
        </div>

        <!-- Example row of columns -->
        <div class="row">
            @foreach($company->projects as $projects)
                <div class="col-lg-4">
                    <h2>{{$projects->name}}</h2>
                    <p class="text-danger">{{$projects->description}} </p>
                    <p>
                        <a class="btn btn-primary" href="/projects/{{$projects->id}}" role="button">View Project</a>
                    </p>

                </div>
            @endforeach
        </div>
    </div>
@endsection

5 个答案:

答案 0 :(得分:1)

你有错字!名称空间应该是字符串:

app.js

答案 1 :(得分:0)

您的模型中缺少 class company extends Model { // protected $fillable=[ 'name', 'description', 'user_id' ]; public function user() { return $this->belongsTo(App\User::class); } public function projects() { return $this->hasMany(App\projects::class); } } 个关键字

import re

query = ['whole regular milk', 'gatorade is better',
         'whole almond chocolate milk', 'chocolate milk', 'wholes']

types = [{'type': t, 'regex': re.compile(r'\b{}\b'.format(t))}
         for t in ['whole', 'regular', 'chocolate']]

df = pd.DataFrame({'Query': query})

def check(q):
    return ' | '.join(type_info['type'] for type_info in types
                      if type_info['regex'].findall(q))

df['Type'] = df['Query'].apply(check)

print(df)

#                           Query                Type
#  0           whole regular milk     whole | regular
#  1           gatorade is better                   
#  2  whole almond chocolate milk   whole | chocolate
#  3               chocolate milk           chocolate
#  4                       wholes                            

希望这有帮助

答案 2 :(得分:0)

在问题中传递模型的类或它的命名空间

public function user()
{
  return $this->belongsTo(App\User::class); //or $this->belongsTo('App\User');
}

public function projects()
{
  return $this->hasMany(App\Project::class);
}

答案 3 :(得分:0)

除了使用引号引用上面提到的模型之外。 可能还需要定义类的命名空间。

旁注:它也是使用PascalCase进行类名的Laravel命名约定(即公司,WetCamel,FooBarClass)

namespace App\Company;

use Illuminate\Database\Eloquent\Model;


class Company extends Model
{
    protected $fillable = [
        'name',
        'description',
        'user_id'

    ];

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

    public function projects()
    {
        return $this->hasMany('App\Projects');
    }

}

答案 4 :(得分:0)

使用''这个来调用模型。

将语法更正为:

public function projects()
{
    return $this->hasMany('App\projects');
}