So I have some problem in here, the links is not shown on my output data, I have try made query like this :
$getData = Inventory::select('id_inventory', 'inventory_code', 'inventory_id_location', 'inventory_id_category', 'date_of_buy', 'price_of_item', 'condition_of_item', 'description', 'created_by', 'edited_by')
->selectRaw('group_concat(inventory_id_category) as inventory_id_categories')
->selectRaw('group_concat(id_inventory) as id_inventories')
->with(['categories' => function ($query) {
$query->select('id_category', 'category_code', 'category_name');
}])
->with(['locations' => function ($query) {
$query->select('id_location', 'location_name');
}])
->with(['delivery_inventories' => function ($query) {
$query->select('id_delivery', 'delivery_id_inventory', 'delivery_id_location', 'delivery_date', 'deliver_by', 'description')
->with(['location_deliveries' => function ($query) {
$query->select('id_location', 'location_name');
}]);
}])
->whereHas('categories', function ($query) use ($search) {
$query->orWhere('category_code', 'like', "%{$search}%")
->orWhere('category_name', 'like', "%{$search}%");
})
->whereHas('locations', function ($query) use ($search) {
$query->orWhere('location_name', 'like', "%{$search}%");
})
->whereHas('delivery_inventories', function ($query) use ($search) {
$query->orWhere('delivery_date', 'like', "%{$search}%")
->orWhere('deliver_by', 'like', "%{$search}%")
->orWhere('description', 'like', "%{$search}%");
})
->where('inventory_code', 'like', "%{$search}%")
->orWhere('price_of_item', 'like', "%{$search}%")
->orWhere('condition_of_item', 'like', "%{$search}%")
->orWhere('description', 'like', "%{$search}%")
->groupBy('inventory_code')
->orderBy('inventory_code')
->paginate(10);
return response()->json([
'get_data' => $getData
], 200);
but on my console the result like this :
I wanna ask any wrong or something uncompleted query so that's my links doesn't show up on my console? Any suggest? Thank you.
my project before (but I can't use that in my current project) :
Did you update your AppServiceProvider?
if not go to app/providers/AppServiceProvider.php
and add paginator in there.
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Pagination\Paginator;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
Paginator::useBootstrap();
}
}
Related
I am using Laravel Livewire dataTables and while searching I am getting an error.
Object of class Illuminate\Database\Eloquent\Builder could not be
converted to string
my method for searching is
public static function search($QUERY)
{
return empty($QUERY) ? static::QUERY()
: static::WHERE(function ($QUERY) {
$QUERY->WHERE('name', 'like', '%' . $QUERY . '%')
->orWhere('email', 'like', '%' . $QUERY . '%');
});
}
Livewire render method
public function render()
{
//$this->roles = ROLE::WHERE('company_id', SESSION('company_id'))->paginate(5);
return VIEW('livewire.users', [
'users' => USER::search($this->search)
->WITH(['role', 'company'])
->WHERE('company_id', SESSION('company_id'))
->orderBy($this->sortField, $this->sortAsc ? 'asc' : 'desc')
->paginate($this->perPage),
]);
}
Instead of having the actual method search(), you can make a scope, which will be rendered the same, but will also have some Laravel-magic helping you.
Replace your search() method on your User model with this scopeSearch() method. You do not change the query where its being used.
/**
* Scope a query to search for users name and email
*
* #param \Illuminate\Database\Eloquent\Builder $query
* #return \Illuminate\Database\Eloquent\Builder
*/
public function scopeSearch(Builder $query, string $search)
{
return $query->where('name', 'like', '%'.$search.'%')
->orWhere('email', 'like', '%'.$search.'%');
}
And import that Builder class, by adding the following line to the top of your User model class,
use Illuminate\Database\Eloquent\Builder;
The trick here is the scope prefix, with a capital S in "search". See the official documentation for more details.
Here is my Macro for relation search in Laravel Eloquent.
Bellow is my Macro in AppServiceProvider.php
Builder::macro('whereLike', function ($attributes, string $searchTerm) {
$this->where(function (Builder $query) use ($attributes, $searchTerm) {
foreach (Arr::wrap($attributes) as $attribute) {
$query->when(
str_contains($attribute, '.'),
function (Builder $query) use ($attribute, $searchTerm) {
[$relationName, $relationAttribute] = explode('.', $attribute);
$query->orWhereHas($relationName, function (Builder $query) use
($relationAttribute, $searchTerm) {
$query->where($relationAttribute, 'LIKE', "%{$searchTerm}%");
});
},
function (Builder $query) use ($attribute, $searchTerm) {
$query->orWhere($attribute, 'LIKE', "%{$searchTerm}%");
}
);
}
});
return $this;
});
Bellow is my search Query in MemberController.php
public function getMembers(request $request){
$members = Member::with('products')->where('groupId', 1)->whereLike(['firstname',
'lastname', 'products.name'], $request->searchValue)->paginate(10);
dd($members);
}
The "whereLike" is invoked from the appServiceProvider.php as a macro in there thus where the error occur if i remove the members.name the macro executes perfectly.
I get an Error with orWhereHas
"Call to undefined method Illuminate\Database\Query\Builder::orWhereHas()"
I have a model named Student with the field name , here is the function, which is correct.
public function index(Request $request)
{
$user = $request->user();
$students = Student::query()
->when($user->hasRole('admin') !== true, function (Builder $query) use ($user) {
$query->where('email', $user->email);
})
->when($request->has('search'), function (Builder $query) use ($request) {
$query->where('name', 'like', '%'.$request->input('search').'%');
})
->paginate(5);
return view('admin.students.index', compact('students'))
->with('display_search', $user->hasRole('admin'));
}
Now, in my model Payment I have a request with 2 tables (Payment & Student).
Here is my function index()
public function index(Request $req)
{
if ($req->search == "") {
$payments = Payment::paginate(5);
return view('admin.payments.index', compact('payments'));
} else {
$validated = $req->validate([
'search' => 'alpha',
]);
$payments = payment::whereHas('students', function($query) use($req) {
$query->where('name', 'like', '%' . $req->search . '%');
})->paginate(5);
return view('admin.payments.index', compact('payments'));
}
}
My problem is I want to adapt my function for connect me with an user.
I have this for now ???
public function index(Request $request)
{
$user = $request->user();
$payments = Payment::query()
->when($user->hasRole('admin') !== true, function (Builder $query) use ($user) {
$query->where('email', $user->email);
})
->when($request->has('search'), function (Builder $query) use ($request) {
how to include this:
$payments = payment :: whereHas ('students', function ($ query) use ($ req) {
$query-> where ('name', 'like', '%'. $ req-> search. '%');
I have a problem with my function index() concerning a request. In fact, I have to adapt my old function with a recent.
For information, my function index() (old) below was written like this and it works !!
public function index(Request $req)
{
if ($req->search == "") {
$retours = Retour::join('eleves', 'retours.fk_eleve', '=', 'eleves.id')->orderBy('eleves.nom', 'asc')->select('retours.*')->paginate(5);
return view('admin.retours.index', compact('retours'));
} else {
$validated = $req->validate([
'search' => 'alpha',
]);
$retours = Retour::join('eleves', 'retours.fk_eleve', '=', 'eleves.id')->where('eleves.nom','like', '%' . $req->search . '%')->orderBy('eleves.nom', 'asc')->select('retours.*')->paginate(5);
return view('admin.retours.index', compact('retours'));
}
}
Now, I have to change a party of code and I must to adapt my request:
$retours = Retour::join('eleves', 'retours.fk_eleve', '=', 'eleves.id')->where('eleves.nom','like', '%' . $req->search . '%')->orderBy('eleves.nom', 'asc')->select('retours.*')->paginate(5);
Here is an idea of my new code, my problem is that the search bar doesn't filter ? ^^
public function index(Request $request)
{
$user = $request->user();
$retours = Retour::query()
->when($user->hasRole('admin') !== true, function (Builder $query) use ($user) {
$query->where('email', $user->email);
})
->when($request->has('search'), function (Builder $query) use ($request) {
$query->join('eleves', 'retours.fk_eleve', '=', 'eleves.id')->orderBy('eleves.nom', 'asc')->select('retours.*');
})
->paginate(5);
return view('admin.retours.index', compact('retours'))
->with('display_search', $user->hasRole('admin'));
}
My problem is perhaps here ?
->when($request->has('search'), function (Builder $query) use ($request) {
$query->join('eleves', 'retours.fk_eleve', '=', 'eleves.id')->orderBy('eleves.nom', 'asc')->select('retours.*');
})
->paginate(5);
Do have you an idea please?
Thank you
I am trying to do a search on products but it does not return any results. I suspect it has something to do with the whereHas function but I just cannot figure it out. Here is my code:
My Relations:
Categories Model:
class Categories extends Eloquent {
public function product()
{
return $this->hasMany('Products')->orderBy('name');
}
}
Products Model:
class Products extends Eloquent {
public function category()
{
return $this->belongsTo('Category')->orderBy('name');
}
}
My search function:
public function search()
{
$search = Input::get('search');
$result = Products::where('name', 'LIKE', '%'. $search .'%')->get();
if ($result->first()) {
return View::make('groceries.index')
->with('categories', Categories::with('product')->orderBy('name', 'asc')
->whereHas('product', function($query) use ($search)
{
$query->where('name', 'LIKE', '%'.$search.'%');
}));
}
}
In the view:
#foreach ($categories as $category)
//do stuff
#foreach ($category->product as $products)
//Show results
#endforeach
#endforeach
You have to call get() to get the data.
And I think this will be more readable code.
public function search()
{
$search = Input::get('search');
$result = Products::where('name', 'LIKE', '%'. $search .'%')->get();
if ($result->first()) {
$categories = Categories::with('product')->orderBy('name', 'asc')
->whereHas('product', function($query) use ($search){
$query->where('name', 'LIKE', '%'.$search.'%');
})->get();
return View::make('groceries.index')
->with('categories', $categories);
}
}