Iam learning laravel.I don't understand how to read multiple data in laravel.
In my Data-table , my value is 1,2,3
my column name is hobby & value is 1,2 . 1 & 2 are related other table, where i stored my hobby name .
suppose ,
id || name
1 || gardening
2 || playing
I want to display
gardening,playing
My controller's Code :-
$interests = DB::table('tbl_interests') ->join('tbl_interest_masters','tbl_interest_masters.interest','=','tbl_interests.id')
->select()
->get();
return view('profile')
->with('interest',$interests);
Don't understand how display data in my view ?
is my code right for read multiple data !!
Note that, i already learn how to read single data with relationship in Laravel.
just remove the select() method from the chain.
$interests = DB::table('tbl_interests')
->join('tbl_interest_masters','tbl_interest_masters.interest','=','tbl_interests.id')
->first();
return view('profile')->with('interest',$interests);
I don't fully understand your question, do you want to return multiple variables?
If so, try this:
return view('YOUR_VIEW', compact('VARIABLE1', 'VARIABLE2'));
If you are using blade, you can use this to display the returned variables:
{{$VARIABLE1}}
Loop through an array:
#foreach($interests as $interest)
{{$interest->name}}
#endforeach
Related
I have a 2 columns in one of my table.
| mark_1 | mark_2 |
These marks are saved for 2 types of users in the system.
mark_1 is for user_type_1
mark_2 is for user_type_2
When fetching from this table, I need to conditionally get specific mark column (mark_1 or mark_2) as mark for each type of user. How to achieve this using Laravel eloquent model.
But want this in Eloquent such as when I query Mark::all() I need the necessary mark column value.
if($userType == 'student_1'){
$marks = DB::table('marks')
->select('user_id', DB::raw('marks_1 as marks'));
} else {
$marks = DB::table('marks')
->select('user_id', DB::raw('marks_2 as marks'));
}
use 'addSelect' statement:
$query=User::query()->select('id');
if($userType == 'student_1')
$query=$query->addSelect('marks_1 as marks');
else
$query=$query->addSelect('marks_2 as marks');
$marks=$query->get();
more details about 'addSelect' in here:
https://laravel.com/docs/7.x/eloquent#advanced-subqueries
You can use Accessors & Mutators to get specify column value depend condition or append new attribubte to your model
check this link:
https://laravel.com/docs/7.x/eloquent-mutators#accessors-and-mutators
Using Laravel + Voyager, I have a "Has Many" relationship:
Course hasMany Teachers.
In backend all is fine, but if I try to get the information in the frontend, I only get the value in table, not the relation, so the output goes something like:
0
id 1
teachers_id null
name "Math"
1
id 2
teachers_id null
name "English"
Current code in web routing:
Route::get('/course', function () {
$co= App\Course::all();
return $co;
});
How can I get the correct output?
teachers_id Xav, Titus
so like #Tpojka commented you can start by eager loading the relationship when you are initializing the course likeso $co = App\Course::with('teachers')->get(); after which you do not need to make another unnecessary call to your database for the teachers of that course. You can get a collection of all the teachers of that course by simply calling $teachers = $co->teachers; here $teachers is now a laravel collection you can simply loop through it on the client side and display the information about the teachers you want to display. I hope this helps.
Good luck and happy coding. :)
Im using Eloquent. But I'm having trouble understanding Eloquent syntax. I have been searching, and trying this cheat sheet: http://cheats.jesse-obrien.ca, but no luck.
How do i perform this SQL query?
SELECT user_id FROM notes WHERE note_id = 1
Thanks!
If you want a single record then use
Note::where('note_id','1')->first(['user_id']);
and for more than one record use
Note::where('note_id','1')->get(['user_id']);
If 'note_id' is the primary key on your model, you can simply use:
Note::find(1)->user_id
Otherwise, you can use any number of syntaxes:
Note::where('note_id', 1)->first()->user_id;
Note::select('user_id')->where('note_id', 1)->first();
Note::whereNoteId(1)->first();
// or get() will give you multiple results if there are multiple
Also note, in any of these examples, you can also just assign the entire object to a variable and just grab the user_id attribute when needed later.
$note = Note::find(1);
// $user_id = $note->user_id;
im trying to get the total count of records that are less than a value in my table so im using this query in laravel 4.2
$critical = DB::table('dbo_modules')
->where('ModuleCountLeft','<','ModuleCriticalLevel')
->count();
and passes it like this
return View::make('ssims.board' ,compact('title','mTotal','critical'));
//please don't mind the others
then receives it in the view page like this
<div>Modules at critical level <span><strong><?= $critical ?></strong></span></div>
unfortunately, im getting zero whereas in my database, i have 2 records where ModuleCountLeft is less than ModuleCriticalLevel
any ideas?
thanks
Hi turns out that i needed to pluck out the value of ModuleCriticalLevel first here is the code
$critical = DB::table('dbo_modules')
->where('ModuleCountLeft' , '<' , DB::table('dbo_modules')->pluck('ModuleCriticalLevel') )
->count();
-thumbs up here-
I'm new to smarty and prestashop. I'm building a quick, dirty module that pulls out cms pages with a particular category:
$result = Db::getInstance()->executeS('SELECT *
FROM ps_cms_lang
INNER JOIN ps_cms ON ps_cms_lang.id_cms = ps_cms.id_cms
WHERE ps_cms.id_cms_category =2
AND id_lang =1
LIMIT 0 , 30');
$smarty->assign('news', $result);
So far this is all working dandy. Thing is I want to format some of this data before I assign it to the template variable (news). How do I do this? 6 fields are returned in the $result variable. How do I get at them and do what I need (which is essentially just truncating some of the text in the description field that is returned) to do and then package them back up for the assign?
You may use $result as array, for example: $result['id_cms'].