i have some table,one of them is user table that i want fetch id from this table by searching username and second one is prize_info table that i want to fetch user_id from here by searching a prize,after all i want fetch just prize_info.user_id and user.id.what is the best way for this search part in laravel4?? here is my codes
$search = '%'.Input::get('keywords').'%';
$pages = DB::table('user')
->select('user.id','user.username')
->where('username', 'LIKE', $search)->get();
$blogitems = DB::table('prize_info')
->select('prize_info.id', 'prize_info.prize_name', 'prize_info.user_id')
->where('prize_name', 'LIKE', $search)->get();
what is your idea about above codes?????
and i check the result by this line but it does not work!
$results = $pages->union($blogitems)->take(30)->get();
please help me!best regard
I would advise you used Laravel's Eloquent ORM Relationships to do this. You can easily fetch related record using the available relationships;
One-to-One
One-to-Many
BelongsTo
BelongsToMany
HasMany
HasOne
Polymorphic Tables (using pivot tables)
You can use this:
$search = '%'.Input::get('keywords').'%'
$results = DB::table('user')
->select('user.id','user.username','prize_info.id', 'prize_info.prize_name', 'prize_info.user_id')
->leftJoin('prize_info','user.id','prize_info.user_id')
->take(30)
->get();
Related
Say I have the following tables:
event_relationships
id
user_id
relationship
primary_event_id
secondary_event_id
events
id
user_id
name
color
In my user model, I have my relationship setup like:
$this->hasMany(EventRelationships::class, 'user_id');
And each event relationship belongs to one user.
How can I use Laravel Eloquent to get a list of all the event relationships, but also get each event's name and color? Matching on the primary_event_id and secondary_event_id.
Is this possible with one query?
In pure eloquent, in the laravel way, that would be :
$eventRelationships = EventRelationships::with('primaryEvent', 'secondaryEvent')->get();
foreach($eventRelationships as $eventRelationship){
$primary_event_name = $eventRelationship->primaryEvent->name;
$secondary_event_name = $eventRelationship->secondaryEvent->name;
}
but it's in 2 queries. If you want only one query, you have to use a join :
$eventRelationships = EventRelationships::query()
->join('events as primary_events', 'events.id', 'event_relationships.primary_event_id')
->join('events as secondary_events', 'events.id', 'event_relationships.secondary_event_id')
->select('event_relationships.*', 'primary_events.name as primary_event_name', 'secondary_events.name as secondary_event_name')
->get();
foreach($eventRelationships as $eventRelationship){
$primary_event_name = $eventRelationship->primary_event_name;
$secondary_event_name = $eventRelationship->secondary_event_name;
}
It might be easier to use the first one as you manipulate eloquent object
I have 3 SQL tables.
clients
events
client_events
Because a client can have multiple events, I made the third table to show those relationships. I am using the following code to retrieve all of the clients that have the have a record matching this event, but it is only returning 1 record when there are multiple.
$eventHosts = DB::table('clients')->whereIn('id', function($query) {
$query->select('client_id')->from('client_events')->where('event_id', '=', explode('/', $_SERVER['REQUEST_URI'])[2]);
})->get();
What am I overlooking?
You can fetch the ids first, then pass to the whereIn query.
$clientIds = DB::table('client_events')
->where('event_id', explode('/', $_SERVER['REQUEST_URI'])[2])
->pluck('client_id')
->toArray();
$eventHosts = DB::table('clients')->whereIn('id', $clientIds)->get();
To get the results in a single query and more efficeintly, try join
$eventHosts = DB::table('clients') //select your main table
->join('client_events','client_events.client_id','=','clients.id') //join it on related columns
->where('client_events.client_id',explode('/', $_SERVER['REQUEST_URI'])[2])) //apply your condition on client_events
->get();
I have many to many relationship. In postman I pass find_id and status.
My query is like: UPDATE finds_user SET status = TRUE WHERE find_id = 2 AND user_id = 1.
I do it now like that:
DB::table('finds_user')
->where('find_id', '=', $request->find_id)
->where('user_id', '=', $user->id)
->update(['status' => $request->status]);
How I can do it with Eloquent instead of DB Query Builder?
Thank you a lot!
we have tow condition for update:
1- find_id = 2
2- user_id = 1.
suppose the relation name in the user model called 'finds'
the query will look like this:
User::find(1)->finds()->newPivotQuery()->where('find_id',2)->update(['status'=>true])
and if you have a model for the pivot table with name like "FindUser":
FindUser::where('user_id',$user->id)->where('find_id',2)->update(['status'=>true]);
I have M:N relationship between tables handymen and categories. So, pivot table is category_handyman. How to fetch all handymen data, who have category_id=1 in pivot table? I wanted to do something like this: (but this doesnt work)
$handymen = Handyman::with('categories')
->where('category_id', 1)
->get();
You can use whereHas() method to filter on related records:
$handymen = Handyman::whereHas('categories', function($query) {
$query->whereId(1);
})->get();
I have two tables Candidates & Qualifications with a joining pivot table called Results.
What I want to do is select all candidates for this centre and filter them by a value in the pivot table.
So select all candidates from centre 1 where the pivot “status” = “REGISTERED”
I have tried numerous approaches and am currently with the one below but nothing is working.
$candidates = Candidate::where('batch_centre_id','=', $id)->with(array('qualification' => function($q)
{
$q->wherePivot('status','=', 'REGISTERED');
}))->get();
Any advice would be much appreciated
In Eloquent wherePivot method can only be loaded on a relation. The way you're doing it, you're calling it on qualifications relation, therefore you're fetching all candidates and then, for each of them, you're loading qualifications that have status=REGISTERED. That's why you're getting all candidates.
You could work around it by using Eloquent's whereHas() method that let's you filter the base model you're trying to fetch by attributes of some relation. In your case the following should work:
$candidates = Candidate::where('batch_centre_id','=', $id)
->whereHas('qualifications', function($q) {
$q->wherePivot('status','=', 'REGISTERED');
})->with('qualifications')->get();
This way you'll get only candidates that have a qualification with status=REGISTERED and all their qualifications. If you want to additionally filter loaded qualifications by status, add the constraint like you did in your code:
$candidates = Candidate::where('batch_centre_id','=', $id)
->whereHas('qualifications', function($q) {
$q->wherePivot('status','=', 'REGISTERED');
})->with(array('qualifications', function($q) {
$q->wherePivot('status','=', 'REGISTERED');
})->get();