Retrieving data from table with multiple row - laravel

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.

Related

Laravel ::all->where returns everything

I'm trying to get only rows which have column approved filled with value 1
$reviews = Review::all()->where('approved', '1');
return view('items.show', compact('reviews')
I also tried
$reviews = Review::where('approved', '1');
return view('items.show', compact('reviews')
and then I'm fetching them with
#foreach ($item->reviews->sortByDesc('created_at') as $review)
But this return every row from Review table from database no matter what the approved value. What am I doing wrong? Thanks in advance!
You must be use
$reviews = Review::where('approved', '1')->latest()->get();
return view('items.show', compact('reviews');
in blade
#foreach ($reviews as $review)
First thing is there is no reason to get "all" the records from the database then filter them when you can ask the database to only give you the records you want filtered.
$reviews = Review::where('approved', 1)->orderBy('created_at', 'desc')->get();
Second, as mentioned in the comments you should be iterating $reviews:
#foreach ($reviews as $review)
In the first example, you're adding a where condition after the query has executed. In the second you're not executing the query at all.
Instead do $reviews = Review::where('approved', '1')->get();

How to load multiple number of pages dynamically in laravel?

I have this table instruction_title and this table instruction_body. One instruction title can have one or many instruction body. In my resource controller instController the index funtion is showing the list of instruction title, onclick it is going to the show function and showing which bodies it has.
If the instruction id is 25, it is going to /instructions/25.
But I need something like /instructions/25/Body1 with a next button if it has body2 and on next it will go to /instructions/25/Body2 and go on.
You can do something like this :
in your routes :
Route::get('/instructions/{titleId}/{bodyName}', 'instController#index')->name('titleIndex');
And in your blade :
#php
$currentBodyId = $next = \DB::table('instruction_body')
->orderBy('id', 'asc')->select('id')
->first();
$next = \DB::table('instruction_body')
->where('titleId', $YOUR_TITLE_ID)
->where('id', '>', $currentBodyId)
->sortBy('id')
->first();
$nextUrl = ($next !== null) ? $next->YOUR_BODY_NAME : '#' ;
#endphp
And in your next button in blade:
#if ($nextUrl != '#')
<a href="{{ route('titleIndex', ['bodyName' => $nextUrl]) }}">
Next
</a>
#endif
#behnam's has some bad practice. Never have logic to query data in the presentation layer! That was the reason why MVC architecture was built.
But what you could do is attach a relationship one-to-many for both Models:
Read here
Then you can fetch bodies that belongs to a specific title like:
//in your controller
$datas = Title::find(25)->with('bodies');
Then you can loop through it to get the id.

laravel one to one retrieve many results

I have business and location table and they have one to one relationship. Everything works fine so far but they problem is I only find 1 result.
However business_id which is a foreign key in location table has loads of results.
My business table has id from 5-95 and these are the records I want to return
so in my location table I also have business_id which is from 5-95.
How do I tell eloquent to retrieve these specific records?
At this moment I have:
function transportProfile() {
$type = 2;
$business = Business::where('type', $type)->get();
$location = Business::find(3)->locations;
echo $location;
return view('userProfile', compact('business', 'location'));
}
So to clarify business table has id from 5-95 and location table has business_id from 5-95 I want to return results from those 2 tables so I can use it in view
Use the eager loading:
$businesses = Business::where('type', $type)->with('location')->get();
To display the data:
#foreach ($businesses as $business)
{{ $business->location->name }}
#endforeach

Linking user to project without direct relationship

I am trying to figure out how to do something. I make an API call to retrieve a load of Projects.
$projects = APIHelper::returnRequestedProjects();
The API being called is on another server and what is returned is an array. Within my own Laravel project, I have a database with my own users. I do not model the projects against these users. However, each product returned above however has a user_id, which matchesa user's id within my system.
What I need to do is obtain the user model for each project. If I was dealing with a single project I could do something like this in my Controller
$user = User::where('id', $project->user_id);
As the API call returns an array of projects, I pass this directly to my view. I would then do something like this
#foreach($projects as $project)
#endforeach
How would I go about though matching a projects user_id to a user within my system?
Thanks
If I understand you correctly, you are getting a response from a remote server with an array of data you need to get User data for.
$projects = APIHelper::returnRequestedProjects();
Therefore, inside your controller method,
$ids = [];
foreach ($projects as $project) {
$ids[] = $project['user_id']; // Assuming the user_id is the id field
}
$returnArray = [];
foreach ($ids as $id) {
$returnArray[] = User::where('id', $id)->first();
}
Pass the return array into your view:
return view('example.view', ['returnArray' => $returnArray]);
And then inside the view...
#foreach($returnArray as $value)
{{$value->id}}
#endforeach
EDIT: To answer the question in comments...
$returnArray[$project['id']] = User::where('id', $id)->first();
Inside your views, the returnArray keys are now the actual IDs of projects you retrieved.
#foreach($returnArray as $projectId => $user)
Project ID: {{$projectId}}, User ID: {{$user->id}}
#endforeach
You can try this one:
foreach($projects as $project) {
$project['user'] = User::where('id', $project['used_id'])->first();
}

database fetch value show in dropdown using 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)}}

Resources