I have a movies website that I want to allow people to search by genre
movies.com/people/action/genre
The Route
Route::get('people/{genre}/genre', array('uses' => 'ActorController#genre', 'as' => 'people.genre'));
The ActorController#genre
public function genre()
{
$genre=Input::get('genre');
$actors = $this->actor->allgenre($genre);
return View::make('Actor.All')->withActors($actors);
}
This grabs all the actors from the db
function allGenre($genre)
{
return $this->actor->where('genre', 'like', '$genre')->orderBy('views', 'desc')->paginate(24);
}
This is returning no results, when it should be returning results because if I go
function allGenre($genre)
{
return $this->actor->where('genre', 'like', 'action')->orderBy('views', 'desc')->paginate(24);
}
Results show up
When you use that routing, the $genre variable would be bound the the controller:
public function genre($genre)
{
$actors = $this->actor->allgenre($genre);
return View::make('Actor.All')->withActors($actors);
}
While in your previous question you used query strings, so you needed Input::get('key'), now you changed the url, and you don't use resource controllers anymore, so you must go back to the "usual" way
I got it to work
function allGenre($genre)
{
return $this->actor->where('genre', 'like', $genre)->orderBy('views', 'desc')->paginate(24);
}
Apparently Laravel doesn't accept '$genre' with the ' around the variable
Related
i want to multi filter data in laravel but i show this error:
Too few arguments to function Illuminate\Support\Collection::get()
Please help me to solve this issue.
public function searchLanding(Request $request)
{
$landings = Landing::all();
if(count($landings) && !is_null($request->title)) {
$landings = $landings->where("name", "LIKE", "%{$request->title}%")->get();
}
if (count($landings) && !is_null($request->start_at)) {
$landings = $landings->where('start_at', '>=', $request->start_at)->get();
}
if (count($landings) && !is_null($request->end_at)) {
$landings = $landings->where('end_at', '<=', $request->end_at)->get();
}
}
public function searchLanding(Request $request)
{
$landings = Landing::query();
if(!is_null($request->title)) {
$landings->orWhere("name", "LIKE", "%{$request->title}%");
}
if (!is_null($request->start_at)) {
$landings->orWhere('start_at', '>=', $request->start_at);
}
if (!is_null($request->end_at)) {
$landings->orWhere('end_at', '<=', $request->end_at);
}
return $landings->get();
}
Note:
You shouldn't call all() or get() when you are still building your query, only call them when you want to get the result.
Use where() when you want all conditions to be true,
or use orWhere() when you want one of the conditions to be true.
In the example above, only one of the conditions needs to be true e.g. search found in title or after start_at or before end_at.
I have this query in my codebase,
$listings = Tag::has('listings')->with(['listings' => function ($query) use ($request) {
$query->where('moderated', 1)
->where('active', 1);
if($request->query('free') == true) {
$query->where('cost', '0.00');
}
if($request->query('type') != "") {
$query->with(['types' => function($q) use ($request) {
$q->whereIn('id', explode(",", $request->query('type')));
}]);
}
$query->with('primaryImage');
}])
->paginate(3);
What I am trying to do is add parts of the query based on what is in the GET request (this bit works), what isnt working is the query on a relation.
Here I am querying Tags that can have many listings, each listing can have many types and I want to only return tags that have listing that match the filter parameters, i.e only show listings that cost "0.00" and then only tags that have listings that match the types in the get request.
So if a user sends type=1,2,3 in the GET request I want to return tags that have listings where the types relationship contains 1 of those IDs, is this possible?
The types relationship on a listing looks like this,
public function types() {
return $this->belongsToMany('App\Type');
}
and the relation from type to listing looks like this,
public function listings() {
return $this->belongsToMany('App\Listing');
}
Instead of using has and with maybe you can use whereHas
$listings = Tag::whereHas('listings', function($query){
$query->where('moderated', 1)
->where('active', 1);
if($request->query('free') == true) {
$query->where('cost', '0.00');
}
if($request->query('type') != "") {
$query->whereHas('types', function($q) use($request){
$q->whereIn('id', explode(",", $request->query('type')));
});
}
$query->with('primaryImage');
});
As you said, "each listing can have many types"
Change this in your Listing::class
public function types() {
return $this->hasMany('App\Type');
}
I want to show variables value in blade file. Below is the controller code:-
public function ClientListReviews()
{
// to show client list get data from users booking table
$select_clientlist =
DB::table('users_booking')>where('service_provider', '=', 1)->get();
if(count($select_clientlist) > 0)
{
return view('client-database')->with($select_clientlist);
}
else
{
return view('client-database')->withMessage('No Details Found');
}
}
I want to show the values coming in $select_clientlist variable. Below is the code in my blade file:-
#foreach($select_clientlist as $clientlist)
{{$clientlist->firstname}}
#endforeach
And below is the route file code:-
Route::post('client_list_ajax','ClientDatabase\ClientdatabaseController#ClientListReviews');
I am receiving error.
What am I doing wrong?
Pass the variable using compact method
return View::make('myviewfolder.myview', compact('view1','view2','view3'));
view1,view2,view3 are variable names
Since you only pass the variable when there is records wrap your for each inside isset
if (isset($select_clientlist)) {
foreach($select_clientlist as $clientlist) {
}
}
your query should be like this . you may be forget SELECT statement
$select_clientlist = DB::table('users_booking')->select('*')->where('service_provider', '=', 1)->get();
Either use as
return view('client-database')->with('select_clientlist',$select_clientlist);
Or
return view('client-database',compact('select_clientlist'));
Also add in select_clientlist else part to prevent undefined error
public function ClientListReviews()
{
// to show client list get data from users booking table
$select_clientlist =
DB::table('users_booking')>where('service_provider', '=', 1)->get();
if(count($select_clientlist) > 0)
{
return view('client-database')->with('select_clientlist',$select_clientlist);
}
else
{
$select_clientlist = [];
return view('client-database')->with('select_clientlist',$select_clientlist)->withMessage('No Details Found');
}
}
OR check by isset($select_clientlist) in blade file
$__currentLoopData = isset($select_clientlist)?$select_clientlist:[];
Pass that variable to your view either way .. it should be a collection. If there are no records, it is just empty. The foreach wont run if its empty. Its as simple as that. No need to check if anything is set or is empty etc... just always pass that collection.
public function ClientListReviews()
{
$select_clientlist = DB::table('users_booking')->where('service_provider', 1)->get();
$view = view('client-database', compact('select_clientlist'));
if ($select_clientlist->isEmpty()) {
$view->with('message', 'No Details Found');
}
return $view;
}
Here's my function to load submissions created by a user.
public function viewSubs()
{
$user = User::find(Input::get('id'));
$submissions = Submission::find($user)->sortByDesc('created_at');
$submissions->load('user')->load('votes')->load('suggestions.votes');
return view('submissions.index' , compact('submissions'));
}
This returns with an error
Call to a member function load() on null
when there are no records on the submission.
How to handle if there are no submission on the DB?
Just check if its null first using an if statement:
public function viewSubs()
{
$user = User::find(Input::get('id'));
if ($submissions = Submission::find($user)->sortByDesc('created_at')) {
$submissions->load('user')->load('votes')->load('suggestions.votes');
}
return view('submissions.index' , compact('submissions'));
}
Also, depending on your DB structure I'm pretty sure you can cut out a lot of the code by utilising your models' relationships by doing something like this:
$user = User::find(Input::get('id'))
->with(['submissions' => function($query) {
$query->orderBy('created_at', 'asc');
}, 'submissions.votes', 'submissions.suggestions.votes']);
Then pass the $user variable to the view, or:
$submissions = Submission::with('user', 'votes', 'suggestions.votes')
->where('user_id', Input::get('id'))
->sortByDesc('created_at')
->first();
Not entirely sure the code will work perfectly, but I'm sure you can tweak it. The point is your code can be a lot shorter and still/or more readable by using relationships you've already set up.
Keep in mind I have minimal SQL experience, so I may be doing it all wrong.
I'm trying to set up search for a table but simply searching for the string a user enters is not ideal because though something may come up for "steve", if a user types "steve jobs story", nothing will match if that doesn't exist, as it'll be looking for that whole string instead of looking for each word respectively.
I tried to break it down by exploding the string into an array and then imploding that and separating it with %'s, so a search like "steve jobs story" would be "steve%jobs%story", and with my query it would be "%steve%jobs%story%". When I try searching this, I get no error but the query finds nothing, whereas when I just type one word it works, so I'm guessing what I'm trying to do isn't allowed.
Can someone point me in the right direction? Here is my controller/model:
// Controller
$query = $request->get('q');
$explodedQuery = explode(" ", $query);
$newQuery = implode("%", $explodedQuery);
$articles = Article::isLikeTitle($newQuery)
->isLikeBody($newQuery)
->latest('published_at')
->published()
->paginate(10);
// Model
public function scopeIsLikeTitle($query, $q)
{
return $query->where('title', 'LIKE', "%$q%");
}
public function scopeIsLikeBody($query, $q)
{
return $query->where('body', 'LIKE', "%$q%");
}
You can use loop to append the query with orWhere like this
$query = $request->get('q');
$explodedQuery = explode(" ", $query);
$articles = Article::isLikeTitle($explodedQuery)
->isLikeBody($explodedQuery)
->latest('published_at')
->published()
->paginate(10);
// Model
public function scopeIsLikeTitle($query, $q)
{
foreach($q as $eachQueryString)
{
$query->orWhere('title', 'LIKE', '%'.$eachQueryString .'%');
}
return $query;
}
public function scopeIsLikeBody($query, $q)
{
foreach($q as $eachQueryString)
{
$query->orWhere('body', 'LIKE', '%'.$eachQueryString .'%');
}
return $query;
}