Or with And Statement in Eloquent livewire - laravel

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.

Related

Pagination links is not show when using paginate in laravel

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();
}
}

issue in laravel relationship search data

I want to search for usernames from the user's table.
I have used the Laravel relationship(eloquent). I don't search for usernames from users' tables. My code is Below and here I Explain the relationship with All Tablets
I have a query write inside the group controller
public function getGroupsData(Request $request){
$group = Group::with(['memberscount', 'getusername'])->where('groupName', "like", "%" . $request->searchText . "%")
->whereHas('getusername.user', function ($query) use ($request) {
return $query->where('name', 'like', '%'. $request->searchText . '%');
})
->get();
}
memberscount and getusername relationship write-in group model my group model is below
class Group extends Model
{
public function memberscount(){
return $this->hasMany('App\Models\Members', 'group_id', 'id')->where('memberStatus', 'Joined');
}
public function getusername(){
return $this->hasOne('App\Models\Members', 'group_id', 'id')->where('isOwner', 0);
}
}
And user's table relationship in members models. members model is below
class Members extends Model
{
public function user(){
return $this->hasOne('App\User', 'id', 'user_id');
}
/**get group name */
public function group(){
return $this->hasOne('App\Models\Group', 'id', 'group_id');
}
}
I have search user's data this(user) relationship. But I can't search data in a multilevel relationship.
OK, I'm the first step is to rename name relationships in class Group
I think it was more logical names
memberscount -> members
getusername -> owner
And the second step is to search. I write like this:
$group = Group::whereHas('owner', function ($query) use ($request) {
return $query->where('groupName', 'like', '%'. $request->searchText . '%');
})->get();
or more effective and clear code we can save in scope us query like that:
$group = Group::whereHas('owner', function ($query) use ($request) {
return $query->haveGroupName($request->searchText);
})->get();
In the Group model class write code like that:
/**
* Scope a query to only have groupName.
*
* #param \Illuminate\Database\Eloquent\Builder $query
* #param string $param
* #return \Illuminate\Database\Eloquent\Builder
*/
public function scopeHaveGroupName($query, $param)
{
return $query->where('groupName', 'like', '%' . $param . '%');
}

orWhereHas not existing laravel Eloquent php 7.4

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()"

Laravel Eloquent how to override the base query?

I have 2 models shared a same table.
Table name: books, I separate normal book & novel by an attribute called type
Book model
class Book extends \Illuminate\Database\Eloquent\Model
{
protected $table = 'books';
}
Novel model
class Novel extends Book
{
protected $table = 'books';
// Is such a method available?
protected function someMethodToOverride()
{
$this->where('type', 'novel');
}
}
What I want to achieve here is
$results = Novel::where('title', 'LIKE', '%' . $title . '%')->get();
from this query, I want it to pre-set the condition
where('type', 'novel')
Is there any function I can override to achieve this?
use Anonymous Global Scopes and add this boot method in Novel Model
protected static function boot()
{
parent::boot();
static::addGlobalScope('type', function (\Illuminate\Database\Eloquent\Builder $builder) {
$builder->where('type', 'novel');
});
}
now it will automatically add query
$results = Novel::where('title', 'LIKE', '%' . $title . '%')->get();
now If you would like to remove a global scope for a given query, you
may use the withoutGlobalScope method.
Novel::withoutGlobalScope('type')->get();
for more information read this article
In Model You can use scope:
public function scopeOfType($query, $type)
{
return $query->where('type', $type);
}
And pass the parameters when calling the scope:
Model::ofType('novel')
->where('title', 'LIKE', '%' . $title . '%')
->get();
refer link Laravel Scope

Call to a member function latest() on string in laravel

What is this error.
Call to a member function latest() on string
Controller
public function search()
{
$keyword = request('search');
$articles = Article::search($keyword)->latest()->get();
return $articles;
}
Model
public function scopeSearch($query , $keyword)
{
$query->where('title', 'LIKE', "%".$keyword."%");
return $keyword;
}
You shouldn't return a string from the local scope. So, change it to:
public function scopeSearch($query , $keyword)
{
return $query->where('title', 'like', '%' . $keyword . '%');
}

Resources