下午好。
我试图显示分配给用户的结果,其逻辑是在管理中用户只能看到分配的内容。
我通过以下方式获得模型,其中我在scopeName函数中尝试将admins和proposal表的数据传递给控制器:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Proposal extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $table = 'proposals';
protected $fillable = [
'user_id', 'admin_id', 'prefix', 'order', 'title', 'province_id', 'municipalities', 'address', 'description', 'status', 'comments', 'closing_comments', 'institution_id', 'table_id'
];
public function users()
{
return $this->belongsTo('App\User');
}
public function userid()
{
return $this->belongsTo(User::class, 'user_id');
}
public function admins()
{
return $this->belongsTo('App\Admin');
}
public function adminId()
{
return $this->belongsTo(Admin::class, 'admin_id');
}
public function scopeName($query, $name)
{
$query->join('admins', 'proposals.admin_id', '=', 'admins.id')
->select(
'proposals.*',
'admins.name as admin_name',
'admins.last_name as admin_last_name',
'admins.area as admin_area',
'admins.job_title as admin_job_title',
'admins.email as admin_email',
'admins.phone as admin_phone',
'admins.movil as admin_movil')
->where('proposals.order','like',"%$name%")
->orWhere('proposals.title','like',"%$name%");
}
}
我的控制器:
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$data = Proposal::name($request->get('criteria'))->where('admin_id','=',Auth::id())->orderBy('id','asc')->paginate(5);
return view('administracion.propuestas.inicio', compact('data'))->with('i', ($request->input('page', 1) - 1) * 5);
}
它引发的视图错误如下:
(2/2) QueryException
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'admins.last_name' in 'field list' (SQL: select `proposals`.*, `admins`.`name` as `admin_name`, `admins`.`last_name` as `admin_last_name`, `admins`.`area` as `admin_area`, `admins`.`job_title` as `admin_job_title`, `admins`.`email` as `admin_email`, `admins`.`phone` as `admin_phone`, `admins`.`movil` as `admin_movil` from `proposals` inner join `admins` on `proposals`.`admin_id` = `admins`.`id` where (`proposals`.`order` like %% or `proposals`.`title` like %%) and `admin_id` = 1 order by `id` asc limit 5 offset 0)
答案 0 :(得分:1)
在admins表中检查是否有一个last_name字段。如果没有,那么
public function scopeName($query, $name)
{
$query->join('admins', 'proposals.admin_id', '=', 'admins.id')
->select(
'proposals.*',
'admins.name as admin_name',
'admins.area as admin_area',
'admins.job_title as admin_job_title',
'admins.email as admin_email',
'admins.phone as admin_phone',
'admins.movil as admin_movil')
->where('proposals.order','like',"%$name%")
->orWhere('proposals.title','like',"%$name%");
}