I'm trying to get data saved in json format inside database column
Data:
[
{
"option_value":"Delhi",
"optionl2_value":"\u0926\u092f\u093e\u0930\u093e\u092e \u0938\u093e\u0939\u0928\u0940",
"has_file":0,
"file_name":""
},
{
"option_value":"Panjab",
"optionl2_value":"\u0930\u093e\u0916\u0932\u0926\u093e\u0938 \u092c\u0928\u0930\u094d\u091c\u0940",
"has_file":0,
"file_name":""
}
]
and here is the code i'm using.
#if(is_array(json_decode($question->answers)))
#foreach ( json_decode($question->answers) as $value)
<span> {{ $value->option_value }}</span><br>
#endforeach
#endif
and I get the following error
> Error : Undefined property: stdClass::$option_value
How can I resolve?
This works for me:
<?php
$data = json_decode($jsondata);
?>
<ul>
#foreach($data as $value)
<li> {{ $value->city}}</li>
#endforeach
</ul>
Related
In my laravel application, I'm having a list of regions in my DB
This is my controller method
public function index()
{
$region_options = Region::get();
return view('home',compact('region_options'));
}
and I have following in my blade,
#if(!$region_options->isEmpty())
<ol class="region-pop">
#foreach ($region_options as $key => $region)
<li>{{ $region->name }}</li>
#endforeach
</ol>
#endif
But when I run this it gave me an error saying, Undefined variable: region_options
I'm struggling to find what the issue is...
i have live json and i want to display it on laravel blade
my controller code is :
public function index()
{
$json = json_decode(file_get_contents('link_of_json_file'),true);
return view('show' , compact('json'));
}
and blade code is :
#foreach ($json as $p)
{{ $p->name }}
#endforeach
and this erros is display:
(2/2) ErrorException
Trying to get property of non-object (View:
E:\work-landingpage\test\JSON\resources\views\show.blade.php)
you can remove the "true" to use it as object :
public function index()
{
$json = json_decode(file_get_contents('link_of_json_file'));
return view('show' , compact('json'));
}
#foreach ($json as $p)
{{ $p->name}}
#endforeach
so if you want to use it as array you can use it like:
#foreach ($json as $p)
{{ $p['name']}}
#endforeach
I have two tables and i would like to retrieve data from them and pass it to my table.
For this I've created 2 models with an one to one relationship:
[Adress]
class Adress extends Model
{
public function KontoKorrent()
{
return $this->hasOne(KontoKorrent::class, 'Adresse');
}
}
[KontoKorrent]
class KontoKorrent extends Model
{
public function Adresse()
{
return $this->belongsTo(Adress::class,'Adresse');
}
}
My controller look like this:
class AdressesController extends Controller
{
public function index()
{
$adresses = Adress::with('KontoKorrent')->paginate(2);
return view('welcome', compact('adresses'));
}
}
When I use tinker
App\Adress::
Every adress has relation to the kontokorrent. This is working.
App\Adress {#698
Adresse: "3030",
Anrede: "Company",
Name1: "A Company Name",
LieferStrasse: "Dummystreet",
KontoKorrent: App\KontoKorrent {#704
Location: "1",
Adresse: "3030",
Kto: "S0043722",
In my view:
<ul>
#foreach($adresses as $adress)
<li>{{ $adress->Name1 }}</li> //this is working
<li>{{ $adress->KontoKorrent->Kto }}</li> //this is NOT working
#endforeach
</ul>
{{ $adresses->links() }}
The relation is showing me an error:
Trying to get property of non-object
What I'm doing wrong ?
The error that you are getting:
Trying to get property of non-object
Is related to some Adress model that doesn't have a KontoKorrent, then your $adress->KontoKorrent returns null, and null isn't a object, that the reason of the message.
To fix it, you should do an if to check if adress have the relationship:
<ul>
#foreach($adresses as $adress)
<li>{{ $adress->Name1 }}</li> //this is working
<li>
#if($adress->KontoKorrent)
{{ $adress->KontoKorrent->Kto }}
#else
<!-- Put something here if you want, otherwise remove the #else -->
#endif
</li> //this is NOT working
#endforeach
</ul>
This can be shortened to:
{{ $adress->KontoKorrent ? $adress->KontoKorrent : 'the else content' }}
or in PHP >= 7.0, you can use the null coalesce operator:
{{ $adress->KontoKorrent ?? 'the else content' }}
I am a Laravel newbie. I want to pass the results of a database query to a view. I get an error message "Use of undefined constant tasks - assumed 'tasks'". What am I doing wrong?
My code is as follows:
class TasksController extends BaseController{
public function index(){
$tasks = Task::all();
//return View::make(tasks.index, ['tasks' => $tasks]);
return View::make(tasks.index, compact('tasks'));
}
A snippet from my template page is shown below:
<body>
<h1>All tasks!</h1>
#foreach($tasks as $task)
<li>{{ $task-title }} </li>
#endforeach
return View::make('tasks.index')->with(compact('tasks'));
also change:
<li>{{ $task-title }} </li>
to
<li>{{ $task->title }} </li>
should be like this.
Try this,
return View::make(tasks.index, $tasks);
instead of
return View::make(tasks.index, compact('tasks'));
I've searched for all solutions to implement pagination successfully, but nothing I've found worked. I'm new to Laravel so I have maybe only one of those basic misunderstandings.
I have created it like it is described in the current documentation. And I get the first page successfully, exactly the content i need, but when i try to use the pagination, clicking on the "next"-arrow, I get an error message:
ErrorException
Undefined variable: reviews (View: D:\Xampp\htdocs\laravel\app\views\search.blade.php)
I know what this generally means but absolutely no idea how to solve this.
This is the form in the view file:
{{ Form::open(array('url' => '/search', 'class' => 'search', 'method' =>'GET')) }}
{{ Form::text('title') }}
{{ Form::submit('Suche')}}
{{ Form::close() }}
Here is my routes configuration:
Route::any('/search', array('uses' => 'HomeController#search'));
Here is my function in the HomeController:
public function search() {
$input = Input::get('title');
if($input && $input != ''){
$games = DB::table('games')->where('name', 'LIKE', "%$input%")->get();
foreach ($games as $game) {
$reviews = DB::table('reviews')->whereGame_id($game->id)->paginate(3);
}
}
return View::make('search', compact('reviews', 'input', 'games'));
}
Here is the view-code of the search.blade.php view, which displays the results:
#section('content')
<h2>Suchergebnisse</h2>
#foreach ($reviews as $review)
#foreach ($games as $game)
#if ($game->id == $review->game_id)
<h3> {{ $game->name }} </h3>
#endif
#endforeach
<p>{{ (preg_replace('/\n/i', '<br/>', $review->body)) }}</p>
#endforeach
{{ $reviews->appends(array('term' => $input))->links() }}
#stop
I thank you beforehand if someone knows why Laravel has a problem with this solution.
Edit:
I've changed the compact-statement and replaced it with an simple array, just like that:
return View::make('search', array('reviews' => $reviews, 'input' => $input, 'games' => $games));
Then I've added global $games; global $reviews; to the Controller, so the Error "Undefined variable: reviews" disappeared. Now I've got the following message, but only on page 2, page 1 works fine:
ErrorException
Invalid argument supplied for foreach() (View: D:\Xampp\htdocs\laravel\app\views\search.blade.php)
First of all you are doing this
foreach ($games as $game) {
// $reviews is being updated every time in loop
$reviews = DB::table('reviews')->whereGame_id($game->id)->paginate(3);
}
So, every time there is a new value is being set to $review but it should be an array and that's why you should declare an empty array before the loop like this
$reviews = []; // or array();
foreach ($games as $game) {
$reviews[] = DB::table('reviews')->whereGame_id($game->id)->paginate(3);
}
So, you'll get an array in the $review variable.
Regarding your error message Undefined variable: reviews, it's not defined and it could be because if($input && $input != '') condition doesn't match or $games got nothing, so, try to debug step by step your code, at first make sure that execution control is being entered inside if by making dd($input) statement inside if and then if it outputs anything then make sure $games is not null/false by making a dd($games) statement after the games query.
You can change the code snippet to this
$input = Input::get('title');
if($input) {
$games = DB::table('games')->where('name', 'LIKE', "%$input%")->get();
if($games) {
$reviews = []; // or array();
foreach ($games as $game) {
$reviews[] = DB::table('reviews')->whereGame_id($game->id)->paginate(3);
}
return View::make('search', compact('reviews', 'input', 'games'));
}
}
// return a 404 (not found) or something like this or whatever, when nothing found