How can I get data from database in laravel? - php

I want to be able to get data from database.
I need table named role_users, column role_id
and table functions, column function.
Here is my View:
#foreach($contact_users as $contact)
{{ $contact->username }}</h3>
<p>
<strong><i class="icon-real-estate-020 u-line-icon-pro"></i> : </strong>{{ $contact->country }}<br>
<strong><i class="icon-screen-tablet fa-" aria-hidden="true"></i> : </strong>{{ $contact->industry }}<br>
<strong><i class="icon-frame fa-" aria-hidden="true"></i> : </strong>{{ $contact->organization_type }}<br>
#endforeach
and here is my 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')
->select('users.*','industries.industry','countries.country','organization_types.organization_type')
->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();
I want to add function below organization_type.Thank you.

Why are you using DB query joins?
You need to define your Models first then you can have clearer queries using Eloquent. Please look at the documentation for more.
(https://laravel.com/docs/5.8/eloquent)https://laravel.com/docs/5.8/eloquent

Related

How to get the index of the field from my array properly in Laravel blade?

This is the "show" function in my Controller :
public function show($nform)
{
$plan = PlanFormation::findOrFail($nform);
$plan_props = PlanFormation::select(
'clients.raisoci',
'clients.nrc_entrp',
'intervenants.nom',
'intervenants.prenom')
->join('clients', 'clients.nrc_entrp', '=', 'plan_formations.nrc_e')
->join('intervenants', 'intervenants.id_interv', '=', 'plan_formations.id_inv')
->where('plan_formations.n_form', '=', $nform)
->get();
return view('planformation.detail', ['plan_props' => $plan_props, 'plan' => $plan]);
}
Here is my Blade :
<div class="card-header">
<a class="btn btn-dark btn-sm bu-lg-ic" href="/planformation"><i class="fa fa-arrow-left"></i></a>
<h3 class="card-title card-h3">Plan N° {{ $plan->n_form }} > {{ $plan_props['raisoci'] }}</h3>
</div>
I got "Undefined index 'raisoci' when I use $plan_props['raisoci'] in the blade
In your example $plan_props is not an array with element ['raisoci'] but a collection of items. So you can get the first element of this collection and call its 'raisoci' property:
{{ $plan_props[0]['raisoci'] }}
or
{{ $plan_props[0]->raisoci }}
You may also call first() method instead of get() in your query. In this case you'll get exactly what you expected in your example:
$plan_props = PlanFormation::select(...)
...
->where('plan_formations.n_form', '=', $nform)
->first();
It worked when I used ->first() and I left {{ $plan_props['raisoci'] }} as it's
The third answer is the correct answer
Thank you Manssor for your help

How to get a specific value from a table in a view when you have conflicting names[Laravel]

Im making an application for a non-profit organisation for a school project and we are using laravel, the first time for me
I tried {{$instructiekaart->instructiekaarten.name}} but I get an error message when using this code
Controller
$instructiekaarten = DB::table('instructiekaarten')
->join('instructiekaart_set', 'instructiekaart_set.instructiekaart_id' ,'=', 'instructiekaarten.id')
->join('sets', 'sets.id' ,'=', 'instructiekaart_set.set_id')
->join('instructiekaart_stap', 'instructiekaart_stap.instructiekaart_id' ,'=', 'instructiekaarten.id')
->join('stappen', 'stappen.id' ,'=', 'instructiekaart_stap.stap_id')
->join('instructiekaart_user', 'instructiekaart_user.instructiekaart_id' ,'=', 'instructiekaarten.id')
->join('users', 'users.id' ,'=', 'instructiekaart_user.user_id')
->join('instructiekaart_niveau', 'instructiekaart_niveau.instructiekaart_id' ,'=', 'instructiekaarten.id')
->join('niveaus', 'niveaus.id' ,'=', 'instructiekaart_niveau.niveau_id')
->select('sets.name','instructiekaarten.name', 'stappen.name', 'niveaus.name')
->get();
return view('instructiekaarten.index')->with('instructiekaarten', $instructiekaarten);
View
#if(count($instructiekaarten) > 0)
#foreach($instructiekaarten as $instructiekaart)
<div class="card">
<a href="/instructiekaarten">
<h5 class="card-header">{{$instructiekaart->instructiekaarten.name}}</h5>
</a>
<div class="card-body">
<h5 class="card-title"></h5>
<p class="card-text"></p>
</div>
</div>
#endforeach
#else
<p>Geen instructiekaarten gevonden</p>
#endif
This is the error message I get when using {{$instructiekaart->instructiekaarten.name}}
Undefined property: stdClass::$instructiekaarten (View:
D:\wamp64\www\TZC\resources\views\instructiekaarten\index.blade.php)
Try Giving alias
->select('sets.name AS sets_name','instructiekaarten.name AS instru_name', 'stappen.name AS stappen_name', 'niveaus.name AS niveaus_name')
& then
{{ $instructiekaart->instru_name }}

get unique Tasks From relation

I have relation tags that have many tasks whet I get data from foreach I get duplicated values
in example
in my path i have 2 tags PHP ,HTML5
PHP Has [PHP Task_1 , PHP_Task_2]
HTML5 Has [HTml5 Task_1,PHPTask_2]
because task has many tags so i will get to duplicated PHP_Task_2
i need to get every task just one time
My Controller
$posts2 = Path::with(['pathtags' => function ($q) use ($TagArray)
{$q->with(['Tasks' => function ($q) use ($TagArray) {$q->has('tasktags', '=', 2)
->with('tasktags');
}]);
}])->where('id', '=', 1)->get();
My Blade
#foreach ($posts2 as $item)
<h2> {{$item->name}}</h2>
#foreach ($item->pathtags as $Tag)
<li> Path Tag :: {{ $Tag->name }} </li>
#foreach ($Tag->Tasks as $Task)
<li> Task :: {{ $Task->task_name }} </li>
#foreach ($Task->tasktags as $TaskTag)
<li> Task Tags :: {{ $TaskTag->name }} </li>
#endforeach
#endforeach
#endforeach
#endforeach

Laravel schema sql

so simply i want to select user name from table users AS created_by, but after i put this {{ $result->created_by }} in blade tamplate it shows this error "Undefined property: stdClass::$created_by (View: /var/www/hots/resources/views/builds.blade.php)". Please help me.
Schema
public function index() {
$builds = DB::table('Build_Talents')
->join('Builds', 'Builds.BuildID', '=', 'Build_ID')
->join('BuildTalents', 'BuildTalents.TalentsID', '=', 'Talents_ID')
->join('Heroes', 'Builds.hero', '=' ,'Heroes.HeroID')
->join('users', 'Builds.created_by', '=', 'users.id')
->select('Heroes.icon', 'Heroes.name',
DB::RAW('Builds.name AS build', 'users.name AS created_by')
)
->get();
return view('builds', compact('builds'));
}
Blade
#extends('home')
#section('content')
#if($builds)
#foreach($builds as $result)
<div class="col-md-12">
<div class="builds">
<img src="{{ $result->icon }}" alt="Build icon" />
<h2>{{ $result->name }}-{{ $result->build }}</h2>
<i>{{ $result->created_by }}</i>
</div>
</div>
#endforeach
#else
<p>Nic nenalezeno</p>
#endif
#endsection
Instead this:
DB::RAW('Builds.name AS build', 'users.name AS created_by')
use this:
DB::raw('Builds.name AS build, users.name AS created_by')
raw() has only one parameter and you are using two -
Api.

Laravel join query returns Null while raw query has a value

I'm stuck with a really stupid query.
I tried to find the solution for like days, but nothing helped and so I don't find the answer.
I want to get the newest reaction from a table, to display the username.
First of all, these are my Database schemes:
comments (Has a model Comment)
threads (Has a model Thread)
So My raw query looks like this:
select `comments`.`username` from `comments` inner join `threads` on `comments`.`tid` = `threads`.`tid` where `comments`.`deleted_at` is null and `threads`.`cid` = '$categorie->id' order by `comments`.`posted_at` desc limit 1
A var_dump() of $categorie->id returns int(3) where the 3 stands for the categorie number.
When I execute the query (raw) in navicat, it returns this:
What is good, since that is the corract value it needs to return.
However when I rebuild this query in "Laravel-eloquent-style", the query looks like this:
Comment::join('threads', 'comments.tid', '=', 'threads.tid')->where('threads.cid', '=', $categorie->id)->orderBy('comments.posted_at', 'DESC')->pluck('comments.username')
Be aware of that the query is build in a foreach loop and has an if-statement before. But that will be sown later on.
This does return nothing.
When I inspect elements, I just got nothing.
I tried the DB::select(DB::raw('query')), but that doesn't work either.
I render my page in the ForumController:
public function index()
{
$forums = Forums::orderBy('disp_order', 'asc')->get();
$categories = Categorie::orderBy('disp_order', 'asc')->get();
return View::make('index')->with('forums', $forums)->with('categories', $categories);
}
This works fine, and the view looks like this:
#foreach($forums as $forum)
<div class="panel-group col-sm-12">
<div class="panel panel-default">
<div class="panel-heading" style="background-color: {{ $forum->color }};">
<i class="fa fa-folder-open-o fa-fw"></i>
<strong>{{ $forum->name }}</strong>
</div>
<div id="forum4" class="panel-collapse collapse in">
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th></th>
<th>Forum naam</th>
<th class="text-right">Topics</th>
<th class="">Laatste post info</th>
</tr>
</thead>
<tbody>
<tr>
#foreach($categories as $categorie)
#if($categorie->fid == $forum->fid)
<td class="topic-marker-forum">
<i class="fa fa-comments fa-3x"></i>
</td>
<td class="col-md-6 col-sm-6">
<div><strong>{{ $categorie->name }}</strong></div>
<div class=""><em>{{ $categorie->description }}</em></div>
</td>
<td class="col-md-1 col-sm-1 text-right"><span class="badge">{{ Thread::where('cid', '=', $categorie->id)->remember(15)->count() }}</span></td>
<td class="col-md-4 col-sm-4 ">
<div>
#if(Thread::where('cid', '=', $categorie->id)->exists() && Comment::join('threads', 'comments.tid', '=', 'threads.tid')->where('threads.cid', '=', $categorie->id)->orderBy('comments.posted_at', 'DESC')->exists())
{{ Helper::HTMLFilter(Thread::where('cid', '=', $categorie->id)->orderBy('date_posted', 'DESC')->pluck('title')) }}<br>
<i class="fa fa-clock-o"></i> {{ \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', Comment::join('threads', 'comments.tid', '=', 'threads.tid')->where('threads.cid', '=', $categorie->id)->orderBy('comments.posted_at', 'DESC')->pluck('posted_at'))->format('d/m/Y H:i') }}<br>
<i class="fa fa-user"></i> {{ Comment::join('threads', 'comments.tid', '=', 'threads.tid')->where('threads.cid', '=', $categorie->id)->orderBy('comments.posted_at', 'DESC')->pluck('comments.username') }}
#else
<b>-</b>
#endif
</div>
</td>
</tr>
#endif
#endforeach
</tbody>
</table></div>
</div>
</div>
</div>
#endforeach
The strangest part is that this:
{{ \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', Comment::join('threads', 'comments.tid', '=', 'threads.tid')->where('threads.cid', '=', $categorie->id)->orderBy('comments.posted_at', 'DESC')->pluck('posted_at'))->format('d/m/Y H:i') }}
Just works. Like nothing is wrong with that, when I call that item, it returns the correct value.
My Models are just normal;
<?php
use Illuminate\Database\Eloquent\SoftDeletingTrait;
class Comment extends Eloquent
{
use SoftDeletingTrait;
protected $dates = ['deleted_at'];
protected $table = 'comments';
public $timestamps = false;
public function user()
{
return $this->belongsTo('User', 'uid');
}
}
And
<?php
use Illuminate\Database\Eloquent\SoftDeletingTrait;
class Thread extends Eloquent
{
use SoftDeletingTrait;
protected $dates = ['deleted_at'];
protected $table = 'threads';
public $timestamps = false;
}
Hope someone can help me out!
Try something like this
Comment::with(['threads'=>function($query) use ($categorie){
$query->where('threads.cid',$categorie->id)
->whereNull('comments.deleted_at');
}])
->orderBy('comments.posted_at', 'DESC')
->limit(1)
->pluck('comments.username');
If you need - were 'deleted_at' not null
->whereNotNull('comments.deleted_at');
DB::table('coments')
->join('threads', 'comments.tid', '=', 'threads.tid')
->where('threads.cid',$categorie->id)
->whereNull('comments.deleted_at');
->orderBy('comments.posted_at', 'DESC')
->limit(1)
->pluck('comments.username');

Categories