I am new in laravel. i have a table but dont have model for this. I am using below to fetch record.
$classes = DB::table('school_classes')->get();
return view('classes', ['allClass' => $classes]);
it returns below:
[{"id":1,"register_school_id":1,"class":"I","section":"A","created_at":"2020-03-19 00:00:00","updated_at":"2020-03-19 00:00:00"},
{"id":2,"register_school_id":1,"class":"I","section":"B","created_at":"2020-03-19 00:00:00","updated_at":"2020-03-19 00:00:00"},
{"id":3,"register_school_id":1,"class":"I","section":"C","created_at":"2020-03-19 00:00:00","updated_at":"2020-03-19 00:00:00"}, {"id":4,"register_school_id":1,"class":"I","section":"D","created_at":"2020-03-19 00:00:00","updated_at":"2020-03-19 00:00:00"},
{"id":5,"register_school_id":1,"class":"I","section":"E","created_at":"2020-03-19 00:00:00","updated_at":"2020-03-19 00:00:00"}]
how I can pick the value separately of each item... i tried foreach but getting error.
plz help. thanks in advance.
The query builder returns a Illuminate\Support\Collection instance. You can access the collection as below:
#foreach($allClass as $class)
{{ $class->id }}
#endforeach
Also check Collections
You want to get data in array format, then you can use "toArray" & also easily access data using foreach loop Laravel function.
$classes = DB::table('school_classes')->get()->toArray();
return view('classes', ['allClass' => $classes]);
Related
I have the following data structure:
[
{
"id": 1,
"customer_id": "11",
"customer_name": "Vic",
"cheque_withdraw": {
"id": 2,
"request_date", "2019-03-30"
}
}
]
I have gotten this data from multiple tables through the following code;
$paginator = Cheque::with(['chequeWithdraw' => function($query){
$query->where('withdraw_status' , 'withdrawn');
}])
->paginate(5);
How do I access the nested json object request_date without having to convert my collection to an associative array? The reason why I do not want to convert the collection to an associative array is because I am having challenges paginating the data that has been json decoded.
I am currently trying to access the request_date in my blade file this way;
#foreach($paginator as $cheque)
{{ $cheque->cheque_withdraw->request_date }}
#endforeach
However, I am getting the following error;
Trying to get property 'request_date' of non-object
Actually you have chequeWithdraw and not cheque_withdraw
#foreach($paginator as $cheque)
{{ $cheque->chequeWithdraw->request_date ?? 'NA'}}
#endforeach
You might be trying to convert laravel collection to json or array. laravel convert camel case to snake case . So you were getting it for json not for laravel template.
I'm starting to learn about Laravel Framework and I have a little doubt.
I have 2 models: Customers and Opportunities.
I want to display on opportunities/show.blade.php the customer name but I have a problem with the format of the data I retrieve from customer table on the controller.
I have this show function on my OpportunityController:
public function show($id)
{
$opportunity = Opportunity::find($id);
$customerName = Customer::select('name')->where('id','=',$opportunity->customer_id)->first();
return view('opportunities.show',compact('opportunity','customerName'));
}
And i call this variable on the show.blade.php like this:
{{ $customerName }}
But it's showing the customer name on this format:
Customer: {"name":"John"}
How can I show the customer name as this:
Customer: John
Thank you!
The data return by a model is a Std Class Object, so you can use it like:
{{ $customerName->name }} // i.e. variable->column_name
It will print John.
If the you are using get() instead of first() then you have to iterate the returned array of Std Class Object variable by using foreach() loop.
You are currently returning a Model Object. So, you have to specify the attribute you wish to display. In your case, you selected only the name. So, do this:
{{ $customerName->name }}
The following query selects a single row from the database, but it is still returned as an object:
Customer::select('name')->where('id','=',$opportunity->customer_id)->first();
So the result of the query will be an object that translates to {"name":"John"}
If you want the name value you can either use {{ $customerName->name }} in your view or change the query to the following to get only the name column:
Customer::select('name')->where('id','=',$opportunity->customer_id)->first()->name;
$customerName is treated as an object in this case so please try {{ $customerName->name }} for getting a value of name .
Hope it can help you
I can get the first user, who is an admin in Laravel's tinker using the following command:
$adminUser = App\User::where('is_admin',true)->first();
How do I get all the users which meet this where criteria?
Just change first() to get().
$adminUser = App\User::where('is_admin',true)->get();
first() is used when you want to retrieve single row or column. where get() is used to retrieve all rows.
just go through the laravel official documentation database query builder section here. It will give you information regarding most of every possible things you can playoff with laravel.
$users = App\User::where("is_admin", true)->get();
The get method returns an Illuminate\Support\Collection containing the results where each result is an instance of the PHP StdClass object. You may access each column's value by accessing the column as a property of the object:
foreach ($users as $user) {
echo $user->name;
}
Src: https://laravel.com/docs/5.6/queries
In case of Tinker, things in Query builder and Eloquent docs will work in Tinker. It exists for the purpose of instantly getting the result without debugging using your real application.
The first() function is used when you want to retrieve single row, whereas the get() function is used to retrieve all matching rows.
$adminUser = App\User::where('is_admin', true)->get();
dd($adminUser);
I have a problem and can't figure out how to solve it. I've searched online but still couldn't get a firm answer. What I'm trying to do is to paginate Inquiries table - or the query below. Everything seems to work (at least I'm getting 20 inquiries per page, however I can't figure out how to display the links()?
I'm saving Inquiries in the $thecusts collection and passing it to the view.
Tables and relations : Employees - Dealers (manytomany)
Dealers-customers(1 to many)
Customers-inquiries (1 to many).
So I need to paginate x Inquiries for the employee that has many dealers.
Anyone can help ?
$justch = $me->employeehasdealers()->get(['dealers.dealer_id','dealer','abbreviation']); //list of dealers assigned to auth employee
$justch2=$justch->load(['DealerHasInquiries'=>function($query){
$query->with('inquiriescomments:comments_inquiries_id,inquiry_id,employee_id,comments','inquiryspdl','inquiriescustomers:customer_id,customer')->paginate(20);
}]);
$thecusts = new Collection();
foreach ($justch2 as $just) {
$thecusts = $thecusts->merge($just->DealerHasInquiries);
}
The problem is you're not passing back the paginator instance. You can manually create one in your controller based on $thecusts:
$perPage=20;
return view('my-view', ['paginator' => new Paginator($thecusts->toArray(), $perPage)]);
Then you can call the links method on the paginator instance in your view:
{{ $paginator->links() }}
Edit
You may be able to simply all this though in a single query like:
$thecusts = $me->employeehasdealers()->with(['DealerHasInquiries'=>function($query){
$query->with('inquiriescomments:comments_inquiries_id,inquiry_id,employee_id,comments','inquiryspdl','inquiriescustomers:customer_id,customer');
}])->paginate(20);
return view('my-view, compact('thecusts'));
however I can't figure out how to display the links()?
You can do this in your blade:
{{ $justch2->links() }}
The Answer has been posted by btl - (Much appreciate it!)
Here is my code with some additions:
Controller:
$perpage=19;
$justch = $me->employeehasdealers()->with(['DealerHasInquiries'=>function($query) use ($perpage){
$query->with('inquiriescomments:comments_inquiries_id,inquiry_id,employee_id,comments','inquiryspdl','inquiriescustomers:customer_id,customer')->simplePaginate($perpage);
}])->get(['dealers.dealer_id','dealer','abbreviation']);
foreach ($justch as $just) {
$thecusts = $thecusts->merge($just->DealerHasInquiries);
}
$paginator = new Paginator($thecusts->toArray(),$perpage, Paginator::resolveCurrentPage(),['path'=>Paginator::resolveCurrentPath()]);
$inquiries = $thecusts;
return view('/inquiries', ['paginator'=>$paginator,'inquiries' => $inquiries]);
Blade: {{$paginator->links()}}
I there, im using eloquent to create a query where gets the galleries from a specific user_id, but i want also to implement the pagination, but is not working the way im doing.
ex:
$galleries = Gallery::paginate(10)->where('user_id', $userId)
->get();
$galleries = Gallery::where('user_id', $userId)->paginate(10);
In the controller you are supposed to do :
$galleries = Gallery::where('user_id', $userId)->paginate(10);
In the view use :
{{ $galleries->appends(Input::except('page'))->links() }}