嗨,我想要检索我projects所拥有的数据库,这些数据库由auth拥有,user他也创建clients(他们有许多项目和任务)和tasks(属于项目,任务和用户).
我想检索状态表中未标记为已关闭的所有任务,我知道这个ID是2,我可以这样检索:
public function getOpenProjects() {
return \Project::with(['clients', 'tasks', 'status'])
->where('status_id', '!=', '2')
->whereUserId(Auth::user()->id)
->get();
}
Run Code Online (Sandbox Code Playgroud)
但是如何更改此选项以查询状态表中的列,即该表中的名称列?
我有一段代码想要重用。我读过这篇Laravel 清洁代码文章和另一篇Laravel 服务模式文章,其中我意识到可以通过使用服务类在应用程序的多个位置重用代码。
在本例中,我MyService在新文件夹中创建了一个新类app/Services/MyService。
namespace App\Services;
class MyService
{
public function reuse_code($param){
return void;
}
}
Run Code Online (Sandbox Code Playgroud)
当我想通过 Livewire 类组件内的构造函数调用该类时,问题就出现了,如下所示:
<?php
namespace App\Http\Livewire;
use App\Services\MyService;
use Livewire\Component;
use Livewire\WithPagination;
class LivewireTable extends Component
{
use WithPagination;
private $myClassService;
public function __construct(MyService $myService)
{
$this->myClassService = $myService;
}
public function render()
{
$foo = $this->myClassService->reuse_code($param);
return view('my.view',compact('foo'));
}
}
Run Code Online (Sandbox Code Playgroud)
显示的错误如下:
传递给 App\Http\Livewire\LivewireTable::__construct() 的参数 1 必须是 App\Services\MyService 的实例,给定字符串
(但是,如果我使用一个特质,就没有问题。但我担心我的特质会与以前的经验发生冲突)
我如何解决它?我缺少什么?
我有三个表 - 活动,行动和活动家.Campaign有许多操作,Action属于Activist和Campaign.
每个操作都有一个client_id(来自其所属广告系列的client_id),因此当客户查看活动家列表时,他们应该只看到那些对其中一个广告系列采取了操作的人.
同样,在查看个人活动家时,他们应该只看到与其广告系列相关的操作.
楷模
Campaign.php
public function actions()
{
return $this->hasMany('Action');
}
Run Code Online (Sandbox Code Playgroud)
action.php的
public function campaign()
{
return $this->belongsTo('Campaign', 'campaign_id');
}
public function activist()
{
return $this->belongsTo('Activist', 'activist_id');
}
Run Code Online (Sandbox Code Playgroud)
Activists.php
public function actions()
{
return $this->hasMany('Action');
}
Run Code Online (Sandbox Code Playgroud)
控制器
ActivistsController.php
public function index()
{
$activists = Activist::with('actions')->whereHas('actions', function($q) {
$user = Sentry::getUser();
$q->where('client_id', $user->client_id);
}))->get();
foreach ($activists as $activist)
{
$activist->total = $activist->actions()->count();
}
}
public function getActivist($id)
{
$activist = Activist::with('actions')->whereHas('actions', function($q) {
$user = Sentry::getUser();
$q->where('client_id', $user->client_id); …Run Code Online (Sandbox Code Playgroud) 我是 Laravel 的新手,我想使用 with 子句从模型中的控制器传递 $id
我的模特
class Menucategory extends Model
{
protected $fillable = ['title', 'parent_id', 'restaurant_id'];
// loads only direct children - 1 level
public function children()
{
return $this->hasMany('App\Menucategory', 'parent_id');
}
// recursive, loads all descendants
public function childrenRecursive()
{
return $this->children()->with('childrenRecursive');
}
}
Run Code Online (Sandbox Code Playgroud)
我的控制器
public function show($id)
{
$menucatagories = Menucategory::with('childrenRecursive')->where('restaurant_id',$id)->where('parent_id','0')->get();
return $menucatagories;
}
Run Code Online (Sandbox Code Playgroud)
我当前的输出是
[
{
"id": 1,
"title": "TestMenu Parant",
"parent_id": 0,
"restaurant_id": 12,
"children_recursive": [
{
"id": 2,
"title": "TestMenu SubCat1",
"parent_id": 1, …Run Code Online (Sandbox Code Playgroud)