Undefined property: Illuminate\Database\MySqlConnection::$name - php

Hi i'm trying to make a small system filtering in laravel using the following function in my controller to filter users by their roles or their names here's my current code:
profilecontroller.php:
public function membrevis()
{
$filter = isset($_GET['filter']) ? $_GET['filter'] : null;
$users = DB::table('users')
->join('user_role', 'users.id', '=', 'user_role.user_id')
->join('roles', 'user_role.role_id', '=', 'roles.id')
->where('users.valid','=',0)
->select('users.*','roles.description');
if ($filter != null) {
$users->where('users.name','like','%'.$filter.'%')
->orWhere('roles.description',' like','%'.$filter.'%');
}
$users->get();
return view('membre2',['users'=> $users]);
}
My view has an input form where you can type the name or the role of the members you are trying to filter :
membre2.blade.php:
<form action="/profilecontroller/membrevis" method="get">
<input type="text" name="filter" >
<button type="submit">filter</button>
</form>
#foreach($users as $users)
<h4 class="media-heading">{{ $users->name }}</h4>
#endforeach
The error i'm getting is Undefined property: Illuminate\Database\MySqlConnection::$name
I have no idea why i'm getting this error i'm obviously passing $users to my view with this line of command:
return view('membre2',['users'=> $users]);
Any help would be appreciated! thank you

when using $users->get() it returns Collection instance, you must be assign $users = $users->get(); Corrected code is
public function membrevis()
{
$filter = isset($_GET['filter']) ? $_GET['filter'] : null;
// also can use this structure $filter = isset($_GET['filter']) ?? null;
$users = DB::table('users')
->join('user_role', 'users.id', '=', 'user_role.user_id')
->join('roles', 'user_role.role_id', '=', 'roles.id')
->where('users.valid','=',0)
->select('users.*','roles.description');
if ($filter != null) {
$users->where('users.name','like','%'.$filter.'%')
->orWhere('roles.description',' like','%'.$filter.'%');
}
$users = $users->get();
return view('membre2',['users'=> $users]);
}
also in blade fix it
#foreach($users as $user)
<h4 class="media-heading">{{ $user->name }}</h4>
#endforeach

Related

How to combine lines of code into one line?

I have 2 lines of code here but i want to combine all 2 lines into 1
// How to combine these 2 lines here
$product = $this->productService->searchProduct($request);
$product = Product::paginate(9);
Controller:
public function searchProduct(SearchProductRequest $request)
{
$search = $request->search;
$min_price = $request->min_price;
$max_price = $request->max_price;
$product = Product::query('products')
->where('name', 'like', "%{$search}%")
->where('price', [$min_price])
->orWhere('price',[$max_price])
->orderBy('id');
return $product;
}
you can try :
$product = $this->productService->searchProduct($request)->paginate(9);
take a look at your view blade, have you added links();
like this
<div class="float-left mr-3 mb-1">
{{ $product->links() }}
</div>
$product = !empty($this->productService->searchProduct($request)?Product::paginate(9):'';
Try this
public function searchProduct(SearchProductRequest $request)
{
$products = Product::select('id', 'name', 'price')
->when($request->input('search'), function($q){
$q->where('name', 'like', '%' . request()->input('search') . '%');
})
->when($request->input('min_price'), function($q){
$q->where('price', request()->input('min_price'));
})
->when($request->input('max_price'), function($q){
$q->orWhere('price', request()->input('max_price'));
})
->orderBy('id')
->paginate(9);
return $products;
}

Laravel advanced search query fix

I have a search form with multiple input and select boxes I need help to get if conditions in my query in order to each part works separately and all at once.
here is my blade codes:
<form action="{{route('advancesearch')}}" method="post">
{{csrf_field()}}
<div class="sidebar-title">
<span>Advanced Search</span>
<i class="fa fa-caret-down show_sidebar_content" aria-hidden="true"></i>
</div>
<!-- ./sidebar-title -->
<div id="tags-filter-content" class="sidebar-content">
<div class="filter-tag-group">
#foreach($options as $option)
<div class="tag-group">
<p class="title">
<span class="filter-title show_filter_content">{{$option->title}} <span class="pull-right"><i class="fa fa-minus"></i></span></span>
</p>
<div class="filter-content">
<div class="checkbox">
#foreach($option->suboptions as $suboption)
<label for="suboptions">
<input name="suboptions[]" type="checkbox" value="{{$suboption->id}}">
{{ucfirst($suboption->title)}}
</label>
#endforeach
</div>
</div>
</div>
#endforeach
<!-- ./tag-group -->
<div class="tag-group">
<p class="title">
<span class="filter-title show_filter_content">Brand <span class="pull-right"><i class="fa fa-minus"></i></span></span>
</p>
<div class="filter-content">
<div class="checkbox">
#foreach($brands as $brand)
<label for="brands">
<input name="brands[]" type="checkbox" value="{{$brand->id}}">
{{$brand->title}}
</label>
#endforeach
</div>
</div>
</div>
<!-- ./tag-group -->
<div class="tag-group">
<p class="title">
<span class="filter-title show_filter_content">Price Range <span class="pull-right"><i class="fa fa-minus"></i></span></span>
</p>
<div class="row filter-content">
<div class="col-md-6">
<div class="form-group">
<label for="min_price" hidden>Min</label>
<input type="text" name="min_price" class="form-control" placeholder="Rp Min">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="max_price" hidden>Max</label>
<input type="text" name="max_price" class="form-control" placeholder="Rp Max">
</div>
</div>
</div>
</div>
<!-- tag-group -->
<div class="text-center mt-20">
<button type="submit" class="btn btn-danger">TERPAKAN</button>
</div>
</div><!-- ./filter-tag-group -->
</div><!-- ./sidebar-content -->
</form>
and this is my route:
Route::post('/advanced-search', 'frontend\SearchController#filter')->name('advancesearch');
finally my function code is:
public function advancedsearch(Request $request) {
$brands = Brand::all(); // uses for other part of the page. (not related to search function)
$options = Option::all(); // uses for other part of the page. (not related to search function)
$suboptions = DB::table('product_suboption'); // where my product_id and subopyion_id saves
//search function
$products = Product::where(function($query){
//getting inputs
$suboptions2 = Input::has('suboptions') ? Input::get('suboptions') : [];
$min_price = Input::has('min_price') ? Input::get('min_price') : null;
$max_price = Input::has('max_price') ? Input::get('max_price') : null;
$brands2 = Input::has('brands') ? Input::get('brands') : [];
//returning results
$query->where('price','>=',$min_price)
->where('price','<=',$max_price);
})->get();
return view('front.advancesearch', compact('products', 'brands', 'options'));
}
My models relations:
product model:
public function options(){
return $this->belongsToMany(Option::class);
}
public function suboptions(){
return $this->belongsToMany(Suboption::class, 'product_suboption', 'product_id', 'suboption_id');
}
public function brand(){
return $this->belongsTo(Brand::class);
}
Option model:
public function suboptions(){
return $this->hasMany(Suboption::class, 'option_id');
}
public function products(){
return $this->belongsToMany(Product::class);
}
Suboption model:
public function option(){
return $this->belongsTo(Option::class, 'option_id');
}
public function products(){
return $this->belongsToMany(Product::class);
}
Brand model:
public function products(){
return $this->hasMany(Product::class);
}
note
My brands search is coming from products table where I have column brand_id for each product.
BUT
My suboptions come from 3rd table named product_suboption (as you see in my models codes) where i save product_id and suboption_id.
This is just to give an idea. You can use a multiple ->where() and eager loading ->with() for your query.
Take a look with this query below:
$products = Product::where('price', '>=', $min_price) // you get the max and min price
->where('id', '<=', $max_price)->select('id')
->with([
"brand" => function ($query) {
$query->whereIn('id', $brand_ids); // [1, 2, 3,...]
},
"specifications" => function ($query) {
$query->where('some_column', '=', 'possible-value'); // single condition
},
"specifications.subspecifications" => function ($query) {
$query->where([
'some_column' => 'possible-value',
'another_column' => 'possible-value'
]); // you can also pass arrays of condition
}
])->get(); // This will return the products with the price set by the user
// Since we're just using ->with(), this will also return those products
// that doesn't match the other criteria specifications) so we
// still need to filter it.
Finally, you can filter the products which matches the specifications,
- the product with an empty specifications means this product does not match the criteria, therefore we'll have to remove it from the collection.
$filtered = $products->filter(function ($product, $key) {
return count($product->brand) > 0 && count($product->specifications) > 0;
// add your other boolean conditions here
});
dd($filtered->toArray()); // your filtered products to return
You can use laravel orWhere and orWhereHas to get results separately and all at once, let's say you do not select min_price and max_price but you have selected brand then all products with this brnad should be return, your query will look like this
$products = Product::orWhere('price','>=',$min_price)
->orWhere('price','<=',$max_price)
->orWhereHas('brand',function($query){
$query->whereIn('id', $brand_ids);
})
->orWhereHas('suboptions',function($query){
$query->whereIn('id', $suboptions_ids);
})
->orWhereHas('subspecifications',function($query){
$query->whereIn('id', $subspecifications_ids);
})->get();
$products will have products collection If any of the condition stated in above query matched.
Hope this helps.
Here's how I'd do it. Note the use of when for simplifying optional where conditions (no need to set variables either), and the closure for constraining both the whereHas and the with (if you want to eager load the relationships).
$products = Product::query()
->when($request->min_price, function ($query, $min_price) {
return $query->where('price', '>=', $min_price);
})
->when($request->max_price, function ($query, $max_price) {
return $query->where('price', '<=', $max_price);
})
->when($request->suboptions, function ($query, $suboptions) {
$suboptionsConstraint = function ($q) use ($suboptions) {
return $q->whereIn('id', $suboptions);
};
return $query->whereHas('suboptions', $suboptionsContraint)
->with(['suboptions' => $suboptionsContraint]);
})
->when($request->brands, function ($query, $brands) {
$brandsConstraint = function ($q) use ($brands) {
return $q->whereIn('id', $brands);
};
return $query->whereHas('brands', $brandsConstraint)
->with(['brands' => $brandsConstraint]);
});
I suggest tu use each separeted and its help you to feature easaly manupulate code
as your typical condition your sub_option come from third table last relation ship is used.
if(count($request['suboptions'])) {
$product->whereHas('options',function($options) use ($request) {
$options->whereHas('suboptions',function($suboption)use($request) {
$suboption->whereIn('id',$request['suboptions']);
});
});
}
for min price max price i assume your price in procuct table
if(! empty($request['min_price'])) {
$product->where('price','>=',$request['min_price']);
}
if(! empty($request['max_price'])) {
$product->where('price','<=',$request['max_price']);
}
for brand as you say brand_id column in product table then
if(count($request['brands'])) {
$product->whereIn('brand_id',$request['brands']);
}
I suggest a different approach.
On your controller, change it to this:
public function advancedsearch(Request $request) {
$suboptions2 = request->suboptions ? request->suboptions : null;
$min_price = request->min_price ? request->min_price : null;
$max_price = request->max_price ? request->max_price : null;
$brands2 = request->brands ? request->brands : null;
$query = Product::select('field_1', 'field_2', 'field_3')
->join('brands as b', 'b.id', '=', 'products.brand_id')
...(others joins);
// here we do the search query
if($suboptions2){
$query->where('suboptions_field', '=', $suboptions);
}
if($min_price && $max_price){
$query->where(function($q2) {
$q2->where('price', '>=', $min_price)
->where('price', '<=', $max_price)
});
}
if($brands2){
$query->where('products.brand_id', '=', $brands2);
}
// others queries
// finish it with this
$query->get();
return view('front.advancesearch', compact('products', 'brands', 'options'));
I find doing it this way is very useful because it can be really easy to implement additional queries.
This is the method I use to search using laravel eloquent with multiple input:
$input = Input::all(); //group all the inputs into single array
$product = Product::with('options','suboptions','brand');
//looping through your input to filter your product result
foreach ($input as $key => $value)
{
if ($value!='') {
if ($key == "max_price")
$product = $product->where('price','<=', $value);
elseif ($key == "min_price")
$product = $product->where('price','>=', $value);
elseif ($key == "brands")
$product = $product->whereIn('brand_id', $value); //assuming that your Input::get('brands') is in array format
elseif ($key == "suboptions")
$product = $product->whereIn('suboption_id', $value);
}
}
$product = $product->get();
The method above will return all products if no input is submitted, and will filter the result based on the input if available, on top of this it's also a good practice to sanitize your inputs with validations before proceeding with the query
SOLVED
After weeks of playing with codes finally I came to the right results for myself (in my case it works this way for others maybe works with other suggested answers)
public function advancedsearch(Request $request) {
$options = Option::all();
$brands = Brand::all();
$brandss = Input::has('brands') ? Input::get('brands') : [];
$suboption = Input::has('suboptions') ? (int)Input::get('suboptions') : [];
$min_price = Input::has('min_price') ? (int)Input::get('min_price') : null;
$max_price = Input::has('max_price') ? (int)Input::get('max_price') : null;
//codes
if(count($request['suboptions'])){
$products = DB::table('products')
->join('product_suboption', function ($join) {
$suboption = Input::has('suboptions') ? Input::get('suboptions') : [];
$join->on('products.id', '=', 'product_suboption.product_id')
->where('product_suboption.suboption_id', '=', $suboption);
})
->paginate(12);
}
elseif(count($request['brands'])){
$products = DB::table('products')
->whereIn('products.brand_id', $brandss)
->paginate(12);
}
elseif(count($request['min_price']) && count($request['max_price'])){
$products = DB::table('products')
->whereBetween('price', [$min_price, $max_price])
->paginate(12);
}
return view('front.advancesearch', compact('products', 'brands', 'options'));
}
NOTE: most of my pricing issues solved with (int) as you see in my codes (int)Input::get('min_price') and
(int)Input::get('max_price').
Special thanks to Ravindra Bhanderi for his count($request[''] suggestion.
it is very simple to make dynamic search by using treats we can use this for all models I made this dynamic as possible
This is a trait that can be used by any models
This function will remove code repetitions into your project
public function scopeSearch($query, $keyword, $columns = [], $relativeTables = [])
{
if (empty($columns)) {
$columns = array_except(
Schema::getColumnListing($this->table), $this->guarded
);
}
$query->where(function ($query) use ($keyword, $columns) {
foreach ($columns as $key => $column) {
$clause = $key == 0 ? 'where' : 'orWhere';
$query->$clause($column, "LIKE", "%$keyword%");
if (!empty($relativeTables)) {
$this->filterByRelationship($query, $keyword, $relativeTables);
}
}
});
return $query;
}
Filter into relationship also
private function filterByRelationship($query, $keyword, $relativeTables)
{
foreach ($relativeTables as $relationship => $relativeColumns) {
$query->orWhereHas($relationship, function($relationQuery) use ($keyword, $relativeColumns) {
foreach ($relativeColumns as $key => $column) {
$clause = $key == 0 ? 'where' : 'orWhere';
$relationQuery->$clause($column, "LIKE", "%$keyword%");
}
});
}
return $query;
}

Laravel eloquent chunk method return all the time false or null

I am using Laravel v4.2. I want to delete conversation between two users but Now I want that If one user delete the conversation than other user can view the conversation until second user also delete the conversation.
For this I have two column to delete conversation name "delete_on" and "delete_two". For this purpose I am using eloquent chunk method which always return false or null.
$return = Message::where('message_to', '=', $userData['id'])
->where('message_from', '=', $userData['message_from'])
->orwhere(function($query) use($userData) {
$query->where('message_to', '=', $userData['message_from'])
->where('message_from', '=', $userData['id']);
})->chunk(100,function($messages) use($userData){
foreach ($messages as $msg){
if(empty($msg->delete_one)){
$msg->delete_one = $userData['id'];
}else{
$msg->delete_two = $userData['id'];
}
if($msg->save()){
}
}
});
To know more about chunk() refer to this answer mentioned in the comments Thanks Gokigooooks for the acknowledgement.
The syntax you need to employ is the following for in your case it's eloquent:
Eloquent
For first of all check whether you are getting chunk result here like this:
Message::where('message_to', '=', $userData['id'])
->where('message_from', '=', $userData['message_from'])
->orwhere(function($query) use($userData) {
$query->where('message_to', '=', $userData['message_from'])
->where('message_from', '=', $userData['id']);
})->chunk(100,function($messages) use($userData){
foreach ($messages as $msg){
dd($msg); or print_r($msg);
}
});
Now try this
$return = Message::where('message_to', '=', $userData['id'])
->where('message_from', '=', $userData['message_from'])
->orwhere(function($query) use($userData) {
$query->where('message_to', '=', $userData['message_from'])
->where('message_from', '=', $userData['id']);
})->chunk(100,function($messages) use($userData){
foreach ($messages as $msg){
if(empty($msg->delete_one)){
$msg->delete_one = $userData['id'];
}else{
$msg->delete_two = $userData['id'];
}
$msg->save();
}
});

How to include or exclude where statement in Laravel Eloquent

I need the same query for two different user roles. Difference is only in one whereNotIn condition.
So for the Basic user it would be:
$chart2 = DB::connection('mysql2')->table('tv')
->select('*')
->join('epgdata_channel', 'cid', '=', 'channelid')
->where('ReferenceDescription', $campaign->spotid)
->whereNotIn('ChannelName', $sky)
->get();
And for Premium:
$chart2 = DB::connection('mysql2')->table('tv')
->select('*')
->join('epgdata_channel', 'cid', '=', 'channelid')
->where('ReferenceDescription', $campaign->spotid)
->get();
I know I can do it with simple if statement:
if($user->userRole == "Basic"){
//first $chart2
}
else{
//second $chart2}
but I have a lots of queries where I need just to add or remove this whereNotin condition and rewriting the queries (using if statement) is not a nice solution.
Try scope.
In your TVModel.php:
public function scopeConditionalWhereNotIn($query, $doesUse, $col, $val) {
if($doesUse)
$query->whereNotIn($col, $val);
}
Usage:
$condi = true;//or false.
$chart2 = TVModel::select('*')
->join('epgdata_channel', 'cid', '=', 'channelid')
->where('ReferenceDescription', $campaign->spotid)
->conditionalWhereNotIn($condi, 'ChannelName', $sky)
->get();
Inside your model add this:
public function scopeBasicUser($query,$channel){
return $query->whereNotIn('ChannelName', $channel);
}
and in your controller:
$query = DB::connection('mysql2')->table('tv')
->select('*')
->join('epgdata_channel', 'cid', '=', 'channelid')
->where('ReferenceDescription', $campaign->spotid);
if($user->userRole == "Basic")
$query = $query->basicUser($channel);
return $query->get();
$userRole = $user->userRole;
$chart2 = DB::connection('mysql2')->table('tv')
->select('*')
->join('epgdata_channel', 'cid', '=', 'channelid')
->where('ReferenceDescription', $campaign->spotid)
->where(function ($query) use ($userRole){
if($userRole == "Basic"){
$query->whereNotIn('ChannelName', $sky)
}
})
->get();
This code worked for me.

Laravel: i can't send more then 2 variables from controller to a view

So i am trying to send some query from the controller to a view but when try use the third variable it says:
Undefined variable: type(View:)
The code i'm using is this in the controller :
$doc=DB::table('documents')
->join('users', 'users.id', '=', 'documents.id_user')
->join('type_docs', 'type_docs.id', '=', 'documents.id_tipo_doc')
->join('departments', 'departments.id', '=', 'documents.id_departamento')
->select('documents.*', 'type_docs.type', 'users.name','departments.abbreviation')
->get();
$user=DB::table('users')
->select('users.*')
->get();
$type=DB::table('type_docs')
->select('type_docs.*')
->get();
//$doc = Document::all();
return view('dashboard',['doc'=>$doc],['user'=>$user],['type'=>$type]);
and in the view:
#foreach($type as $types)
<option value="{{$types->id}}">{{$types->type}}</option>
#endforeach
You should return one array :
return view('dashboard',['doc'=>$doc,'user'=>$user,'type'=>$type]);
There is other ways such us :
return view('dashboard', array('doc'=>$doc,'user'=>$user,'type'=>$type));
return view('dashboard', compact('doc','user','type'));
return view('dashboard')
->with('doc', $doc)
->with('user', $user)
->with('type', $type);
return view('dashboard') //using laravel Magic method.
->withDoc($doc)
->withUser($user)
->withType($type);

Categories