I'm building a Laravel 9x application where i will need to populate some pages with the google places API.
Reading at the ToS of Google Places, i can only store in my db, for at most 30 days, the place_id.
Now, I'm facing an issue; I have a restaurants.index blade page where I'm showing in a table all the records from the restaurants table.
The issue is that i need to show in each table row the name of the restaurant that i don't have in my db table but i need to fetch it from Google Places Api.
So far, in my RestaurantsController I have done the following :
public function index()
{
$getAllRecords = Restaurant::all();
$google_places_api = new PlacesApi(env('GOOGLE_MAPS_API_KEY'));
$restaurants = new Collection();
foreach($getAllRecords as $restaurant){
$response= $google_places_api->placeDetails($restaurant->google_place_id);
$restaurant['name'] = $response['result']['name'];
$restaurants->prepend($restaurant);
}
dd($restaurants);//When dd here, the collection shows correctly the added value.
$restaurants->paginate(8);
$categories = Category::where('type', 'Restaurant')->cursor();
return view('tenant.restaurants.index', compact('categories', 'restaurants'));
}
And it seems to work pretty well if I dd($restaurants) inside the RestaurantsController as you can see from the picture below :
But when i try to #dd($restaurants) from the restaurants.index page, the added name is completely disappeared as you can see from the picture below :
It's now about a whole day that I'm tryng to understand why this behavior happen, is there anybody who has an idea of what is happening?
Thank you
Related
I am trying to create a trending or popular list of categories in my application but not sure how it works or if what i have works as it should. In my category table, i added a view_count column. In the php block i have
$viewed = Cat::where('parent_id','>',0)->first();
$viewed->increment('view_count',1);
$viewed->save();
$this['trending'] = Cat::orderBy('view_count', 'desc')->take(10)->get();
How do i correctly do this and add session to it so it does not count with each reload by a user?
Edit: I currently have this looking at how it is done in laravel. Dont know if it works differently with october
public function show(Cat $cat)
{
$id = $cat->id;
$cat = Cat::find($id)->where('parent_id', '>', 0)->increment('view_count');
$this['trending'] = Cat::orderBy('view_count')->take(10)->get();
}
I am assuming you would have category_id on trending category page so your code for incrementing view count must have been
Cat::find($category_id)->increment('view_count');
and that's it.
let me know if you need more help
I'm using Laravels Consoletvs\charts to generate charts for my project. I'm trying to get the chart to only display the "question" created by a specific user, currently it just displays all the "questions" that are in the database, but I'm unsure on how to write the string for this, basically I need it to only show the "question" that match the id of the user that is currently logged in.
Any help is much appreciated :)
My chart controller is shown below
$questions = Question::where(DB::raw("(DATE_FORMAT(created_at,'%Y'))"),date('Y'))
->get();
$questionChart = Charts::database($questions, 'bar', 'highcharts')
->title("Questions Assigned")
->elementLabel("Total Questions")
->dimensions(1000, 500)
->responsive(true)
->groupByMonth(date('Y'), true);
return view('welcome', compact('userChart', 'testChart', 'questionChart'));
}
}
If I'm not wrong then you want to display the questions created by the logged in user.
To get a solution you must have a relation predefined between the questions and users table.
I'm assuming that you have a user_id column in your Question Model
$loggedin_user_id = 'some_id';
Try this
$questions = Question::where('user_id', $loggedin_user_id)
->get();
I am developing an application in Laravel 5.5 ... it is kind of a testing system...
What I want to achieve is that when user clicks on RUN TEST, it will generate random set of questions (let's say 5) then I want them to be served to the user one by one ... I did a pagination in AJAX so the questions can be served by pages .. and after that user will click on SUBMIT which will submit the form ..
I am able to get the random list of questions
In the TestController within the method "exec_test" is
// get random id
$q_id = Question::select('id')
->where('test_id','=',$test_id)
//->where('id','=',114)
->orderByRaw("RAND()")
->take(3)
->get();
This will get 3 random ID's for that specific test ...
Following code in the same method is
// get data
$data = array (
'tests' => Test::select('id','code','name',DB::raw('duration_m * 60 as duration_m'),'questions','passing_score')
->where('id','=',$test_id)
->orderBy('code', 'asc')
->paginate($this->page_limit),
'test_questions' => Question::select('questions.id','questions.question_header','questions.question_detail_local_url')
->join('tests','tests.id','=','questions.test_id')
->where('test_id','=',$test_id)
->whereIn('questions.id',$q_id)
//->orderByRaw("RAND()")
//->take(4)
->paginate($this->test_question_page_limit),
'test_answers' => Answer::select('id','answer_text_cleaned')
->whereIn('question_id',$q_id)
->get()
);
// check for ajax requests
if ($request->ajax()) {
return view('/custom/test/run_test_quests', $data)->render();
}
return view('/custom/test/run_test_info',$data);
But I don't know how can I make that first query static and to make it served one by one (per page) to the user .. so he can go trough all questions
Do you have any suggestions please?
while paginating the first page would be 1, it would be specified in the url too.
try this:
if($request->input('page') ==1)
{
//code for first page
}else{
//code for random
}
this is just a suggestion. Sorry if I understood the question wrong
I have an User which has an Avatar and an Armory. The User can only have one Avatar (at the moment) and the Armory can hold any number of Armor. This all works great. However, the Avatar needs to have a Head, Chest, Arms, etc so I created head_id, chest_id, etc on the avatar table. Basically those represent the current piece they are wearing in each position. The Armory is the pieces they can choose from.
So in my Avatar model I have:
public function head()
{
return $this->hasOne('Armor', 'id', 'head_id')
}
Which works just fine and returns me the piece of armor. The issue becomes when I POST data and try to change that piece of armor. What I do is:
$this->head_id = $newHeadID;
Which does actually change head_id, but if I then do $this->head(), it still returns the old head. I've tried $this->save() and $this->push() in-between, but it still doesn't update it immediately. The only way that seems to work is to entirely "re-query" like such:
$head = Armor::where('id', $this->head_id)->first();
I'm pretty sure I'm missing something like not doing an update, refresh, etc. Any ideas?
I'm trying to find out what store a page belongs to in Magento. We currently have along these lines:
$cms_pages = Mage::getModel('cms/page')->getCollection();
$cms_pages->load();
foreach($cms_pages as $_page) {
$data = $_page->getData();
}
How would I get the store ID for each page? Ideally I'd like something as simple as $data->store_id(); but I've not found anything of use yet. Can anyone point me in the right direction?
Add Store id to your collection and it will only return the pages you need:
$collection = Mage::getModel('cms/page')->getCollection()
->addStoreFilter(Mage::app()->getStore()->getId())
->setOrder('sortorder', 'asc');