I have a problem with displaying data from tables, and more accurately displays me a json object.
How can I fix this because I need to display only the titles.
I tried json_decode, but throws out an error that a string was expected, an array was passed
AppServiceProvider:
view()->composer('*', function($view){
$results = [];
$results['zone'] = Zone::all()->last();
$results['tourism'] = Tourism::all()->last();
$results['business'] = Business::all()->last();
$results['culture'] = Culture::all()->last();
$results['education'] = Education::all()->last();
$results['inhabitant'] = Inhabitant::all()->last();
$results['investor'] = Investor::all()->last();
$results['senior'] = Senior::all()->last();
$results['sport'] = Sport::all()->last();
$view->with('sidebar', $results);
});
Sidebar.blade.php:
#foreach($sidebar as $type => $list)
#include('pages.sidebar-components.'. $type, ['items' => $list])
#endforeach
Forexample one sidebar-component view (Business.blade.php):
{{ $list }}
Results:
{"id":1,"title":"test","description":"test","path":"dsadsa"}
I need:
{id:1,title:"test",description:"test",path:"dsadsa"}
I've tried to decode json, but I get the error of the expected string, passed array
O my God...
I just removed ->last(); from Sport::all()->last(); and it worked. Solved, to close.
Related
Trying to save data while open page but stuck at error :
"Method Illuminate\Database\Eloquent\Collection::save does not exist."
I have 2 database :
Buffalodata
Buffalomilkrecord
From 2nd table i need to get the avg of totalmilk and update the same to main database (1). This help me to show updated avgmilk data on dashboard front page.
Route:
Route:: get('buffalo-details', 'App\Http\Controllers\BuffalodataController#buffalodetails');
BuffalodataController Controller :
public function buffalodetails()
{
$buffalidforavgmilk = Buffalodata::groupBy('buffaloID')->get('buffaloID')->pluck('buffaloID')->toArray();
foreach ($buffalidforavgmilk as $id )
{
$milkperid = Buffalomilkrecord::where('buffaloID', $id)->sum('totalmilk');
$avgbuffalocount = Buffalomilkrecord::where('buffaloID',$id)->count();
$getavg = $milkperid / $avgbuffalocount;
$data = Buffalodata::find($buffalidforavgmilk);
$data->avgmilk = ($getavg);
$data->save ();
// dump([$milkperid,$avgbuffalocount,$getavg,$data,$id]);
}
return view ('pages.Buffalo.BuffaloDetails',[---------]);
}
Thanks again in Advance
When you pass an Array to ::find(), it returns a Collection, which doesn't have a save() method. This is your code:
// This is an Array of `buffaloID` values
$buffalidforavgmilk = Buffalodata::groupBy('buffaloID')->get('buffaloID')->pluck('buffaloID')->toArray();
...
// `$data` is now a `Collection` of `Buffalodata` instances
$data = Buffalodata::find($buffalidforavgmilk);
// This now fails, as `Collection` doesn't have a `save()` method
$data->save();
You can rewrite your code as follows:
Buffalodata::whereIn('buffaloID', $buffalidforavgmilk)->update(['avgmilk' => $getavg]);
This will update all records in a single call. If you want to iterate, that's an option too:
$data = Buffalodata::find($buffalidforavgmilk);
foreach ($data as $record) {
$record->avgmilk = $getavg;
$record->save();
}
Or, since you have $id already:
$record = Buffalodata::find($id);
$record->avgmilk = $getavg;
$record->save();
I am using willvincent feed reader to parse rss feeds, But i cannot seem to get the thumbail of the images,
Here is my code
Route::get('feed', function(Request $request) {
$f = FeedsFacade::make('http://www.cbn.com/cbnnews/us/feed/');
// $results = [
// 'image' => $f->get_image_url(),
// ];
foreach($f->get_items(0, $f->get_item_quantity()) as $item) {
$i['title'] = $item->get_title();
$i['thumbnail'] = $item->get_thumbnail();
$i['description'] = $item->get_description();
$i['content'] = $item->get_content();
$i['link'] = $item->get_link();
$i['date'] = $item->get_date();
$results['items'][] = $i;
}
dd($results);
})->name('feed');
Thumbnail always return null, will appreciate anyone's help
Here is how we call image and other contents from rss feed
NOTE: To get image, it is important that the site you fetching rss feeds, use to provide image too in their feed. Else won't get image in your fetched feed (there might be some trick to still fetch image but I am not aware of it for now). For example, google news feed don't provide image so you won't get image from google news feed.
Here is example of one site who use to provide image.
//In your CONTROLLER file
$feed = Feeds::make('https://globalnews.ca/feed/');
$data = array(
'title' => $feed->get_title(),
'permalink' => $feed->get_permalink(),
'items' => $feed->get_items(),
);
return view('view_file', $data)
//OR in case you are calling array value to show on view then it would be like this
return view('view_file', array(
'name' => $var
), $data) //notice "$data" at end of array.
Now after done on controller part, this is how you will call in view file
#foreach ($items as $item)
//GET IMAGE
#if($enclosure = $item->get_enclosure())
<img src="{{$enclosure->get_thumbnail()}}">
#endif
//GET TITLE
<div class="news-title">{{ $item->get_title() }}</div>
//GET DESCRIPTION
{{$item->get_description()}}
//NOTE: this will bring html. TO show as output instead of html you can either use {!! !!) or "strip_tags" function. And "substr" function to show first 100 characters only as small description
{{ substr(strip_tags($item->get_description()), 0, 100) }}
//GET LINK/URL OF NEWS
Read More
#endforeach
Feed which whose example I shown and also reference
https://packagist.org/packages/willvincent/feeds
I have seen many same question about this problem but I can't figure out how to fix mine
so I'm trying to save multiple checkbox and I think this one will work but now I get "must be of the type array, string given".
By the way this is my controller
public function store(Request $request)
{
// $multi = Multi::create($request->only(['data'])->implode(', '));
// $multi = Multi::select('data')->implode();
$multis = implode(',', $request->get('data'));
$multis = Multi::create(['data' => $request->get('data')]);
return redirect()->route('multi.create')->with('success', 'berhasil.');
and this is my create.blade.php
#foreach($multis as $multi)
{{$multi['data']}}<br>
#endforeach
<br>----------------<br>
{{Form::open(['action'=>'MultiController#store'])}}
{{Form::checkbox('data[]','A')}}A<br>
{{Form::checkbox('data[]','B')}}B<br>
{{Form::checkbox('data[]','C')}}C<br>
{{Form::submit('TAMBAH')}}
{{Form::close()}}
Try this
$multis = implode(',', $request->data));
$multis = Multi::create(['data' => $multis]);
You have to add fields name as a key of array and define all the data into different variables like this this will work.
$multis1 = implode(',', $request->d);
$multis2 = implode(',', $request->i);
$multis3 = implode(',', $request->s);
$multis4 = implode(',', $request->c);
$multis= Test::create(array('d' => $multis1,'i' => $multis2,'s' => $multis3,'c' => $multis4));
https://api.cinelist.co.uk/get/times/cinema/10565
This link gives me a json list of title and times that the cinema is playing that specific film. I want to get that information and convert it to string however how is that possible? I have used json_decode and it says that node list is empty.
Here's my code:
function odeon(){
$client = new Client();
$crawler = $client->request('GET', 'https://api.cinelist.co.uk/get/times/cinema/10565');
//print $crawler->text()."\n";
json_decode($crawler);
print_r($crawler);
}
file_get_contents() does the trick for simple things like these.
function odeon(){
$data = file_get_contents('https://api.cinelist.co.uk/get/times/cinema/10565');
$data = json_decode($data);
return view()->make('odeon')->with(['listings' => $data->listings]);
}
and then in your blade simply do something like this:
#foreach($listings as $listing)
<strong>{{$listing->title}} </strong>:
#foreach($listing->times as $time)
<p>{{$time}}</p>
#endforeach
#endforeach
I want create a DOMPDF with laravel, and I must passing my variable to view. I've been try passing variable like below, but it still not working yet.
here my Laravel Controller
public function pdf(Request $request, $id){
$salesorder = $this->show($id)->salesorder;
$detailservice = $this->show($id)->detailservice;
$detailemployee = $this->show($id)->detailemployee;
$data = [$salesorder, $detailemployee, $detailservice];
$pdf = PDF::loadView('summary.invoice', $data);
return $pdf->download('invoice.pdf');
}
the error on my view is :
Undefined variable: salesorder
How to passing some variable from Laravel controller to DOMPDF ?
PS : dd($data) result is correctly
You have to pass the data as below
$data = [
'salesorder' => $salesorder,
'detailemployee' => $detailemployee,
'detailservice' => $detailservice
];
or try using compact
$data = compact('salesorder', 'detailemployee', 'detailservice');
You may try this following
public function pdf(Request $request, $id){
$salesorder = $this->show($id)->salesorder;
$detailservice = $this->show($id)->detailservice;
$detailemployee = $this->show($id)->detailemployee;
$pdf = PDF::loadView('summary.invoice', array('salesorder' => $salesorder,'detailemployee'=>$detailemployee,'detailservice'=>$detailservice));
return $pdf->download('invoice.pdf');
}
You can use library function to do that easily.
$pdf_text = "It will be the text you want to load";
PDF::loadHTML($pdf_text)->save('public/you-file-name.pdf');
You can change the orientation and paper size, and hide or show errors (by default, errors are shown when debug is on)
PDF::loadHTML($pdf_text)->setPaper('a4', 'landscape')->setWarnings(false)->save('public/you-file-name.pdf')
You may try the following.
$html = view('summary.invoice', ['salesorder' => $salesorder, 'detailemployee' => $detailemployee, 'detailservice' => $detailservice])->render();
$pdf = App::make('dompdf.wrapper');
$invPDF = $pdf->loadHTML($html);
return $pdf->download('invoice.pdf');
I know this is old but this may help others. you need to use array key in your view.
Controller:
$data = [
'foo' => $bar,
];
$pdf = PDF::loadView('pdf.document', $data);
return $pdf->stream('doc.pdf');
View:
<p>{{$foo}}</p>