Illegal string offset Laravel 5.3 - laravel

I'm trying to build a 'breadcrumb' thing dynamically using an array and this pass it to the view
In my controller
//build the breadcrumbs
$breadcrumbs = [
[
'link' => 'test link',
'name' => ''
]
];
return view('home',compact('breadcrumbs'));
the 'home.blade.php' extends the 'master.blade.php' where the
#include('_breadcrumbs')
resides. And here's the contents of '_breadcrumbs.blade.php'
<!-- Page Heading -->
<div class="row">
<div class="col-lg-12">
<ol class="breadcrumb">
<li #if(!isset($breadcrumbs))class="active"#endif>
<i class="fa fa-dashboard"></i> Dashboard
</li>
#if(isset($breadcrumbs))
#foreach($breadcrumbs as $b)
<li>{{$b['name']}}</li>
#endforeach
#endif
</ol>
</div>
</div>
<!-- /.row -->
but it gives me this error
Illegal string offset 'link' (View:
C:\laravel-projects\dinhi_ecommerce\resources\views_breadcrumbs.blade.php)
(View:
C:\laravel-projects\dinhi_ecommerce\resources\views_breadcrumbs.blade.php)\
If I do
dd(var_dump($breadcrumbs));
it gives me the contents of the array 'breadcrumbs' which means it does work in the controller part.
Any ideas, help please?
UPDATE:
I actually have a line where I modify the index 'name' in the array which I forgot to include in my post. Anyway I change it from
$breadcrumbs['name'] = categories::where('cat_id',$id)->first()->cat_name;
to
$breadcrumbs[0]['name'] = categories::where('cat_id',$id)->first()->cat_name;
and now its working :)

Related

Homepage won't show newly added records but other pages do

I'm using a Laravel 5.7 in an old web project last year 2019 but now I encountered a very weird bug.
This is a website that when you add a new game, it will show to the menu list and if it is featured it will show to the homepage content.
this is the table ['game_id', 'name', 'key', 'status', 'order', 'is_featured']
in the header view I put this code:
<ul class="nav-menu">
<li class="{{ ($page == "games" ? 'menu-active' : '') }} submenu dropdown"><a href="/games" >Games</a>
<ul class="dropdown-menu">
#foreach(GetPublishedGames() as $game)
<li class="nav-item">{{$game->name}}</li>
#endforeach
</ul>
</li>
...
calling this helper code GetPublishedGames()
function GetPublishedGames(){
$data = DB::table("game")->where("status", 1)->orderBy("order")->get();
return $data;
}
in the homepage controller I had this line
"feat_games" => Game::where("status", 1)->where("is_featured", 1)->orderBy("order")->limit(5)->get(),
that calls in the homepage view:
#if(count($feat_games))
#foreach($feat_games as $feat_game)
<div class="feat-game-item wow fadeInUp">
... some content ...
</div>
#endforeach
#endif
I have no idea why this happening, it works before in the local and development stage. I did tried to clear cache.
any idea?

in laravel error Invalid argument supplied for foreach() (View: i cant see $value

I am trying to make a blog foreach that see this erro
Invalid argument supplied for foreach() (View:
my site https://weadam.com/
my code is:
controller:
public function index()
{
$localetmp = Session::get('locale');
$blogs = #json_decode(file_get_contents("https://www.weadam.com/blogs/wp-json/wp/v2/posts?per_page=4&lang=" . $localetmp));
$blogsold = Blog::orderBy('id','desc')->take(100)->get();
foreach ((array)$blogs as $blog){
// $blog->elapsed = $this->time_elapsed_string('#'.$blog->date);
// $blog->date = date("d F Y",$blog->date);
}
$error = "";
return view('home-new',['blogs' => $blogs]);
}
and view is:
<div class="eng-home-blog">
<div class="col-sm-9 home-post-disp bx-shadow brd-radius bg-white">
#foreach($blogs as $blog)
#if($blog->sticky)
<img src="{{$blog->fimg_url}}" alt="" class="img-responsive brd-radius bx-shadow">
<div class="hm-post-date">{{$blog->date}}</div>
<h3>{{$blog->title->rendered}}</h3>
<p>{!!$blog->excerpt->rendered!!}</p>
<span>{{__("READ MORE")}}</span>
#endif
#endforeach
</div>
</div>
pass true as secondary param at json_decode function
$blogs = #json_decode(file_get_contents("https://www.weadam.com/blogs/wp-json/wp/v2/posts?per_page=4&lang=" . $localetmp),true);
now you have to fetch this blog data as associative data. not as an object. here's the example code for it
<div class="eng-home-blog">
<div class="col-sm-9 home-post-disp bx-shadow brd-radius bg-white">
#foreach($blogs as $blog)
#if($blog['sticky'])
<img src="{{$blog['fimg_url']}}" alt="" class="img-responsive brd-radius bx-shadow">
<div class="hm-post-date">{{$blog['date']}}</div>
#endif
#endforeach
</div>
</div>
You can simply use forelse it will take care of the empty case
#forelse($blogs as $blog)
// do what ever you want to do
#empty
<div> Nothing to show </div>
#endforelse
Hope it helps reference.
Thanks.

Laravel custom validation message for multiple fields with the same name

I have the following validation in the controller's action:
foreach ($request['qtys'] as $key => $val){
if (!$this->_validateMinQty($key, $job, $val)){
$customerTitle = $job->customers()->where('customer_id',$key)->first()->title;
return redirect()->back()->withErrors(['qtys' => __('The qty of the customer :customerTitle is less than allowed qty',['customerTitle' => $customerTitle])]);
}
}
This check multiple form's input named qtys in the view:
#foreach($job->customers as $customer)
<div class="form-group {{$errors->first('qtys has-error')}}">
{!! Form::label('qtys-'.$customer->id, __('Qty').' '.$customer->title) !!}
<div class="row">
<div class="col-md-9">
{!! Form::text('qtys['.$customer->id.']',$customer->pivot->e_production,['class' =>'form-control qtys', "data-sumequal"=>"qty",'required' => 'required','title' => $customer->pivot->aid,'id' => 'qtys-'.$customer->id]) !!}
<div class="help-block with-errors"></div>
#php ($eleE = $errors->first('qtys'))
#include('layouts.form-ele-error')
</div>
<div class="col-md-3">
<i class="fox-add"></i>{{__('Add Storage')}}
</div>
</div>
</div>
#endforeach
The above code works, but with the following limitation:
The error message is rendered under every input named qtys[x] where x is an integer and the first input only Testana has the invalid qty, like the following screen shot:
In the controller's action return message, I have tried to use indexed name for the input like the following:
return redirect()->back()->withErrors(['qtys.10' => ....
However, it prevents rendering the error message under any qtys field. Is there any solution?
The solution that I have found starts from the definition of first method found in the view :
#php ($eleE = $errors->first('qtys'))
This, in my code, should be changed to:
#php ($eleE = $errors->first('qtys.'.$customer->id))
Because the multiple fields have gotten keys equals to the customer id. This is a technique I usually use, when I want to send double piece of data in single post or in single form element.
Then in the controller, I keep the first try,
return redirect()->back()->withErrors(['qtys.'.$key => __('The qty of the customer :customerTitle is less than allowed qty',['customerTitle' => $customerTitle])]);
Where $key is an integer.

Symfony DomCrawler can't get attribute

For this div:
<section class='menu'>
<div class="some beautiful classes" link="/some/path/in/web">
<div class="contImgHome">
<img src="/images/icon.jpg alt="">
</div>
<h5>
Go to Section
</h5>
</div>
<div class="some beautiful classes" link="/another/path/in/web">
<div class="contImgHome">
<img src="/images/icon.jpg alt="">
</div>
<h5>
Go to another section
</h5>
</div>
</section>
I make:
$response = $guzzleClient->request('GET', $url_base);
$crawler = new Crawler( (string) $response->getBody() );
$crawler->filter('section.menu > div')->each(function( Crawler $div, $i )
{
$xx = $div->extract( [ 'class', 'link'] );
print_r( $xx );
echo PHP_EOL;
die;
});
And return this:
Array
(
[0] => Array
(
[0] => some beautiful classes
[1] =>
)
)
I also tried with:
$div->attr('classes');
$div->attr('link');
But 'link' attribute is always empty. ¿Why I cant get "link" attribute?
I'm using Laravel 5.2 and installed Symfony Crawler vía composer.
It was my mistake!
Web set that "link" attribute with javascript using Document Ready, so attr is not existing when Crawler gets page DOM.

laravel 5.4 undefined variable in view

I'm kind of new to laravel and trying to pull data from a database but keep hitting an error page saying there's an undefined variable within the view.
I'm supposing there's something I'm doing wrong (or not doing) within the controller. So I have a create.blade.php that posts data from a form to the database and everything works fine, however I want it such that after posting to the database to redirect to the show.blade.php showing the uploaded data and here is where I hit a brick wall with the error messages.
This is my store function within the controller -
public function store(Request $request)
{
$data4page = new Data4page;
$data4page->h1Ttle = $request->h1Ttle;
$data4page->h4Ttle = $request->h4Ttle;
$data4page->description = $request->description;
$data4page->save();
/*redirecting to the content's show page*/
return redirect()->route('content.show', $data4page->id);
}
And this is the show function within the same controller -
public function show($id)
{
//
$data4page = Data4page::find($id);
return view('digital.show')->with(compact('data4page'));
}
The error message I'm getting is -
Undefined variable: data4page (View: /var/www/html/theproject/resources/views/content/show.blade.php)
And in the show.blade.php this is a sample of what I have -
<h1>{{ $data4page->h4Ttle }}</h1>
<p>{{ $data4page->description }}</p>
<h4>{{ $data4page->h4Ttle }}</h4>
Having said all that, I have also tried various means of returning the view within the show function of the controller but all these result in the same error message above. These variants include -
return view('content.show')->with('data4page', $data4page);
return view('content.show',['data4page'=>$data4page]);
Update: I earlier had typos but now the error I get is -
undefined variable: {"id":9,"h1Ttle":"h1 sample text","h4Ttle":"h4 sample text","description":"body text goes here"} (View: /var/www/html/theproject/resources/views/content/show.blade.‌​‌​php)
This is the show.blade.php -
#extends('main')
#section('title')
#section('content')
<div class="app-template">
<main class="wrapperMain">
<div class="">
<aside class="navSidebar">
<nav class="navProperty">
<h1 class="navProperty-title">{{ $data4page->h1Ttle }}</h1>
</nav>
<footer class="footerProperty">
<div class="upcomingSchedule">
<h4 class="upcomingSchedule-heading">{{ $data4page->h4Ttle }}</h4>
</div>
</footer>
</aside>
</div>
<div class="content content--withSidebar">
<div class="lead--Home">
<p>{{ $data4page->description }}</p>
</div>
</div>
</main>
#endsection
You have a name mismatch.
return view('content.show')->with(['name' => $data4page]);
Here you should use:
<h1>{{ $name->h4Ttle }}</h1>
As you called it name
In your other options you defined Data4page and in your view you use data4page (lower case d).
Changed this
return view('content.show')->with(['name' => $data4page]);
To:-
return view('content.show')->with(compact('data4page'));
Hope it hepls!
Why have you resorted to typing the variable name with the wrong case when the answers given below are absolutely fine? PHP variables are case sensitive. You're passing $Data4page to your view in the code you've shown and then trying to use the access $data4page. Any of the below will work.
return view('content.show', ['data4page' => $data4page]);
return view('content.show', compact('data4page'));
return view('content.show')->with('data4page', $data4page);
return view('content.show')->with(['data4page' => $data4page]);
return view('content.show')->with(compact('data4page'));
You need to loop through the collection to get the value
#section('content')
<div class="app-template">
<main class="wrapperMain">
#foreach ($data4page as $d4p)
<div class="">
<aside class="navSidebar">
<nav class="navProperty">
<h1 class="navProperty-title">{{ $d4p->h1Ttle }}</h1>
</nav>
<footer class="footerProperty">
<div class="upcomingSchedule">
<h4 class="upcomingSchedule-heading">{{ $d4p->h4Ttle }}</h4>
</div>
</footer>
</aside>
</div>
<div class="content content--withSidebar">
<div class="lead--Home">
<p>{{ $d4p->description }}</p>
</div>
</div>
#endforeach
</main>
#endsection

Resources