我在关系方面遇到了很大的问题,不幸的是,我决定在数据库中使用不同名称的模型和表,因此laravel无法正确匹配它们。我已将protected $table
变量添加到FuseCollector模型中,并将表名和列名添加为用户模型中hasMany()
方法中的第二和第三属性,但仍然无法使用。我正在寻找解决方案两天,然后将头发拔出头来。请帮助!
我的数据库表:
fusecollector_projects: id (key), user_id (foreign key), notes, created_at, updated_at
users: id (key), name, email, email_verified_at, password, remember_token, created_at, updated_at
模型FuseCollector(App \ FuseCollector.php)
namespace App\FuseCollector;
use Illuminate\Database\Eloquent\Model;
class Project extends Model
{
protected $table = 'fusecollector_projects';
protected $fillable = [
'name', 'notes'
];
public function user()
{
return $this->belongsTo('App\User');
}
}
模型用户(App \ User.php)
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function fuseCollectorProjects()
{
return $this->hasMany('App\FuseCollector\Project', 'fusecollector_projects', 'user_id');
}
}
控制器项目(App \ Http \ Controllers \ FuseController \ Project.php)
<?php
namespace App\Http\Controllers\FuseCollector;
use Request;
use App\Http\Requests\CreateFCProjectRequest;
use App\FuseCollector\Project;
use Auth;
use Session;
use App\Http\Controllers\Controller;
//use App\Http\Requests;
class ProjectController extends Controller
{
public function index()
{
return view('fusecollector.project.index');
}
public function create()
{
return view('fusecollector.project.create');
}
public function store(CreateFCProjectRequest $request) // zapisywanie projektu do bazy
{
$project = new Project($request->all());
Auth::user()->fuseCollectorProjects()->save($project);
Session::flash('video_saved', 'Film został zapisany');
return redirect('fusecollector.project.index');
}
}
[当我尝试从我的简单创建表单(仅'name'和'notes'字段)使用create()方法时,我收到错误消息:
Symfony\Component\Debug\Exception\FatalThrowableError
Call to a member function fuseCollectorProjects() on null
App\Http\Controllers\FuseCollector\ProjectController::store:26
第26行是:
Auth::user()->fuseCollectorProjects()->save($project);
我知道关系存在问题,所以我尝试检查php artisan tinker中的关系:
>>> $p = new App\FuseCollector\Project
=> App\FuseCollector\Project {#3061}
>>> $p->first()
=> App\FuseCollector\Project {#3068
id: 1,
name: "Projekt",
user_id: 1,
notes: "aaa",
created_at: "2020-01-04 23:42:12",
updated_at: "2020-01-04 23:42:12",
}
>>> $u = new App\User
=> App\User {#3055}
>>> $u->first()
=> App\User {#3072
id: 1,
name: "ForgottenLord",
email: "[email protected]",
email_verified_at: null,
created_at: "2020-01-04 01:00:49",
updated_at: "2020-01-04 01:00:49",
}
>>> $p->user()
=> Illuminate\Database\Eloquent\Relations\BelongsTo {#3063}
>>> $u->fuseCollectorProjects()
=> Illuminate\Database\Eloquent\Relations\HasMany {#3070}
它表明没有关系,我不知道为什么!
请在此处[https://laravel.com/docs/6.x/eloquent-relationships#one-to-many]]参阅Laravel的官方文档>