我有项目hasMany用户和用户belongsTo一个项目.
我想计算一个项目的用户总数,所以我需要链接它们.
这样我就收到了Call to undefined method Illuminate\Database\Query\Builder::user()错误.
我究竟做错了什么?
控制器:
class ProjectController extends Controller
{
private $project;
public function __construct(Project $project){
$this->project = $project;
// $this->project = $project
// ->with('user');
}
public function index(Project $project)
{
$projects = $project->with('user')->get();
$currenttime = Carbon::now();
// return view('project.index', array('projects' => $projects, 'currenttime' => $currenttime));
return view('user.index', compact('projects'));
}
Run Code Online (Sandbox Code Playgroud)
}
型号user:
public function project(){
return $this->belongsTo('App\Project', 'project_id','id');
}
Run Code Online (Sandbox Code Playgroud)
型号project:
public function users() {
return $this->hasMany('App\User', …Run Code Online (Sandbox Code Playgroud) 我有3个表:company< - > users< - > invoice.
一个公司的hasMany用户.
用户belongsTo是公司和用户hasMany发票.
belongsTo用户发票.
现在我有一张发票,上面有关于用户(客户)的信息,我希望向用户提供有关公司的信息,所以我做了一个:
发票hasManyThrough用户,公司(通过用户获取公司)
现在它不能正常工作.
楷模:
class Company extends Eloquent {
protected $table = 'companies';
public function users()
{
return $this->hasMany('App\User', 'id');
}
public function invoices()
{
return $this->hasManyThrough('App\Company', 'App\User');
}
}
class User extends Model {
protected $table = 'users';
public function usertype()
{
return $this->belongsTo('App\UserType','usertype_id','id');
}
public function company()
{
return $this->belongsTo('App\Company','company_id','id');
}
public function invoice()
{ …Run Code Online (Sandbox Code Playgroud) 我有一个项目列表,我可以点击它,它(需要)显示项目名称和项目任务列表(ToDo应用程序)
但是当我点击某个项目时,我收到了这个错误.
("H2"项目名称将显示)
h2>{{{ $project->name }}}</h2>
@if ( !$project->tasks->count())
There are no tasks for this project.
@else
<ul>
@foreach( $project->tasks as $task)
<li><a href="{{ route('projects.tasks.show', [$project->slug, $task->slug]) }}">{{ $task->name }}</a></li>
@endforeach
</ul>
@endif
Run Code Online (Sandbox Code Playgroud) 假设我正在从API使用AJAX调用中获取数据.(例如,himoviedb)
现在API需要一个API密钥才能从中获取数据,但我不想API在滥用此密钥时向公众显示此密钥.
是否有解决方案来确保此密钥是安全/隐藏的,而不是通过PHP函数进行路由?
$.ajax({
type: "POST",
url: "http://api.themoviedb.org/3/search/movie?query=" + data + "&api_key=xxxxxxxxxxxxxxxxxxxxxx",
dataType: 'jsonp',
data: {
data: data,
},
success: function (resData) {
// whatever functions here
}
})
Run Code Online (Sandbox Code Playgroud)