I want to fetch a record(price) from db table and when sending it to api response I want to change it inside my model according to some conditions, I have searched and not found any relevant solution to this.
Try something like this?
$record = ModelName::find($id);
$record->price = $newValueBasedOnCondition;
$record->save();
Related
If we have a find like this:
$returnedModel = Flight::
with("passengers")
->find(1);
Now if I add a where on returnedModel like this:
$returnedModel->passengers()->where("name" , "hamid")->first();
Will Laravel reconnect to the database for any where on returned model collection?
I added DB::getQueryLog() and there is a query with this where! how can I get this without reconnect to database?
You need to access your passengers as a property, not a method, by the way you get a Laravel collection already loaded from the DB (thanks to the with eager-loading).
$returnedModel->passengers->firstWhere('name', 'hamid');
Flowing query is working fine on mysql database but how can I retrieve this in laravel? code of mysql is given bellow:
select count('brand_id') from products where brand_id=3
You could do
$number = 3;
DB::table('products')->where('brand_id',$number)->count();
But I recommend using Models
Product::where('brand_id',$number)->count();
Following what laravel has on https://laravel.com/docs/5.6/queries at the aggregates topic:
$products = DB::table('products')->where('brand_id', 3)->count();
Welcome to Stack Overflow!
As #PlayMa256 said, you can do exactly this query using
DB::table('products')->where('brand_id', 3)->count();
BUT, if you have defined your models correctly, you can do this directly using you model class. If your model is called Product, then:
Product::where('brand_id', 3)->count();
Another awesome way to do this is by using the magic methods for where clause:
Product::whereBrandId(3)->count();
Back when I was using CodeIgniter I had functions in my models like
public function GetArticlesFormatted($inactive = FALSE)
and then in my controller I could have had
$articles->GetArticlesFormatted(true);
And so on.
How should I achieve the same with Laravel 5.4? The database of the app I'm building is already built and full and is a mess so most of the "automated" super-restrictive things that Laravel has don't work out of the box.
For example there is a Country Code that I'm retrieving and I need it as is, but in some instances I need it converted in a Country Name which I had in another table.
Right now I just have in my controller wherever I am retrieving data from the model:
$countryResult = Country::where('country_code', $item['country_code'])->first();
$countryArray = $countryResult->toArray();
$item['country'] = $countryArray['country_name'];
Can I somehow achieve that in a more elegant way?
I tried accessors but for some reason couldn't get anything to work for my purposes.
A select query can be used to limit selection to a particular column.
$countryName = Country::select('country_name')
->where('country_code', $item['country_code'])
->first();
Does laravel provide a more efficient way to only get values for the authenticated user instead of repeating the following line for every model type.
$cards = Auth::user()->cards;
$books = Auth::user()->books;
$friends = Auth::user()->friends;
.........
I can see how this can be accomplished by a middleware but wanted to see if laravel provides anything out of the box.
You can eager load relationships using eloquent's "with" method.
I have a database containing products. I want the user to be able to select some filters in a form and return the relative data from the database in the same view. How do you do that in Laravel 5.1?
Your form should/could send the data to your controller and return the view based on those fields they filled in on the form.
Your controller might end up looking something like this.
$srch_val = Input::get('srch_val');
$return_query = Model_Name::where('dbColumn', 'LIKE', $srch_val . '%')
return View::make('your_view')->with('return_query', $return_query);
At least something to get you started, I hope.
Also might be a little different as that last time I did something like this was in 4.2 lol
Code fixed, thanks Kiran.