根据输入值,查询类别中是否包含姓名或公司名称。我要return公司名单
With the input value, query whether the category contains the name or the company name. I want to return the company list
根据输入值,查询类别中是否包含姓名或公司名称。我要return公司名单。
控制器中的查询方法中,return $comp;听起来很空。
控制器:
public function query(Request $request)
{
$search = $request->what;
$companies = Company::where('deleted_at', null);
if ($search) {
$categories = Category::where('deleted_at', null);
$comp = [];
foreach ($categories as $category) {
if (Str::contains(mb_strtolower($category->name), mb_strtolower($search)))
array_push($comp, $category->company);
}
return $comp;
}
}
类别模型:
public function companies()
{
return $this->hasMany('App\Company', 'category_id', 'id');
}
公司型号:
public function category()
{
return $this->belongsTo('App\Category', 'category_id', 'id')->withDefault([
'name' => '-']);
}
类别迁移:
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('upper_id')->default(0);
$table->string('name', 100);
$table->string('slug', 100);
$table->string('description', 500)->nullable();
$table->timestamps();
$table->softDeletes();
});
公司迁移:
Schema::create('companies', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('category_id');
$table->string('name', 100);
$table->string('slug', 100);
$table->timestamps();
$table->softDeletes();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
});
如果你想要公司列表,最好在公司模型中查询。
$companies = Company::with('category')
->where('name', 'LIKE',"%{$search}%")
->orWhereHas('category', function($query) use($search) {
$query->where('name','LIKE',"%{$search}%");
})
->get();
它将 return collection 搜索公司名称或类别名称的公司。
如果你想在类别模型中查询,你需要使用类似
的东西
$categories = Category::where('name', 'LIKE',"%{$search}%")
->orWhereHas('companies', function($query) use($search) {
$query->where('name','LIKE',"%{$search}%");
})
->get();
现在您可以推送与每个类别关联的公司并将数组发送到blade。但最好的方法是将类别发送到 blade 并像
一样迭代它们
@foreach($categories as $category)
@foreach($category->companies as $company)
{{ $company->name }}
@endforeach
@endforeach
根据输入值,查询类别中是否包含姓名或公司名称。我要return公司名单。
控制器中的查询方法中,return $comp;听起来很空。
控制器:
public function query(Request $request)
{
$search = $request->what;
$companies = Company::where('deleted_at', null);
if ($search) {
$categories = Category::where('deleted_at', null);
$comp = [];
foreach ($categories as $category) {
if (Str::contains(mb_strtolower($category->name), mb_strtolower($search)))
array_push($comp, $category->company);
}
return $comp;
}
}
类别模型:
public function companies()
{
return $this->hasMany('App\Company', 'category_id', 'id');
}
公司型号:
public function category()
{
return $this->belongsTo('App\Category', 'category_id', 'id')->withDefault([
'name' => '-']);
}
类别迁移:
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('upper_id')->default(0);
$table->string('name', 100);
$table->string('slug', 100);
$table->string('description', 500)->nullable();
$table->timestamps();
$table->softDeletes();
});
公司迁移:
Schema::create('companies', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('category_id');
$table->string('name', 100);
$table->string('slug', 100);
$table->timestamps();
$table->softDeletes();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
});
如果你想要公司列表,最好在公司模型中查询。
$companies = Company::with('category')
->where('name', 'LIKE',"%{$search}%")
->orWhereHas('category', function($query) use($search) {
$query->where('name','LIKE',"%{$search}%");
})
->get();
它将 return collection 搜索公司名称或类别名称的公司。 如果你想在类别模型中查询,你需要使用类似
的东西$categories = Category::where('name', 'LIKE',"%{$search}%")
->orWhereHas('companies', function($query) use($search) {
$query->where('name','LIKE',"%{$search}%");
})
->get();
现在您可以推送与每个类别关联的公司并将数组发送到blade。但最好的方法是将类别发送到 blade 并像
一样迭代它们@foreach($categories as $category)
@foreach($category->companies as $company)
{{ $company->name }}
@endforeach
@endforeach