I want to access values from my query in view.
$bookings = DB::table('bookings')
->join('staffs', 'staffs.id' , '=', 'bookings.staff_id')
->join('customers', 'customers.id' , '=', 'bookings.customer_id')
->select('bookings.id', 'bookings.start_time', 'bookings.end_time', 'bookings.service', 'staffs.name as Staff-Name', 'customers.name as Customer-Name')
->orderBy('customers.name', 'desc')
->get();
return view('booking.index')->with('bookings', $bookings);
This is simple enough to understand. What I am trying to do is that i want to get start_time, end_time which are in bookings table, name which is staffs table also name in customers table.
Right now I am doing like:
#foreach($bookings as $booking)
{{ $booking->start_time }}
{{ $booking->end_time }}
{{ $booking->name }} // name column which is in customers table
#endforeach
But these things don't seem to work.
It's all because you are using column names instead of aliases which you set.
Take a look at it, by example customers.name is named Customer-Name.
->select('bookings.id', 'bookings.start_time', 'bookings.end_time', 'bookings.service', 'staffs.name as Staff-Name', 'customers.name as Customer-Name')
If you want to access data you need to do it like that:
#foreach($bookings as $booking)
{{ $booking->start_time }}
{{ $booking->end_time }}
{{ $booking->Customer-Name }} // name of alias
#endforeach
Anyway I don't think that dash in alias is good solution, you should use underscore instead. (customers.name as Customer_Name) and $booking->Customer_Name
Try this
$bookings = DB::table('bookings')
->join('staffs', 'staffs.id' , '=', 'bookings.staff_id')
->join('customers', 'customers.id' , '=', 'bookings.customer_id')
->select('bookings.id', 'bookings.start_time', 'bookings.end_time', 'bookings.service', 'staffs.name as Staff-Name', 'customers.name as Customer-Name')
->orderBy('customers.name', 'desc')
->get();
return view('booking.index')->with(compact('bookings'));
The $bookings contains an array of data. You can print these in your view ike this..
#foreach($bookings as $booking)
{{$booking->start}}
{{$booking->end_time}}
#endforeach
Related
here is my code:
$data = Courses::select('courses.id','courses.standid', 'courses.publisher', 'courses.coverpic', 'courses.course_sts', 'standards.standtitle AS stitle', 'subjects.subtitle AS btitle')
->join('subjects', 'subjects.id', '=', 'courses.subjectid')
->join('standards', 'standards.id', '=', 'courses.standid')
->get();
and blade codes are:
#foreach ($data as $row)
<p>{{ $row->stitle }}</p>
#endforeach
the query works fine when i check dd($data) under attributes, but the stitle and btitle columns never shows inside blade as {{ $row->stitle }} or {{ $row->btitle }}
what am I missing or how to collect the joining table column into the blade file?
cheers
Thanks for response. It was the redirect method that taking me back to history page rather then the post update status.
it was return redirect (the_view_name)->with(vars)
instead of return view (the_view_name)->with(vars)
my first experience to ask a question at stackoverflow and resolved by myself.
I'm trying to take out "superadmins" by using this controller:
$user->superadmin = DB::table('role_users AS ru')
->leftJoin('users AS u', 'u.id', '=', 'ru.user_id')
->where('role_id', '=', '5')
->select("u.*")
->get('');
And blade:
#foreach ($user->superadmin as $superadmin)
{{ $superadmin->username }}
#endforeach
"users" table have a unique ID. That unique ID is the same as role_users.user_id.
But i'm getting error:
Creating default object from empty value
Any help? Thanks!
$user->superadmin = ...;
Creating default object from empty value
You need to define $user as some object before trying to set a property on it (using it like an object).
$user = new stdClass;
$user->superadmin = ...;
I am not sure what type you are expecting $user to be besides some object.
remove get(' ') from get as '' it selects an empty value
$user->superadmin = DB::table('role_users AS ru')
->leftJoin('users AS u', 'u.id', '=', 'ru.user_id')
->where('role_id', '=', '5')
->select("u.*")
->get();
To get users with the specific role id :
User::with(['role_users'=>function($user) use($someId){
$user->where('role_id', $someId);
}]);
Try this :
$user->superadmin = DB::table('role_users')->select(['users.*'])
->leftjoin('users', 'users.id', '=', 'role_users.user_id')
->where('role_users.role_id','5')
->get();
Pretty new to Laravel and trying to figure out the ins and outs.
If I write one main query that selects everything in my table, can I write individual queries when selecting columns and rows from the table.
Example:
Main Query:
public function allTickets(){
$tickets = DB::table('tickets')->get();
return view('admin.index',compact('tickets'));
In my view:
//THIS WORKS. It gives me a count of all open and closed tickets
{{$tickets->where('status', '=', 'OPEN')->Count()}}
{{$tickets->where('status', '=', 'CLOSED')->Count()}}
However this does not work...
Error: Call to a member function where() on string (View: ...
#foreach ($tickets as $ts)
{{$ts->subject->where('status', '=', 'OPEN')}}
#endforeach
#foreach ($tickets as $ts)
{{$ts->subject->where('status', '=', 'CLOSED')}}
#endforeach
Is there a way to use that ONE main query and show all the subjects of the tickets that are open and closed, or will I need to write multiple queries in my controller to achieve this?
You can do like this with simple solution.
first write one and then another one with if statement
#foreach ($tickets as $ts)
#if($ts->status == 'OPEN')
{{$ts->subject}}
#endif
#endforeach
#foreach ($tickets as $ts)
#if($ts->status == 'CLOSED')
{{ $ts->subject }}
#endif
#endforeach
I am trying to fetch 3 values = 2 select forms and 1 from date form and compare it to the one's on my database if it's equal but whenever I tried to compare the 3 of them using the AND operator in laravel, it doesn't give me any details. How can I compare them correctly?
OnewayflightController.php
public function onewayflightresults()
{
$search1 = Input::get('destinationto');
$search2 = Input::get('destinationfrom');
$search3 = Input::get('departure');
$results = DB::table('oneways')->where('destinationto','=',$search1)
->where('destinationfrom','=',$search2)
->where('destinationfrom','=',$search3)
->get();
var_dump($results);
}
Database:
id-1
destinationto-Australia
destinationfrom-Japan
departure-01-2-14
onewayflight.blade.php
<div>
{{ Form::label('label','From: ') }}
{{ Form::select('destinationfrom', $destinationfrom)}}
</div>
<div>
{{ Form::label('destinationto','To: ') }}
{{ Form::select('destinationto', $destinationto)}}
</div>
<div>
{{ Form::label('departure','Departure:', array('class'=>'"input-group-addon btn"'))}}
{{ Form::text('departure', '', array('id' => 'calendar')) }} <span class="glyphicon glyphicon-calendar"></span>
{{ Form::close() }}
UPDATE
I've changed
->where('destinationfrom','=',$search3)
to
->where('departure','=',$search3)
But still gives me no results.
Your third where should be searching departure, not destinationfrom a second time.
Problem with
->where('destinationfrom','=',$search3)
Update your to
->where('departure','=',$search3)
I think that will work.
Need to update you third where condition. you are comparing destinationfrom value two times.
I think you are looking to a collection, and think that is not a result.
Try:
foreach($results as $result)
{
var_dump($result);
}
This will dump all the rows found with your input. Only want 1 result? change ->get(); to ->firstOrFail(). Then you can dump $results without a foreach.
Or maybe a subquery is the solution?
$results = DB::table('oneways')
->where('destinationto', '=', $search1)
->where('destinationfrom', '=', $search2)
->where(function($query) {
return $query->where('departure', '=', $search3)
})
->get();
Is there only 1 result you are trying to get? Maybe this is better:
$results = DB::table('oneways')
->where('destinationto', '=', $search1)
->where('destinationfrom', '=', $search2)
->where(function($query) {
return $query->where('departure', '=', $search3)
})
->firstOrFail();
This will give you 1 result, not a collection.
If this doens't work, you can try dumping your input to check if its filled correctly:
public function onewayflightresults()
{
dd(Input::all());
// Rest of the code
}
If the values in Input::all(); are exactly the same as the values in your database, the query should give you the result. If not, try this to futher debug:
dd(DB::table('oneways')->where('id', '=', 1)->first());
I'm trying to paginate a page in my view like this:
#foreach($tasks as $task)
{{ $task->user_id }}
{{ $task->client_id }}
{{ $task->description }}
{{ $task->duration }}
{{ link_to_route('clients.show', 'View client', array($task->client_id), array('class' => 'btn btn-primary')) }}
#endforeach
{{ $tasks->links() }}
Using the following query in my controller:
$tasks = DB::table('tasks')
->join('users', 'tasks.user_id', '=', 'users.id')
->join('clients', 'tasks.client_id', '=', 'clients.id')
->select(array('tasks.description', 'tasks.duration', 'tasks.client_id', 'tasks.user_id', 'users.email', 'clients.name'))
->where('tasks.group_id', '=', $usergroup)
->orderBy('tasks.created_at', 'DESC')
->paginate(20);
return View::make('tasks.index', compact('tasks'));
It shows the tasks fine but there's no pagination link showing up so I can't head over to the next batch of 20 results.
Any ideas on how I can make this work?
I've tried #foreach($tasks->result as $task) in my view as suggested in http://forums.laravel.io/viewtopic.php?id=4092 but it gives me an error "Undefined property: Illuminate\Pagination\Paginator::$result"
For those playing at home - I discovered the answer to this:
The compact function is converting the object to an array. Change your return view method to:
return View::make('tasks.index')->with('tasks', $tasks);
And you're in the clear!
Another point - if you're using Bootstrap 3 RC1 like me you'll find that pagination breaks because of the way BS3 styles pagination - if you have that issue head over here for the solution: https://github.com/laravel/laravel/issues/2215
:)