I need to check if schedules_id equal to request schedules_id then need to check profile 'booked' or 'pending' then need to pass all the 'booked' profile seat values into blade.php. How can i do that or if another way or logic to do this please suggest me i'm new to laravel
how i getting data now :
class PassengersController extends Controller
{
public function booking(Request $request)
{
//river is used to pass important params with flow of it from page to page
$seat = $request->seat;
$buses_id = $request->buses_id;
$schedules_id = $request->schedules_id;
$data = Buses::where('buses_id', $buses_id)->first();
$seat = json_decode($data->seat_layout, true);
$front = json_decode($data->front_layout, true);
$bookingSeat = Bookings::whereColumn('schedules_id', 'schedules_id')->get();
$bookingSeat = $bookingSeat->map(function ($bookSeat) {
$bookSeat->seat = explode(",", $bookSeat->seat);
return $bookSeat;
});
return view('frontend.booking', ['seat' => $seat, 'buses_id' => $buses_id, 'schedules_id' => $schedules_id, 'front' => $front, 'bookingSeet' => $bookingSeat]);
}
}
blade.php
<div class="bus">
#foreach($seat as $key => $item)
#foreach($bookingSeet as $seer)
<div class="col-md-1">
<div class="seats back seats
#if(in_array($item['name'], $seer['seat']))
activeSeat
#endif"
data-id="{{$key}}">
<div class="special-attributes"></div>
#if(isset($item['name'])){{$item['name']}}#else 11A #endif
<input type="checkbox" name="seat_id[]" id="{{$key}}" value="{{$key}}">
</div>
</div>
#endforeach
#endforeach
</div>
table
bookings_id users_id schedules_id buses_id routes_id seat price profile
1 1 6 1 3 1 Null pending
2 1 6 1 3 2 Null booked
3 1 6 1 3 3 null booked
Problem with my current code is getting 2 array with all bookings table column how to pass that value into blade, when i try i getting duplicated checkbox for ex. one bus have a 50 seat but now showing 100 seat. seats like 1,1,2,2 in view
One array look like this :
Collection {#426 ▼
#items: array:2 [▼
0 => Bookings {#431 ▼
#fillable: array:7 [▶]
#primaryKey: "bookings_id"
#connection: "mysql"
#table: null
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:10 [▼
"bookings_id" => 1
"users_id" => 1
"schedules_id" => 6
"buses_id" => 1
"routes_id" => 3
"seat" => "1"
"price" => null
"profile" => "pending"
"created_at" => "2019-04-09 00:00:00"
"updated_at" => "2019-04-09 00:00:00"
]
#original: array:10 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
1 => Bookings {#432 ▶}
]
}
try this
Bookings::where('schedules_id', $schedules_id)->where('profile', 'booked')->get();
Try this:
$bookingSeat = Bookings::where('schedules_id', $request->schedules_id)->where(function ($q) {
$q->where('profile', 'booked')->orWhere('profile', 'pending');
})->get();
or this:
$profileTypes = ['booked', 'pending'];
$bookingSeat = Bookings::where('schedules_id', $request->schedules_id)->whereIn('profile', $profileTypes)->get();
#Edit
In this way, if your Bookings table contains all seats, already registered (1 to 50), it will return all seats that your profile is "booked" or "pending" for a particular bus, route, and time.
$profileTypes = ['booked', 'pending'];
$bookingSeat = Bookings::where('schedules_id', $request->schedules_id)
->whereIn('profile', $profileTypes)
->where('buses_id', $buses_id)
->where('routes_id', $request->route_id)
->get();
You are allowed to use arrays als wheres.
For example:
$model->where([
['field-one', '=', 'valueOne'],
['field-two', '=', 'valueY']
]);
If I understand the situation correctly, if schedules_id is provided schedules_id and the profile is either booked or pending, you want to retrieve data, the query should be
Bookings::where('schedules_id', $schedules_id)->whereIn('profile', ['booked', 'pending'])->get();
Related
We have a Laravel model and sometimes we want to get data from it's default table, but other times from a different table. It all works from a data perspective, but when the collection is returned from the alternate table, the 'table' attribute still references the original table (even though the data is from the other one as expected).
If we dd() getTable(), it is correct:
$model = COUNTRY;
$table = "countires";
$org_id = 1;
$org_collection = $model->setTable('merge_' . $table)->get()
->where('organization_id', $org_id)->keyBy('id');
dd($model->getTable()); // *** THIS SHOWS THE 2nd TABLE AS EXPECTED
dd() ouput:
"merge_countries"
But with the exact same code, if we dd() the result the table attribute in the collection is still the 1st one ('orgs') and not 'merge_orgs' as expected even though the data is correctly coming from the 'merge_orgs' table:
$model = COUNTRY;
$table = "countires";
$org_id = 1;
$org_collection = $model->setTable('merge_' . $table)->get()
->where('organization_id', $org_id)->keyBy('id');
dd($org_collection); // *** THIS SHOWS THE 1st TABLE EVEN THOUGH THE getTable() ABOVE DOES NOT
dd() output:
Collection {#297
#items: array:1 [
3 => Country {#295
#connection: "common"
#table: "countries" <---- WHY IS THIS NOT 'merge_countries'???
#hidden: array:2 [
0 => "created_at"
1 => "updated_at"
]
#appends: array:1 [
0 => "level"
]
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:7 [
"id" => 3
"organization_id" => 1
"region_id" => 2
"name" => "Southpark"
"active" => 1
"created_at" => "2018-06-21 14:05:36"
"updated_at" => "2018-06-21 13:25:27"
]
#original: array:7 [
"id" => 3
"organization_id" => 1
"region_id" => 2
"name" => "Southpark"
"active" => 1
"created_at" => "2018-06-21 14:05:36"
"updated_at" => "2018-06-21 13:25:27"
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#visible: []
#fillable: []
#guarded: array:1 [
0 => "*"
]
}
]
}
Any idea how to address this? We're not sure what else to even try at this point. Laravel if 5.6 if that matters.
OK, there is a fix reference and more detail at github.com/laravel/framework/issues/26058. In the interim you can also override the model newInstance method in the trait to work around it:
public function newInstance($attributes = [], $exists = false)
{
// Overridden in order to allow for late table binding.
$model = parent::newInstance($attributes, $exists);
$model->setTable($this->table);
return $model;
}
In my Laravel App, a poll has many poll options. I can create a collection of those options by simply running:
$result = Poll::find(1)->options()->get();
This query returns:
Collection {#387 ▼
#items: array:6 [▼
0 => PollOption {#388 ▼
#table: "poll_options"
#connection: null
#primaryKey: "id"
#keyType: "int"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:8 [▼
"id" => 1
"poll_id" => 1
"option_text" => "Never"
"responses" => 54
"is_public" => 0
"created_at" => null
"updated_at" => null
"deleted_at" => null
]
#original: array:8 [▶]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [▶]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
1 => PollOption {#389 ▶}
2 => PollOption {#390 ▶}
3 => PollOption {#391 ▶}
]
}
Within my poll option, there is a column called responses, which returns an integer. I need to take the collection above and isolate the responses key/value pair.
Collection {#385 ▼
#items: array:6 [▼
"responses" => 54
"responses" => 20
"responses" => 10
"responses" => 123
"responses" => 33
"responses" => 11
]
}
The only() collection method does exactly what I need, but I can't figure out how to structure the query when working with Eloquent models:
$options = Poll::find(1)->options()->get();
$responses = $options->only('responses');
dd($responses->all());
Returns an empty collection because the responses value is nested within the PollOption object.
I've also tried flatten(), but that seems to have no effect in this scenario.
Is there an easier way to return a single key/value pair within an Eloquent model collection?
It's not actually possible to assign the same key to multiple values within a collection. I knew this, but I wasn't thinking the problem through properly.
For future devs: the pluck() method takes two arguments: the value you want to pluck, and a second value that gets assigned to the key. Using the scenario above, I was able to write:
$result = $poll->options()->pluck('responses', 'option_text'); //Value and corresponding key
This results in something like:
Collection {#386 ▼
#items: array:6 [▼
"Never" => 54
"Occasionally" => 21
"2–3 times a week" => 80
"4–5 times per week" => 33
"Daily" => 11
]
}
Which ultimately did what I need it to do. Amit Gupta's mapWithKeys() answer is also correct, and will land you in the same place.
I have an array that looks like this when dd'd out :)
array:2 [▼
0 => Comment {#253 ▼
#guarded: []
#table: "comments"
+timestamps: false
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
#attributes: array:7 [▶]
#original: array:7 [▼
"id" => "1"
"on_projects" => "2"
"from_user" => "19"
"body" => "hi"
"unread" => "1"
"created_at" => "2016-06-13 23:54:39"
"updated_at" => "0000-00-00 00:00:00"
]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
1 => Comment {#269 ▶}
]
Now my goal is to count the amount of where the unread key is true.
This is what I've tried but doesn't seem to be working.
$unreads = $comments->where('unread', 1);
dd(count($unreads));
I get this error:
Call to a member function where() on array
Anyone can help me out here?
Many thanks in advance!
EDIT
$uid = Auth::user()->id;
$projects = User::find($uid)->projects;
//comments
if (!empty($projects)) {
foreach ($projects as $project) {
$comments_collection[] = $project->comments;
}
}
if (!empty($comments_collection)) {
$comments = array_collapse($comments_collection);
$unreads = collect($comments)->where('unread', 1)->count();
dd($unreads);
}
This gives me 0 it should give me 2 since I have 2 comments with unread 1
After you've retrieved the data you can collect, filter and count using Illuminate\Collection.
collect($comments)->where('unread', 1)->count();
That said, if your goal is to simply count the data and do nothing else with it, you could achieve this using Eloquent as presumably you've already used a similar query to get the data in the first place.
Comments::where('unread', 1)->count();
Amends based on OP edit.
$projects = Project::with(['comments'])
->where('user_id', Auth::id())
->get();
$count = $projects->sum(function ($project) {
return $project->comments->where('unread', 1)->count();
});
dd($count);
Having thought a bit more about it, as you just want a count (it seems), you can add a relation and an accessor to your Project model.
public function commentsCountUnread() {
return $this->hasOne(Comment::class)
->selectRaw('project_id, count(*) as aggregate')
->where('unread', 1)
->groupBy('project_id');
}
public function getCommentsCountUnreadAttribute()
{
if (! $this->relationLoaded('commentsCountUnread')) {
$this->load('commentsCountUnread');
}
$related = $this->getRelation('commentsCountUnread');
return ($related) ? (int) $related->aggregate : 0;
}
Then you could do something like
$posts = Project::with('commentsCountUnread')->get();
$count = $projects->sum(function($project) {
return $project->commentsCountUnread;
});
dd($count);
Before query all the information from DB I want to check if a row of a table was insert or not and if there are no any new insert I will not use query method.But I'm much understand about how to check up if it was inserted or not in Laravel5 with Eloquent library.
I've research some tutorial I found as below sql sentence but I don't how to used it in Eloquent and this sql is work as my expected or not.
On checkingIfNotificationUpdated method I want to checking for true and false when a row was insert into that tables only but as this sentence it will query all data
public function checkingIfNotificationUpdated(){
// SELECT * FROM INFORMATION_SCHEMA.'.$this->table.' WHERE DATE_SUB(NOW(), INTERVAL 1 HOUR) < UPDATE_TIME
RETURN self::select('*')->where(DB::raw('DATE_SUB(NOW(), INTERVAL 1 HOUR)'),'<','UPADTE_TIME')->get();
}
Here is my query method within checking Notification table is insert or not
public function getNotification($user_id, $gId)
{
if($this->checkingIfNotificationInserted() == true){
$this->_data = self::select('*')->join('users', 'users.id', '=', 'n_user_id')
->where('n_group_code_id','=', $gId)
->get();
if (count($this->_data)) {
return $this->_data;
} else {
return false;
}
}else{
// to go with old data.
}
}
Here is data my respond data when Used dd()
Collection {#619 ▼
#items: array:1 [▼
0 => Notification {#620 ▼
#table: "notification"
+timestamps: true
-_data: false
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
#attributes: array:11 [▼
"n_id" => 269
"n_user_id" => 69
"n_group_code_id" => 11
"n_source_id" => 231
"n_activity_type" => "Issue Till"
"n_create_times" => "2016-04-06 09:04:40"
"n_description" => "Issue Till"
"n_status" => 0
"url" => "teller/trans_issues"
"updated_at" => "2016-04-06 09:45:40"
"created_at" => "2016-04-06 09:45:40"
]
#original: array:11 [▼
"n_id" => 269
"n_user_id" => 69
"n_group_code_id" => 11
"n_source_id" => 231
"n_activity_type" => "Issue Till"
"n_create_times" => "2016-04-06 09:04:40"
"n_description" => "Issue Till"
"n_status" => 0
"url" => "teller/trans_issues"
"updated_at" => "2016-04-06 09:45:40"
"created_at" => "2016-04-06 09:45:40"
]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [▼
0 => "*"
]
#dates: []
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
}
]
}
i think you can check with your input fields when you inserted:
Here,
in Controller :
$input = $request->all();
//check input value if exists or not in db...
$b_exists = ModelClass::where('title','=',$input['title'])->exists();
if($b_exists){
//show message: alteady exists
}
else{
//
....new data inserted
}
If you are using Eloquent there is no need to query back to the DB.
Assume you do an insert like this:
$myModel = ModelClass::create($params);
or this:
$myModel = new ModelClass($params);
$myModel->save();
You can then do this check:
if (! empty($myModel->id)) {
// Model has been successfully inserted
}
I'm new to Laravel and I cannot figure out how to get the value of an item from within the returned collection. Here is my code:
$aircraft = Aircraft::join('pams_shared.shared_aircraft', 'aircraft.shared_aircraft_id', '=', 'pams_shared.shared_aircraft.shared_aircraft_id')
->select('aircraft.*', 'shared_aircraft.*')
->where('aircraft.owner_id',Auth::user()->owner_id)
->where('registration',$reg)
->get();
I want to get the value from aircraft.aircraft_id but that's where I get stuck. I've tried:
$id = $aircraft->aircraft_id;
I've tried:
$id = $aircraft->aircraft.aircraft_id;
I've also tried:
$id = array_get($aircraft, 'aircraft_id');
But I'm getting null as a value. When I perform either a var_dump($aircraft) or dd($aircraft) I'm able to confirm that the data is there.
In my view I am able to access the value by looping through, but I need this data in my controller to perform a second query on another table.
Hope that makes sense. Cheers.
EDIT
When running dd($aircraft); I get this:
Collection {#239 ▼
#items: array:1 [▼
0 => Aircraft {#240 ▼
#fillable: array:8 [▶]
#table: "aircraft"
#primaryKey: "aircraft_id"
#connection: null
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:16 [▼
"aircraft_id" => 5
"owner_id" => 2
"shared_aircraft_id" => 67443
"year" => 2012
"serial_number" => "C127RG3"
"registration" => "C-SMTH"
"created_by" => 2
"modified_by" => 2
"created_at" => "0000-00-00 00:00:00"
"updated_at" => "0000-00-00 00:00:00"
"aircraft_code" => "2072438"
"aircraft_mfr" => "CESSNA"
"aircraft_model" => "172RG"
"aircraft_type" => 4
"aircraft_eng" => 1
"aircraft_cat" => 1
]
#original: array:16 [▶]
#relations: []
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#dates: []
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
}
]
}
When you use get() method on a query builder it would return an array no matter how many record it gets. So you can access it by pointing it by index. By the way do not forget to check for emptiness to overcome possible errors.
$id = 0;
$aircraft = Aircraft::join('pams_shared.shared_aircraft', 'aircraft.shared_aircraft_id', '=', 'pams_shared.shared_aircraft.shared_aircraft_id')
->select('aircraft.*', 'shared_aircraft.*')
->where('aircraft.owner_id',Auth::user()->owner_id)
->where('registration',$reg)
->get();
// now you can access properties
if(count($aircraft) > 0) $id = $aircraft[0]->aircraft_id;
If you execute the query with get() you will always get a collection as result. Even if you just need one model. The simplest way to change that is to use first():
$aircraft = Aircraft::join('pams_shared.shared_aircraft', 'aircraft.shared_aircraft_id', '=', 'pams_shared.shared_aircraft.shared_aircraft_id')
->select('aircraft.*', 'shared_aircraft.*')
->where('aircraft.owner_id',Auth::user()->owner_id)
->where('registration',$reg)
->first();
$id = $aircraft->aircraft_id;