我有表格艺术家,歌曲和artist_song,它是数据透视表
我想要上传歌曲并存储一个以属于该歌曲的艺术家命名的新目录。但是我得到了这个错误
Undefined property: Illuminate\Database\Eloquent\Relations\BelongsToMany::$name
这是我在SongController上的商店方法。
public function store(Request $request)
{
//
$this->validate($request, [
'title' => 'required',
'mp3' => 'required',
'artists' => 'required'
]);
$song = new Song;
$song->title = $request->title;
$song->lyrics = $request->lyrics;
$song->youtube_id = $request->youtube_id;
$song->album_id = $request->album_id;
$song->category_id = $request->category_id;
if($request->hasFile('mp3')){
$mp3 = $request->file('mp3');
$trackname = $mp3->getClientOriginalName();
$path = public_path('/uploads/songs/'.$song->artists()->name);
if (!file_exists($path)){
mkdir($path, 0777, true);
}
$mp3->move($path, $trackname);
$song->mp3 = $trackname;
}
$song->save();
$song->artists()->sync($request->artists, false);
return redirect()->route('song.index')->with('success', 'New song successfully upploaded');
}
我无法获得该艺术家的名称属性..
所以我的代码有什么问题,还有一件事可以帮助我,这是正确的方法来做这样的关系表。我引用了一个关于标记的教程,我做了同样的