database fetch value show in dropdown using laravel 4 - laravel-4

hello I am in big trouble please help me about database fetch value show in drop down using laravel 4 my code is below
<?php //echo "<pre>"; print_r($user);exit; ?>
{{ Form::select($user, $user ) }}
in print_r($user); showing all database value fetch from database but i want to show only value = id , user name field I try for each but not working any idea how to show value=id , user name filed
and result showing in to the drop down like this
<select name="[{"id":"1","username":"skaka","name":"sandip","avatar_id":"1","gender":"1","dob":"1989-10-20","grade":"A","school":"kbpv","created_at":"2014-07-22 08:45:59","updated_at":"2014-07-22 08:48:04","parent_email":""},{"id":"2","username":"ttt","name":"test2","avatar_id":"2","gender":"2","dob":"1989-10-20","grade":"A","school":"kbpv1","created_at":"2014-07-22 08:58:25","updated_at":"2014-07-22 08:58:25","parent_email":""}]">

In order to use {{Form::select()}} the easiest way to achieve this is by calling your data from the database with lists() method.
So
$users = User::lists('username', 'id');
Now you have an array with data like this:
$users = [1 => 'skaka', 2 => 'ttt', ....];
So you are ready to use efficiently Form::select() by doing:
{{Form::select('dropdown_name', $users)}}

Related

Retrieving data from table with multiple row

I am currently having trouble retrieving the data from a table based on their id. Here the $content will pluck multiple ids from the table. When retrieving the ModuleDevelopment table, it retrieves all of the data without organizing by their id. What should I do to separate the status based on their id?
$content = ModuleListing::where('sme_role', $selectedRole)
->get()->pluck('mit_id');
$stats = ModuleDevelopment::whereIn('mdev_mit_id', $content)
->orderBY('mdev_mit_id','desc')->get()->pluck('mdev_status');
result of $stats
result of $content
Just an update of the coding I previously had trouble with, the main issue was to organize the status of each module development based on their ID,
therefore, the code I had was
this was at my controller:
$moduleDev = ModuleDevelopment::select('mdev_mit_id','mdv_sts')
->whereIn('mdev_mit_id', $content)
->orderBY('mdev_mit_id', 'asc')
->get();
and this is what was on my blade.php
#foreach($moduleDev as $modDev)
#if($listing->mit_id == $modDev->mdev_mit_id)
#if($modDev->mdv_sts == 'complete' )
{{'Complete'}}
#elseif($modDev->mdv_sts == 'incomplete')
{{'Incomplete'}}
#endif
#endif
#endforeach
I hope it is right and helpful for other people.

Laravel - Get users name from ID

I have a Laravel 5.6 app that is logging various information into a database table, I am then displaying this data in a HTML view. I am calling this view like this...
$logs = Log::all();
return view('logreport.index')->with('logs', $logs);
The table only contains the user ID, is there a way to get the users first name and last name for the users table? Or do I need to set a relationship up?
if the log table contain the user_id and you want to get user name you can get the user name through relation as the following:
in log model add the following method:
public function user(){
return $this->belongsTo('App\User','user_id','id');
}
and then you can access the user information in view file such as firstname as the following
#foreach($logs as $log)
<h1>{{$log->user->first_name .' '.$log->user->last_name }}</h1>
#endforeach
You don't need to set a relationship. Just use eloquent.
$users = User::whereIn('id', array(...))->get();
Or with query builder
$users = DB::table('users')->whereIn('id', array(...))->get();
Replace the dots by the values of your IDs
You can use Eloquent relationship as answered by Ali or you can use "Joins" with your result as:
$logs = Log::join('users', 'users.id', '=', 'logs.user_id')->get();
return view('logreport.index')->with('logs', $logs);
and then access the result as:
<h1>{{ $log->first_name .' '. $log->last_name }}</h1>

How to show specific items of an array in Laravel

I am using Laravel.i want to show only the Name in my View Part. But facing problem.
function index() {
$data=array(
['id'=>'1','Name'=>'Debasais','sirname'=>'Acharya'],
['id'=>'2','Name'=>'prashant','sirname'=>'Mohaptra'],
['id'=>'3','Name'=>'Silu','sirname'=>'Mohaptra']);
return view("welcome")->withdata($data);
}
To show just the name in your view, just loop through $data and select only name.
#foreach($data as $names)
{{$names['Name']}}
#endforeach
I am pretty sure you are already looping through $data just make sure to output the name only

Laravel Query DB Get Results from 2 Users with ID

I work on an Message System and i want Query the conversation between 2 Users. I cant use Controller now, soo i must try it on this way.
I want display the Results for example Title between my User ID Auth::user and the Senders ID.
Where "to" is my ID= Auth::user : ->where('to',
How can i change this Code, because with my Code i see all Messages from me and not only between my ID and the Other part ID:
<?php $subjects = DB::table('messages')->where('to', '=', Auth::user()->id)->pluck('title'); echo $subject; ?>
Thanks
You can add a second where clause, just chain them like this
<?php $subjects = DB::table('messages')->where('to', '=', Auth::user()->id)->where('from', '=', $senders_id)->pluck('title'); echo $subject; ?>
Check documentation for more information about query building
https://laravel.com/docs/5.5/queries#where-clauses

Laravel Foreign Keys Drop-down List

I have 2 tables:
CUSTOMERS(id, full_name, company_id)
COMPANIES(id, company_name)
I already created the relation between the two tables, and it's working fine because I can display company name in the customers view like this: $customer->company->company_name
I'm now having issues with the customer create and edit views. I'd like to have the company_name as a drop-down (Form Select) in create and edit views. Then insert the company id to the CUSTOMERS table.
You need to supply Form::select with companies as an array('id'=>'name'):
// Controller, repo or wherever you want it:
$companies = Company::lists('company_name','id');
// $companies passed to the view, then in the create view:
{{ Form::select('company_id', $companies, null, $options) }}
// edit view:
{{ Form::model($customer, array('route' => array('YourCustomerUpdateRoute', $customer->id))) }}
...
{{ Form::select('company_id', $companies, null, $options) }}
// form model binding autopopulates the form, so correct option will be selected
After submitting the form validate the input, check if provided company_id exists on the companies table and save the customer, that's all.
The Jarek Tkaczyk answer is excellent. However if you want make a default value for create form and avoid pre-select the first element in the $companies array, you can do something like this in your controller:
$companies = Company::lists('company_name','id');
$companies = array('0' => 'Select Company') + $companies;
Then pass $companies array to the view as Jarek Tkaczyk said.
Note:
I did
$companies = array('0' => 'Select Company') + $companies;
in order to preserve the array keys.

Resources