How do i retrieve the content data to blade from this json - laravel

i'm encountered with an blade issue.
[{"banner_type":"2","content":"[\"e6aaaff3ae42af3af3a9cb6a45845da8f905c906Screenshotfrom2015-01-1917:17:31.png\",\"559a0e13473a22ac5bde041c5260257a12d1811eScreenshotfrom2015-01-2115:16:01.png\",\"559a0e13473a22ac5bde041c5260257a12d1811eScreenshotfrom2015-01-2208:49:53.png\",\"559a0e13473a22ac5bde041c5260257a12d1811eScreenshotfrom2015-01-2212:36:42.png\"]"}]
This is the json i'm getting from webservice, i need to get each image url and need to assign to a tag . how could i achive it?

#foreach($json->content as $img)
<img src="{{$img}}" alt="alt text">
#endforeach
From the information provided that is the most I can give you.

For the given json, you can try something like this on your controller:
public function index()
{
$json_string = '[{"banner_type":"2","content":"[\"e6aaaff3ae42af3af3a9cb6a45845da8f905c906Screenshotfrom2015-01-1917:17:31.png\",\"559a0e13473a22ac5bde041c5260257a12d1811eScreenshotfrom2015-01-2115:16:01.png\",\"559a0e13473a22ac5bde041c5260257a12d1811eScreenshotfrom2015-01-2208:49:53.png\",\"559a0e13473a22ac5bde041c5260257a12d1811eScreenshotfrom2015-01-2212:36:42.png\"]"}]';
$json = json_decode($json_string, true);
$images = json_decode($json[0]['content']);
return view('index', compact('images'));
}
Then, on your blade view:
#foreach($images as $image)
<img src="$image">
#endforeach

Related

Undefined variable: foods

hi guys am in need of assistance , i know this seems to be an easy one but am a bit confused , i have a foreach loop in my main.blade.php file, which shows foods from my database it works fine but when its clicked it meant to lead to the individual post and thats where i get the error
Undefined variable: foods
heres my foreach loop in main.blade.php file
#foreach($foods as $food)
<li class="item">
<a href="{{ route('Foods.show', $food->id) }}">
<img src="{{ Storage::disk('local')->url('food_images/'.$food->image ) }}" class="img-responsive w-25 p-3" alt="Food" >
<div class="menu-desc text-center">
<span>
<h3> {{ $food->title }}</h3>
{{ $food->body }}
</span>
</div>
</a>
<h2 class="white"> #{{ $food->price }}</h2>
</li>
#endforeach
heres my main.blade.php controller
public function LoadHome()
{
$foods = Food::all();
$foods = Food::orderBy('created_at','desc')->inRandomOrder()
->limit(12)
->get();
return view('pages.home')->withFood($foods);
}
and heres Foods.show controller function
public function show($id)
{
$foods = Food::Find($id);
return view('foods.show')->withFood($foods);
}
please what am i doing wrong
Have you tried passing data from your controller to the view using this something like this:
public function show($id)
{
$foods = Food::Find($id);
return view('foods.show')->with('foods', $foods);
}
or, if you're passing multiple variables to the view:
public function show($id)
{
$foods = Food::Find($id);
$something = "else";
return view('foods.show', compact('foods', 'something'));
}
Your view doesn't know what $foods is at that point, so it's always good practice to check that foods is set before the loop:
#if (isset($foods) && $foods->count() > 0)
#foreach($foods as $food)
...
#endforeach
#endif
See the official Laravel docs for more information.
If you want the variable to be named foods in the view then you would need to use withFoods not withFood:
return view(...)->withFoods($foods);
As mentioned in the other answers, there are other ways to pass data to the views as well.
There is no data being passed to a view.
This is how you pass data to a view:
public function LoadHome()
{
$foods = Food::orderBy('created_at','desc')->inRandomOrder()
->limit(12)
->get();
return view('pages.home', ['foods' => $foods])
}
If you always want to have $foods in main.blade.php you should place this in a service provider
View::share('foods', Food::orderBy('created_at','desc')->inRandomOrder()
->limit(12)
->get());
https://laravel.com/docs/7.x/views#sharing-data-with-all-views

The result of the code is not displayed in the browser and there is no error in laravel

This is postController.php code:
public function index()
{
$posts = post::orderBy('created_at', 'DESC')->get();
return view('posts.index', compact('posts'));
}
Also I make Route ( web.php ):
Route::resource('post','PostController');
This is view ( index.blade.php ) code:
#section('content')
#foreach($posts as $post)
<div class="panel">
<div class="panel-heading">
<h3>{{ $post -> title }}</h3>
</div>
<div class="panel-body">
{{ $post -> body }}
</div>
</div>
#endforeach
#endsection
After all of that I cannot find the error why my result doesn't display from data base
This tables should be displayed from database
When I try another way the result was displayed u can see that when I write at postController.php code:
public function index()
{
$posts = post::orderBy('created_at', 'DESC')->get();
return $posts;
}
It showed from data base but there is also problem here ! Why the code is not displayed in a neat way in browser
try print post variable on post.index.blade using
{{dd($posts)}}. if your getting $posts variable on blade than everything fines.if not than dd("some meassage") into your index() to check if your index method actually get call.
if your index() method does not print dd("some message") than in terminal type php artisan route:list to check if your routes is prefix with something.

How to update image file in laravel?

I am trying to update an image of a database (One-To-Many and Belongs-To Relationships)
i have table proposition with fields id, proposition.
now the proposition table has relations with image table like this
image module
public function proposition()
{
return $this->belongsTo('App\Proposition');
}
proposition module
public function propositionimage() {
return $this->hasMany('App\PropositionImages');
}
now i want to update the image to an proposition:
i want to get the old value of input file and change it with the new value of input file .
the probleme is whene i use this
$proposition = Proposition::find($num_proposition);
$image = $proposition->propositionimage()->get();
it showing all the image of the proposition
i want to update all the image but one by one , any help plz ?
the HTML code
<img src="/images/{{ $propositonimage->imagename }}" alt="">
<div class="caption">
<input type="file" name="image" value="{{$propositonimage->imagename}}" >
</div>
the update function
public function update(PropositionRequest $request, $num_proposition)
{
$proposition = Proposition::find($num_proposition);
$image = $proposition->propositionimage()->get();
// echo "$image";
$images = Input::file('image');
if (Input::hasfile('image')) {
$rules2 = array('image' => 'mimes:jpeg,bmp,png');
$validator2 = Validator::make($images, $rules2);
if ($validator2->passes()) {
$distinationPath = 'images';
$imagename = $images->getClientOriginalName();
$upload_success = $images->move($distinationPath, $imagename);
$extension = $images->getClientOriginalExtension();
}
}
Im not 100% sure what the problem is. But it seems as you want to iterate the collection over the div?
proposition model
public function propositionimage()
{
return $this->hasMany(PropositionImages::class);
}
image model
public function proposition()
{
return $this->belongsTo(Proposition::class);
}
Then you return a view with proposition in a controller, and eager load PropositionImages?
public function index(){
$propositions = \Proposition::with('propositionimage')->get();
return view('image', compact('propositions'));
}
You will have a collection of proposition with corresponding images.
#forelse ($propositions as $propositon)
<img src="/images/{{ $propositon->imagename }}" alt="">
#if($propositon->propositionimage->count())
<div class="caption">
#foreach($propositon->propositionimage as $propositionimage)
name: {{$propositionimage->imagename}}<br>
<input type="file" name="image" value="{{$propositionimage->imagename}}" >
#endforeach
</div>
#else
No proposition image <br>
#endif
#empty
No images :(
#endforelse
Please add more details to your question otherwise.
Edit
I tried this at my local dev machine and it works.
All relations of propositionimage will be iterated to the corresponding proposition.

How to set null if relations doesn't exist?

In Model I have:
public function publisher()
{
return $this->belongsTo('App\User', 'user_id', 'id');
}
In template blade I try to show data User:
#foreach($announcement->offers as $key => $item)
<img src="{{url($item->publisher->photo)}}">
#endforeach
The Problem is that if there is no data in the User table, the program crashes because I can not get the property $item->publisher->photo.
How to fix it?
You can do something like this:
#if (empty($item->publisher))
It's empty
#else
<img src="{{ url($item->publisher->photo) }}">
#endif
Working on memory here, but you could use #forelse/ #empty. These directives can be used if the object your'e iterating over is empty:
#forelse($announcement->offers as $key => $item)
<img src="{{url($item->publisher->photo)}}">
#empty
//default image or message?
#endforelse
https://laravel.com/docs/5.4/blade#loops

Pass sql result from controller to views in laravel

I'm having a problem to get my data from controller into my view, and I was hoping someone could help me.
Here is the code for my controller:
$Info = new Info();
$Info = DB::select('SELECT * FROM 'tblinfo');
and I only echo it in controller because my value results doesn't pass into the view, here's the additional code for my controller:
foreach ($Info as $Infos)
{
echo $Infos->fname;
echo $Infos->gender;
}
$data['result'] = $Infos->fname;
$data['result'] = $Infos->gender;
$this->view = View::make(App::make('web')->makeDeviceVersionPath('.').'.infofolder.infofile', $data);
Here is the code for my views:
#foreach $result as $info
{{ $info->name }}
{{ $info->gender }}
#endforeach
I hope someone could help me to pass my sql result to my views. Thank you! :)
if u have model u dont need to type raw sql code
just do like this
$info= Info::all();
return View('Index.index')->with('info',$info); // use ->with('*',$*) to send data to view
// Index is View folder
// .index is view name
Now in view grab $info data with for each
#foreach($info as $infodata)
<h3>{{ $infodata->name }}</h3>
<h3>{{ $infodata->gender}}</h3>
#endforeach
Adnan's answer is ok.
Op you should really check Eloquent and Response documentation from laravels website.
http://laravel.com/docs/4.2/eloquent
http://laravel.com/docs/4.2/responses

Resources