我试图通过belongsTo
和hasMany
方法将我的图片模型与用户模型联系起来。
Picture.php
模型文件中编写的代码:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Picture extends Model{
public function user()
{
return $this->belongsTo('App\User');
}
}
<小时/> 这是我的
User.php
模型文件的代码:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
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',
];
public function pictures()
{
return $this->hasMany('App\Picture');
}
}
<小时/> 我想在我
manage
文件中展示的RouteController
视图中显示用户自己上传的图片:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Picture;
use App\User;
use DB;
class RouteController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth', ['except' => ['welcome', 'auth.login', 'auth.register']]);
}
public function welcome()
{
$pictures = Picture::all();
return view('welcome')->with('pictures', $pictures);
}
public function login()
{
return view('auth.login');
}
public function register()
{
return view('auth.register');
}
public function manage()
{
$user_id = auth()->user('id');
$user = User::find($user_id);
$pictures = Picture::all();
return view('pictures.manage')->with('pictures', $pictures->user);
}
}
<小时/> 在
manage.blade.php
我必须输出图片。为此,我写了这段代码:
@foreach ($pictures as $picture)
<div class="well">
<h3>{{ $picture->hash }}</h3>
</div>
@endforeach
<小时/> 在经历了这场喧嚣之后,它给了我一个错误:
Property [user] does not exist on this collection instance.
manage
视图中输出。如果要查看项目中的所有文件,请访问此GitHub commit。
答案 0 :(得分:1)
这是因为您正在呼叫$pictures->user
。 $pictures
拥有此语句中所有图片的collection(增强数组):$pictures = Picture::all();
。集合不是对象,也没有属性。
此外,您不会将图片仅限于用户的任何位置。您正在调用Picture::all()
,它将返回所有用户的所有图片。
假设您的关系按预期工作,您可以使用$user->pictures
方法获取当前登录用户的图片。这就是你所需要的:
$pictures = auth()->user()->pictures;
return view('pictures.manage')->with('pictures', $pictures);
同时从->user
移除->with('pictures', $pictures)
部分。
答案 1 :(得分:1)
我认为旅行问题是manage
方法
尝试这样做
public function manage()
{
$user = auth()->user();
$pictures = $user->pictures;
return view('pictures.manage')->with(['pictures' => $pictures]);
}
答案 2 :(得分:1)
在图片迁移中,您需要添加user_id
外键才能在模型中拥有用户和图片之间的关系。像这样:
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePicturesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('pictures', function (Blueprint $table) {
$table->increments('id');
// $table->string('storage_url');
// $table->string('preview_url');
$table->string('hash');
$table->unsignedInteger('user_id');// or $table->integer('user_id')->unsigned();
$table->timestamps();
});
Schema::table('pictures', function (Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users');
// also you can put some triggered actions ->onUpdate('cascade')->onDelete('cascade')
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('pictures', function (Blueprint $table) {
$table->dropForeign(['user_id']);
});
Schema::dropIfExists('pictures');
}
}
请确保users.id
和pictures.user_id
必须是相同的类型字段。
检查docs中的各种选项。 然后,其他人在答案中说了什么。
答案 3 :(得分:0)
请添加用户与图片之间的关系。
进行新迁移以在图片模型中添加外键。请参考以下链接。 https://laravel.com/docs/5.6/migrations
还在可填写部分的图片模型中添加外键列名称。
然后更改你的管理方法代码,如...
$ user = User :: with([&#39; pictures&#39;]) - &gt; where(&#39; id&#39;,Auth :: id()) - &gt; first();
返回视图(&#39; pictures.manage&#39;) - &gt; with([&#39; pictures&#39; =&gt; $ user [&#39; pictures&#39;]);