Laravel Choosing one column in database conditionally - laravel

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

Related

Converting multiple same name into one single name and also values get sum in laravel

I am trying to fetch the multi same name to single name also sum associated QTY values also.Show PART-ID as it's
example
I want to show like this
my controller
public function productserach()
{
$searchitem = Item_list::get();
return view('products.product-serach',compact('searchitem'));
}
DB::table('your_table')->select(DB::raw('sum(qty) as qty,name,part_id'))->groupBy('name','part_id')->get()
So basically use raw select to sum qty you want and group by fields that you want to have unique. Also you can use laravel eloquent like above but change it a bit because it is incorrect the output data will be not same as you want.
you can do try this code
use DB;
$sums = Model::select(DB::raw('sum(qty) as total_quantity'),'name')->groupBy('name')->get();

changing a query result value according to wherehas condition in laravel

I am using laravel
I want to change the value of a column according to a specific condition.
if a condition is satisfied in wherehas then change the value of specific column to 1 let's say.How could i do it.
if i can call a function in the model inside the wherehas function to change the value how could I do it ??
i can iterate the result set using a 2 for loops and change it, however I want to decrease the complexity by changing the value while retrieving the data
Course::with('stages','stages.levels')->whereHas('stages.levels', function($query)use ($levelsarray){
$query->wherenotIn('id', $levelsarray);
here I want to change a column value in table levels
})->first();
Here is a general way,
Assuming you have Three models, Course, Stage, Level
When you are retrieving data from Level model, add an accessor,
For more info click here.
Eg:
On Level.php model,
public function getColumnNameAttribute($value) // replace ColumnName with column name
{
//write application logic here.
return $value;
}

Laravel 5.5 - Check if only one column will be updated

Using Laravel 5.5 and models, I would like to know, before updated if only one column, statut, will be updated.
The interest is to just updated the statut if there are no changes or, if there are some columns to be updated, create a new row. Because each modification need to be approved before published.
Actually, I have a big if like :
if($request->statut != $product->statut && $request->title == $product ...)
Is there a quickest way ?
Thanks.
You can utilize isDirty('field') method to check if the field was changed (and not yet saved). getDirty() will return you array of all such fields and their values.
If I understood your case correctly, then you can do:
if ($product->isDirty('statut')) {
if (count($product->getDirty()) == 1) {
...
}
}
First we check if field statut was changed for your $product. Then we just check if number of changed fields is 1 (meaning that there are no more changed fields than just statut).

Select one column with where clause Eloquent

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;

return a row of a table using Active Record in Yii

I want to return a field of a row of table which has id = 4 in a model for example Post.
Which one should I use find() method or findByAttributes() ? And what is the correct syntax for it ?
To grab a model by its primary key I'd suggest `findByPk(). It's one of the most simple methods to use;
$id = 4;
$model = Post::model()->findByPk($id);
For other method syntaxes, have a read through the Yii Active Record wiki and Yii Active Record documentation, they're really helpful when starting out.

Resources