So, I have a table countries with a column flag.Here I have codes for every country by id.I tried to join the table in my page by SQL query, but I've got this...And I need to display only the right flag, by the id of country, not all the flags.
https://imgur.com/a/2F6Qvdv
My database structure : table countries , column id, country and flag
Here is my code :
Controller:
$data['contact_users'] = DB::table('contacts')
->join('users', 'users.id', '=', 'contacts.contact_id')
->join('industries', 'industries.id', '=', 'users.industry_id')
->join('countries', 'countries.id', '=', 'users.country_id')
->join('organization_types', 'organization_types.id', '=', 'users.organization_type_id')
->join("role_users", "role_users.user_id","=","users.id")
->leftJoin("career_paths", "career_paths.user_id","=","users.id")
->select('users.*', 'industries.industry', 'countries.country', 'organization_types.organization_type', "role_users.role_id", 'career_paths.function_id')
->where('contacts.contact_id', '!=', $id)
->where('users.deleted_at', '=', null)
->whereIn('contacts.user_id', $contact_id)
->whereNotIn('contacts.contact_id', $contact_id)
->whereNotIn('contacts.contact_id', $inviter_id)
->groupBy('contact_id')
->take(4)
->get();
$flags = DB::select('SELECT flag FROM countries WHERE country = "India"');
View:
#foreach ($flags as $flag)
{{ $contact->country }} <span class="{{ $flag->flag}}"></span>
#endforeach</a><br>
With {{ $contact->country }} I get the name of country and with {{ $contact->id }} I get the id of country.How can I make a conection between id and flag column?
// try this. you should have two tables here since you mentioned something like joining two tables. sample data and query.
countries: id | country | flagid
---|---------|----
1 | US | 1
flag: id | flag
----------
1 | image
//controller. this code is assume you are selecting/inputing the country name
public function getSingleFlag(Request $request)
{
$country = $request->input('countryname')
$data['country']=DB::table('countries')
->leftjoin('flag','countries.flagid','=','flag.id')
->where('country',$country)
->first();
return view('form',$data)
}
//this code is assume you are not selecting/inputing the country name. you just want to display all the countries with corresponding flags
public function getAllFlag()
{
$data['country']=DB::table('countries')
->leftjoin('flag','countries.flagid','=','flag.id')
->get();
return view('form',$data)
}
//your blade.
//for the first function
<p> {{ $country->country }} - {{ $country->flag}}</p>
//for the second function use foreach
#foreach($country as $c)
<p> {{ $c->country }} - {{ $c->flag}}</p>
#endforeach
note: remember to use your image tag to display your flag. just giving you an insight how it will be achieved
Related
i am using laravel 5.5 and am trying to show into a view the current data :
$data = DB::table('demandes')
->join('articles', 'articles.id', '=', 'demandes.article_id')
->join('users', 'users.id', '=', 'demandes.client_id')
->select('demandes.*', 'articles.nom_article', 'users.prenom')
->get();
i am trying to find how to make a foreach statement even thought its not the same table i am working with ,
here is the demande table :
table demandes in phpmyadmin
Few changes in query
$data = DB::table('demandes as D')
->select('D.*', 'A.nom_article', 'U.prenom')
->join('articles as A', 'A.id', '=', 'D.article_id')
->join('users as U', 'U.id', '=', 'D.client_id')
->get();
Foreach loop on view:
#foreach($data as $value)
{{ $value->nom_article }}
{{ $value->prenom }}
#endforeach
See if the same you are looking for.
your foreach is same , there is noeffect of table structure because your above query only add two column in single row
I have the following tables:
main
id
user_id
host_id
users
id
room_id
hosts
id
room_id
rooms
id
number
As you can see both users and hosts are connected with table rooms. Unfortunately users.room_number = 1, and hosts.room_number = 2. How can I create a query using leftJoin in laravel to distinguish between users.room_number and hosts.room_number? And then how I can refer to each room_number in my foreach loop?
I have something like this:
MainController.php
$main = DB::table('main')
->leftJoin('users', 'users.id', '=', 'main.user_id')
->leftJoin('hosts', 'hosts.id', '=', 'main.host_id')
->leftJoin('rooms as users_rooms', '=', 'rooms.id', 'users.room_id')
->leftJoin('rooms as hosts_rooms', '=', 'rooms.id', 'hosts.room_id')
->select('users_rooms.number as u_rooms_number', 'hosts_rooms.number as
h_rooms_number')
->get();
return view('main.index', ['main' => $index]);
main/index.blade.php
#foreach($main as $element)
{{ $element->u_rooms_number }}
{{ $element->h_rooms_number }}
#endforeach
Because of both leftJoin with 'rooms as users_rooms' and 'rooms as hosts_rooms' I get an Error "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'rooms.id' in 'on clause'".
You get the error message, because you join twice on the rooms table. Your on clause therefore cannot identify which table of the two is specified with 'rooms.id'.
To avoid the conflict you correctly renamed the table joins in your query. Therefore you can use the names as if they were the tables themself.
->leftJoin('rooms as users_rooms', '=', 'users_rooms.id', 'users.room_id')
->leftJoin('rooms as hosts_rooms', '=', 'hosts_rooms.id', 'hosts.room_id')
I'm having a problem with my Search functionnality on my website, I have 2 tables: user and review , In my review table, the owner column is equal to the username column in user table, I want to be able to return in the same result the username of the user table and just below the number of review which I can get with:
Review::where('owner', '=', xxx)->where('invitation_id', '')->count();
The xxx should be equal to the username in the user table
And I have to do this to get the username:
User::where('username', '=', xxx)->first();
What I would like to do (I know this is wrong):
$result = User::where('email','LIKE','%'.$search_key.'%')
->orWhere('username','LIKE','%'.$search_key.'%')
AND
Review::where('username', '=', *$result->username* )
->get();
And I would like to be able to return the search result like this in my result.blade.php:
<h3>Username: {{ user->username }}</h3>
<h3>Username: {{ review->number_review }}</h3>
I checked on the Laravel docs to make a relationship between these 2 tables but can't figure it out, I hope what I said is understandable.
You can use eloquent relationship.
// app/Review.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Review extends Model
{
public function users()
{
return $this->hasOne('App\User', 'owner', 'username');
}
}
I do not suggest two table relation with username/owner. I suggest to you relation with user primary_id
You can get user info with following code;
Review::where('owner', '=', xxx)->where('invitation_id', '')->with('users')->count();
It getting user info with ->with('users') condition in Review model.
You achieve the required matching criteria by using join and parameter grouping clause
$result = DB::table('users as u')
->join('review as r', 'u.username', '=', 'r.owner')
->where('email','LIKE','%'.$search_key.'%')
->orWhere(function ($query) {
$query->where('u.username','LIKE','%'.$search_key.'%')
->where('r.owner','LIKE','%'.$search_key.'%');
})
->get();
Which will produce where clause as
WHERE u.email LIKE '%somevalue%' OR (r.owner LIKE '%somevalue%' AND u.username LIKE '%somevalue%')
For review count
$result = DB::table('users as u')
->select('u.*',DB::raw("COUNT(*) as review_count"))
->join('review as r', 'u.username', '=', 'r.owner')
->where('u.email','LIKE','%'.$search_key.'%')
->orWhere(function ($query) {
$query->where('u.username','LIKE','%'.$search_key.'%')
->where('r.owner','LIKE','%'.$search_key.'%');
})
->groupBy('u.username')
->get();
You will need to join your user table to the review table.
Something along these lines, might need tweaking.
$result = User::query()
->join('review', 'owner', 'username')
->where('email','LIKE','%'.$search_key.'%')
->orWhere('username','LIKE','%'.$search_key.'%')
->orWhere('username', $result->username)
->orWhere('owner', $result->username)
->get();
I want to paginate ordering by linkscount em ASC...
The table pages:
----------------------
id | name
----------------------
1 | Globo
----------------------
2 | Google
----------------------
3 | MC Donalds
----------------------
4 | Habibs
The query to FIRST PAGE RESULTS (order/query is OK):
$pages = Mypages::where('pages.author_id', auth()->user()->id)
->orderBy('linkscount', 'desc')
->leftJoin('links', 'links.page_id', '=', 'pages.id')
->selectRaw('pages.*, count(links.id) as linkscount')
->groupBy('pages.id')
->take(2)
->get();
The query TO PAGINATE (order/query is not ok):
$pages = Mypages::where('pages.author_id', auth()->user()->id)
->orderBy('linkscount', 'asc')
->leftJoin('links', 'links.page_id', '=', 'pages.id')
->selectRaw('pages.*, count(links.id) as linkscount')
->groupBy('pages.id')
->where('pages.id', '>', $id)
->take(2)
->get();
This query, don't return nothing, it was to return "MC Donalds"...
$id = LAST ID DISPLAYED.
Like Jacob H said in the comment, this isn't a real SQL, because the group by function need to have all the columns of the query that you have in the select except the functions like counts, sums, etc.
For this example you need to put the pages.name and pages.id in the group by
$pages = Mypages::where('pages.author_id', auth()->user()->id)
->orderBy('linkscount', 'asc')
->leftJoin('links', 'links.page_id', '=', 'pages.id')
->selectRaw('pages.id, pages.name, count(links.id) as linkscount')
->groupBy('pages.id', 'pages.name')
->where('pages.id', '>', $id)
->take(2)
->get();
And if you are sending the correct $id, everything seems ok.
I have the following db structure:
items:
id, name, user_id
users table:
id, name
user_favorites table:
id, user_id, item_id
On my items permalink pages, I have an 'Add to favorites' button which inserts a new row into user_favorites
I want to be able to replace it for a 'Remove from favorites' button if the user already has it in their favorites.
I can't figure out the logic behind this - do I need to check if a row exists in user_favorites that has the current user's id and the permalink item id? This did not work for me:
if (Auth::user()->id) {
if (!is_null(DB::table('user_favorites')->where('user_id', '=', Auth::user()->id)->where('item_id', '=', $item->id)->first())) {
// remove from favorites button will show
}
}
You may want something like this:
$user_favorites = DB::table('user_favorites')
->where('user_id', '=', Auth::user()->id)
->where('item_id', '=', $item->id)
->first();
if (is_null($user_favorites)) {
// It does not exist - add to favorites button will show
} else {
// It exists - remove from favorites button will show
}
I advise you to use exists() or count() to check, not use first().
The fastest way:
$result = DB::table('user_favorites')
->where('user_id', '=', Auth::user()->id)
->where('item_id', '=', $item->id)
->exists();
Or:
$result = DB::table('user_favorites')
->where('user_id', '=', Auth::user()->id)
->where('item_id', '=', $item->id)
->count();
SQL:
select count(*) as aggregate from `user_favorites` where *** limit 1
The faster way: only select id
$result = DB::table('user_favorites')
->where('user_id', '=', Auth::user()->id)
->where('item_id', '=', $item->id)
->first(['id']);
SQL:
select id from `user_favorites` where *** limit 1
The normal way:
$result = DB::table('user_favorites')
->where('user_id', '=', Auth::user()->id)
->where('item_id', '=', $item->id)
->first();
SQL:
select * from `user_favorites` where *** limit 1
Let User_favorite be a model that accesses your user_favorites table
$result = User_favorite::where('user_id',Auth::getUser()->id)
->where('item_id',$item->id)
->first();
if (is_null($result)) {
// Not favorited - add new
User_favorite::create(['user_id'=>Auth::getUser()->id,'item_id'=>$item->id]);
} else {
// Already favorited - delete the existing
$result->delete();
}
The simplest way to do is to use toggle() method of many-to-many relationship.
e.g.
$user->roles()->toggle([1, 2, 3]);
The many-to-many relationship also provides a toggle method which
"toggles" the attachment status of the given IDs. If the given ID is
currently attached, it will be detached. Likewise, if it is currently
detached, it will be attached
It also returns an array which tells you if the ID is attached or detached in DB.