Currently my HomeController looks like this:
class HomeController extends BaseController {
public function getHome()
{
$scripts = Script::select('script.*', DB::raw('COALESCE(SUM(vote.rating), 0) as rating'))
->leftJoin('script_vote as vote', 'vote.script_id', '=', 'script.id')
->with('tags')
->orderBy('rating', 'desc')
->orderBy('views', 'desc')
->groupBy('id')
->paginate(8);
return View::make('home')->with('scripts', $scripts);
}
public function postSearch()
{
$input = array(
'query' => Input::get('query'),
'sort_col' => Input::get('sort_col'),
'sort_dir' => Input::get('sort_dir'),
);
$scripts = Script::select('script.*', DB::raw('COALESCE(SUM(vote.rating), 0) as rating'))
->leftJoin('script_vote as vote', 'vote.script_id', '=', 'script.id')
->where('title', 'LIKE', '%' . $input['query'] . '%')
->orderBy($input['sort_col'], $input['sort_dir'])
->orderBy('views', 'desc')
->groupBy('id')
->with('tags')
->paginate(8);
Input::flash();
return View::make('home')->with('scripts', $scripts);
}
}
As you can see, I'm using (almost) the same big query twice. I would like to call the postSearch() function within the getHome() function and give the three parameters (query = '', sort_col = 'rating', sort_dir = 'desc') with it. Is this possible?
If you plan on using this frequently I would move this out of your controller and put it in your Model as a Custom Query Scope. This really doesn't have a place in the Controller even as a private function.
public function scopeRating($query)
{
return $query->select('script.*', DB::raw('COALESCE(SUM(vote.rating), 0) as rating'))
->leftJoin('script_vote as vote', 'vote.script_id', '=', 'script.id')
->with('tags')
->orderBy('rating', 'desc')
->orderBy('views', 'desc')
->groupBy('id');
}
This could then be called like this
Script::rating();
here are a few possibilities:
write a private function getScripts(...) within the controller (not so sexy)
add a getScripts(...) function on your Scripts model (so-lala sexy)
create a service provider to encapsulate the model(s) and inject them into the controller
Related
Have a query, how I can filter results by translation relation (by name column)
$item = Cart::select('product_id','quantity')
->with(['product.translation:product_id,name','product.manufacturer:id,name'])
->where($cartWhere)
->get();
my model
Cart.php
public function product($language = null)
{
return $this->hasOne('App\Models\Product','id','product_id');
}
Product.php
public function translations()
{
return $this->hasMany('App\Models\ProductTranslation','product_id','id');
}
Update v1.0
do like this, but query takes too long time
$item = Cart::select('product_id','quantity')
->with(['product.translation', 'product.manufacturer:id,name'])
->where($cartWhere)
->when($search,function ($q) use ($search) {
$q->whereHas('product.translation', function (Builder $query) use ($search) {
$query->where('name', 'like', '%'.$search.'%');
$query->select('name');
});
}
)
->get() ;
Inside the array within your with() method, you can pass a function as a value.
Cart::select('product_id','quantity')
->with([
'product', function($query) {
$query->where($filteringAndConditionsHere);
}
]);
https://laravel.com/docs/7.x/eloquent-relationships#eager-loading
I am trying to search multiple data from two related tables. To be specific I want to get only "name column" from the users table and the rest of the columns from the posts table. But whenever I tried to search it prints the following error "Trying to get property 'name' of non-object"
Below is my user model
<?php
namespace App;
use App\Mail\NewUserWelcomeMail;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\Mail;
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'name', 'email','phone', 'username', 'password',
'admin', 'address', 'description', 'approved_at',
];
protected $hidden = [
'password', 'remember_token',
];
public function posts()
{
return $this->hasMany(Post::class)->orderBy('created_at', 'DESC');
}
}
And post model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $guarded = [];
public function user()
{
return $this->belongsTo(User::class);
}
}
And my Controller
public function showcampaign(User $user) {
$q = Input::get( 'q' );
if( !empty( $q ) ) {
$showcampaign = User::join('posts','posts.user_id','users.id')
->where('name','LIKE','%'.$q.'%')
->orWhere('caption','LIKE','%'.$q.'%')
->orWhere('description','LIKE','%'.$q.'%')
->orWhere('duration','LIKE','%'.$q.'%')
->orWhere('amount','LIKE','%'.$q.'%')
->get();
if(count($showcampaign) > 0) {
return view('admin.campaignreport', ['show' => $showcampaign]);
} else {
return redirect('/campaignreport')->with('status', 'No Details found. Try to search again !');
}
} else {
$showcampaign = Post::all();
return view('admin.campaignreport')->with('show', $showcampaign);
}
}
Please help thanks
As you have already declared the relations within the Model So you can use whereHas and also orWhereHas
So
$showcampaign = SampleReception::query()
->whereHas('posts',function(\Illuminate\Database\Eloquent\Builder $query) use ($q){
return $query->where('caption', 'LIKE','%'.$q.'%')
->orWhere('description', 'LIKE','%'.$q.'%')
->orWhere('duration', 'LIKE','%'.$q.'%')
->orWhere('amount', 'LIKE','%'.$q.'%');
})
->orWhere('name','LIKE','%'.$q.'%')
->get();
For any issues leave a comment
Try.. use where instead of orwhere
$showcampaign = User::join('posts','posts.user_id','users.id')
->where('name','LIKE','%'.$q.'%')
->Where('caption','LIKE','%'.$q.'%')
->Where('description','LIKE','%'.$q.'%')
->Where('duration','LIKE','%'.$q.'%')
->Where('amount','LIKE','%'.$q.'%')->get();
I think you need to use a reference table for where clause.
$showcampaign = User::join('posts','posts.user_id', '=', 'users.id')
->where('users.name','LIKE', '%'.$q.'%')
->orWhere('posts.caption', 'LIKE','%'.$q.'%')
->orWhere('posts.description', 'LIKE','%'.$q.'%')
->orWhere('posts.duration', 'LIKE','%'.$q.'%')
->orWhere('posts.amount', 'LIKE','%'.$q.'%')
->get();
If you define relationship correctly then use:
$showcampaign = SampleReception::with(['posts' => function($query) use($q) {
return $query->where('caption', 'LIKE','%'.$q.'%')
->orWhere('description', 'LIKE','%'.$q.'%')
->orWhere('duration', 'LIKE','%'.$q.'%')
->orWhere('amount', 'LIKE','%'.$q.'%');
}])
->orWhere('name','LIKE','%'.$q.'%')
->get();
I use scope query to create search functions. It makes my code more flexible and easy to understand.
Here is how you can do this.
In your "user" Model, you have to write "scopeFunctionName". you can write any functionName with scope. And in your Controller, you can simply call this function whenever you need as functionName(). and pass here your search term from request as functionName(request(['search']))
And now as you have declared relations for these tables to search data from related tables, try using this code in your user Model
public function scopeFilter($query, array $filters)
{
/*
Hey $query! whenever u get 'search term' in URL, call the function.
If ['search'] exists or != null, then pass its value to the function
else return false and don't execute the $query. If value exists,
Then the function will execute the query and returns a result.
*/
$query->when(
$filters['search'] ?? false,
fn ($query, $search) =>
$query
->whereHas('users', function ($query) use ($search) {
return $query
->where('name', 'like', '%' . $search . '%')
})
->where('name', 'like', '%' . $search . '%')
->orWhere('caption', 'like', '%', . $search . '%')
->orWhere('description', 'like', '%', . $search . '%')
->orWhere('id', $search)
);
}
And in Controller, you can pass the value to the scope function as:
public function index()
{
return view('posts.index', [
'show' => Post::all()->filter(request(['search']))->with('user')
]);
}
Note: The code above has not been tested but it might solve your problem.
I am trying to pass the locale data to the eloquent query but it cannot get it. $locale value is taken from the URI.
class MyController extends Controller
{
public function index($locale = 'en')
{
$news = News::join('categories', 'categories.id', '=', 'news.catid')
->select('news.*', 'categories.category')
->where([['news.published', '1'], ['news.deleted', '0']])
->where(function ($query) {
$query->where('news.language', $locale) //$locale is not recognized naturally
->orWhere('news.language', 'all');
})
->orderBy('news.published_at', 'desc')
->take(4)
->get();
.
.
.
}
}
How can I pass $locale value into the subquery?
There is the use construct that you can use to create a closure around the variable:
->where(function ($query) use($locale) {
$query->where('news.language', $locale)
->orWhere('news.language', 'all');
})
See Example #3 in the manual, "Inheriting variables from the parent scope".
I often need to perform this query:
$Op = JobCardOp::where([
['JobCardNum', '=', $JobCardNum ],
['OpNum', '=', $OpNum ]
])->first();
So rather than writing this out every time I want a function like:
public function getOp($JobCardNum, $OpNum)
{
$Op = JobCardOp::where([
['JobCardNum', '=', $JobCardNum ],
['OpNum', '=', $OpNum ]
])->first();
return $Op;
}
That I can call in my controller. Where should I define my function, at the moment the I only need it in one controller but I may need it an another if thats possible. Any help appreciated.
You may define your function in JobCardOpt model as static:
public static function getOp($JobCardNum, $OpNum)
{
$Op = static::where([
['JobCardNum', '=', $JobCardNum],
['OpNum', '=', $OpNum]
])->first();
return $Op;
}
And use it like this in your controllers:
$jobCardOpt = JobCardOpt::getOp(1, 2);
You could put this method on your Model if you wanted to as a static function.
public static function getOp($cardNum, $opNum)
{
return static::where([
['JobCardNum', '=', $cardNum],
['OpNum', '=', $opNum]
])->first();
}
// controller
$res = YourModel::getOp($cardNum, $opNum);
Or add a query scope to the model
public function scopeGetOp($query, $cardNum, $opNum)
{
return $query->where([
['JobCardNum', '=', $cardNum],
['OpNum', '=', $opNum]
]);
}
// controller
$res = YourModel::with(...)->getOp($cardNum, $opNum)->first();
Kinda depends how you want to use it.
Hello everyone I'm trying to make pagination in Laravel 4 but my code doesn't work.
I have controller with action:
public function getSingleProduct($prodName, $id)
{
$singleProduct = Product::getOne($id);
$getAllReviews = Review::getAllBelongsToProduct($id);
$this->layout->content = View::make('products.single')
->with('reviews', $getAllReviews)
->with('products', $singleProduct);
}
and I want to paginate getAllReviews (5 per page). I tried like this:
$getAllReviews = Review::getAllBelongsToProduct($id)->paginate(5); but it doesn't work for me. Here is also my Review model
public static function getAllBelongsToProduct($id) {
return self::where('product_id', '=', $id)
->join('products', 'reviews.product_id', '=', 'products.id')
->select('reviews.*', 'products.photo')
->orderBy('created_at', 'desc')
->get();
}
Where I have a mistake?
Instead of that static method on your model use query scope, this will be flexible:
// Review model
public function scopeForProduct($query, $id)
{
$query->where('product_id', $id);
}
public function scopeWithProductPhoto($query)
{
$query->join('products', 'reviews.product_id', '=', 'products.id')
->select('reviews.*', 'products.photo');
}
Then use it:
// get all
$reviews = Review::forProduct($id)->withProductPhoto()->latest()->get();
// get paginated
$reviews = Review::forProduct($id)->withProductPhoto()->latest()->paginate(5);
latest is built-in method for orderBy('created_at', 'desc').
If you want to have just a single call in your controller, then chain the above and wrap it in methods on your model.