Laravel - Get only attributes data [duplicate] - laravel

This question already has answers here:
Laravel Getting attributes data
(2 answers)
Closed 2 years ago.
I am currently working on Laravel 6.13. I am using eloquent join to fetch the data from multiple tables.
Here is my working code:
$ToGetDefaultAddress = TbPersonaddress::join('tb_cities AS TbCitie','TbCitie.n_CityId_PK', '=', 'tb_personaddresses.n_CityId_FK')
->join('tb_counties AS TbCountie','TbCountie.n_CountyId_PK', '=', 'tb_personaddresses.n_CountyId_FK')
->join('tb_states AS TbState', 'TbState.n_StateId_PK','=' ,'tb_personaddresses.n_StateId_FK')
->select('tb_personaddresses.n_PersonAddressesId_PK','tb_personaddresses.n_PersonId_FK',
'tb_personaddresses.s_AddressTypeCode','tb_personaddresses.s_HouseNo',
'tb_personaddresses.s_HouseDirection1','tb_personaddresses.s_StreetName',
'tb_personaddresses.s_HouseType','tb_personaddresses.s_HouseDirection2',
'tb_personaddresses.s_AddressLine1','tb_personaddresses.s_AddressLine2',
'tb_personaddresses.s_AddressLine3','tb_personaddresses.s_AddressLine4',
'tb_personaddresses.n_CountyId_FK','tb_personaddresses.n_CityId_FK',
'tb_personaddresses.n_StateId_FK','tb_personaddresses.n_CountryId_FK',
'tb_personaddresses.s_PostalCode','tb_personaddresses.n_Zipcodes_FK',
'tb_personaddresses.s_Latitude','tb_personaddresses.s_Longitude',
'tb_personaddresses.s_CountryName','tb_personaddresses.s_Description',
'tb_personaddresses.d_EffectiveDateFrom','tb_personaddresses.d_EffectiveDateTo',
'tb_personaddresses.s_IsDefaultAddress','tb_personaddresses.n_CreatedUser',
'tb_personaddresses.d_CreatedDate','tb_personaddresses.n_UpdatedUser',
'tb_personaddresses.d_UpdatedDate','tb_personaddresses.n_EditVersion',
'TbCitie.s_CityCode','TbCitie.s_CityName','TbCountie.s_CountyCode',
'TbCountie.s_CountyName','TbState.s_StateCode','TbState.s_StateName')->first();
I checked using dd() for output. I want to get only attributes data as specified in image. How can I get those ?
Tried solutions from following link:
Laravel Getting attributes data
But those are not working.
Here's what I tried from link after above code:
$DefaultAddress = $ToGetDefaultAddress->all()->toArray();
dd(":::::::::::To get default address::::::::",$DefaultAddress);
But it returns blank output.

You should chain the toArray() function at the end of your database call to get only the attributes (and any loaded relations).

Related

Laravel Paginate not working with specific model or table

Paginate not working for specific model. with all Models, paginate is working fine except one specific model. Also tried with query builder.
$doubts = DB::table('users')->paginate(10);
dd($doubts->count());
//output: 10
$doubts = DB::table('doubts')->paginate();
dd($doubts->count());
//output: 0
$doubts = DB::table('doubts')->get();
dd($doubts->count());
//output: 8
Don't know what am I missing here. Please help.
Maybe I think you have to put
How many results you want to show per page
So if you want 8
Then I think it should be something like this
$doubts = DB::table('doubts')->paginate(8);
dd($doubts->count());
And if you are using pages Number under your data then you might need the query builder.
You should pass number argument inside paginate function.
Look at laravel pagination documentation
And make sure that DB::table('doubts')
Is returning collection
I thing you must add parameter in paginate(), to show how much data that show per page. You can read documentation in : https://laravel.com/docs/9.x/pagination#paginating-query-builder-results
https://laravel.com/docs/9.x/pagination#cursor-pagination
because you are not add parameter in paginate, it means not data that show every page.
$doubts = DB::table('doubts')->paginate(10);

Laravel 8 validation for image can get old input after symbolic link [duplicate]

This question already has answers here:
How to set a value to a file input in HTML?
(10 answers)
Laravel 5.1: keep uploaded file as old input
(3 answers)
Closed 1 year ago.
Using Laravel 8, I thought when I used to return a form on a validation error, I could load all the old inputs back into the view using the {{old('fieldname')}} helper.
However, that has stopped returning the inputs for my file upload input boxes, everything else returns to the form as normal, except the image inputs.
$validatedData = $request->validate([
'front_image' => 'mimes:jpeg,png,jpg,gif|max:3000',
'back_image' => 'mimes:jpeg,png,jpg,gif|max:3000|required',
'indentured' => 'required|in:yes,no',
'trade_selection' => ''
]);
When I use {{ old('front_image) }} nothing is reported. Weird thing is I can receive the errors for the input, and if I allow the form to push through I can see the files with dd($request) under files.

Best way share data across all view in Laravel

I have some data that I want to share across all views. I am using AppServiceProvider's boot method to share data.it is working fine with MySQL however with pgsql when I run composer or PHP artisan commands I am getting the following error,(i do fresh and seed database very often)
SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "news" does not exist
it took me a while to understand where is the real issue. I m still unable to understand if it is pgsql related error or something else, bellow is code in AppServiceProvider. if I comment this code everything works fine except where I m using this.
public function boot()
{
$activeClubs = (new TournamentService())->getAllActiveClubs();
$activeNews = (new TournamentService())->getActiveNews();
$activeTournaments = (new TournamentService())->getActiveTournament();
View::share(['activeClubs' => $activeClubs, 'activeTournaments' => $activeTournaments, 'activeNews' => $activeNews]);
}
can you please help me that how can i share data across all views that i don't get this error in future.
you can determine if the app is running from console or not and prevent loading data in console
if ( !app()->runningInConsole() ){
// it's not console.
}
you mentioned you want to load this data in all views but sometimes, its one view that loaded in all of your views e.g. the layout.app view.
if its the case I recommend using view composers instead of View::share() as it will pass the data to that view before render and it will only run if that view is included in the page (so basically it will solve your problem even without app()->runningInConsole() condition required )
view composers doc
simple example:
View::composer('view-name', function ($view) {
$view->with('key', 'value');
});

Laravel 5.3 - how do I set fetchmode in query builder?

I'm working with legacy code. The old report engine uses associative arrays, Laravel's query builder returns an array of objects.
I need to turn objects into arrays. I've tried using:
\DB::connection('tars-test') //->setFetchMode(PDO::FETCH_ASSOC)
but that gets me Class 'App\Http\Controllers\PDO' not found
It's been suggested to put ->all() at the end of the query but that throws error Call to a member function all() on array
The most efficient way would be to set the fetchmode at runtime, for the legacy function and just for the legacy function. How do I do it?
You can use 'toArray' method:
https://laravel.com/docs/5.3/collections#method-toarray
Laravel 5.3 and lower
You went the right way but as you can see laravel is trying to find PDO in App\Http\Controllers\PDO which probably means you forgot to add use Illuminate\Database\PDO;
Laravel 5.4 and up
Since laravel 5.4 this is not an option. But you still can set fetch mode globally: Laravel 5.4 - How to set PDO Fetch Mode?
Or if you still wish to change it just locally:
Return values only (no keys/associative array) in Laravel

Laravel eager loading model same name constraint

i've got a Goal model which got a attribute named stopover.
Furthermore this model has a hasMany-Relationship to a model called Stopover.
When i'm trying to get a goal via
$goal = Goal::find(1)->with("stopover")
then i'm running into problems because of the two identically named values. If im using
$goal->stopover
it's returning the attributes value, thats fine.
BUT! how can i get the related model Stopover now? I hope you see the problem.
Thanks in advance.

Resources