2
中为用户返回空值我收到的用户空值为http://localhost:8000/messages
。我正在尝试在消息模型中获取用户表值。它也在控制台中给予null值.......................................... .................................................. 我路线代码是:从
Route::get('/messages', function() {
return App\Message::with('user')-> get();
})-> middleware('auth');
消息型号代码:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Message extends Model
{
protected $fillable = ['message'];
public function user()
{
return $this -> belongsTo(User::class);
}
}
用户型号代码:
<?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 messages()
{
return $this-> hasMany(Message::class);
}
}
工作正常,谢谢 –