Why do I keep getting 'Undefined variable: charities' in Laravel 5.4? - php

I keep getting this undefined variable error. I am trying to display all the charities to the users, I am doing the exact same function for the 'charityowners' type of user and it is working for them.
Route:
Route::get('charities', 'HomeController#displayCharities');
Home Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Auth;
use Image;
use App\Charities;
class HomeController extends Controller
{
public function displayCharities()
{
$charities = Charities::all();
return view('charities', ['charities' => $charities]);
}
}
View:
<table class="table">
<tr>
<td> <strong> Name </strong> </td>
<td> <strong> Image </strong> </td>
<td> <strong> Description </strong> </td>
<td> <strong> Action </strong> </td>
</tr>
#foreach($charities as $charity)
<tr>
<td> {{ $charity->name }} </td>
<td> Not Available </td>
<td> {{ $charity->description }} </td>
</tr>
#endforeach
</table>
The error I keep getting is:
ErrorException in d9249250b321fd33ae875d6ca2417a0420928492.php line 39:
Undefined variable: charities (View: C:\wamp64\www\EasyDonationNew\laravel\resources\views\charities.blade.php)
What am I missing??

You can try this
return view('charities')->with('charities', $charities);

It works now by adding:
#if(isset($ownedCharities))
Before the foreach loop.

Try initializing the variable $charities globally as an empty string or as null.
On the global scope a var is only seen if it is defined.

Related

Two links appears for one user weirdly - Laravel models

I have a many-to-many relationship between my User and Wallet models.
Wallet Model
public function users()
{
return $this->belongsToMany(User::class, 'user_wallet', 'user_id', 'wallet_id');
}
User Model
public function wallets()
{
return $this->belongsToMany(Wallet::class, 'user_wallet', 'user_id', 'wallet_id')->withPivot('balance');;
}
Then in my Blade file, I tried the following.
#forelse($user->wallets as $wallet)
<tr>
<td>
{{ $wallet->name }}
</td>
<td>
{{ $wallet->pivot->balance }}
</td>
<td>
<a href="{{ route('user.WalletTransaction', ['walletId'=>$wallet->id,'userId'=>$user->id]) }}" class="fa fa-exchange text-dark mt-1" />
</td>
</tr>
#empty
<td colspan="5" class="text-center">No wallet exist</td>
#endforelse
It works fine, but the problem is, it shows two links in the table for one user. However, it should be showing one.
And if I do:
#forelse($user->wallets as $wallet)
{{ count(array($user)) }}
#empty
<td colspan="5" class="text-center">
No wallet exist
</td>
#endforelse
The result would be 1.
The problem is coming from 'userId' => $user->id of the link. Because if I remove it, only 1 link would appear, which is correct. But still, I do need the userId and don't know how to pass that into the route name.
So what's going wrong here? I need one link for each user, and I don't know why it prints two of them.
#forelse($user->wallets as $wallet)
<tr>
<td>
{{ $wallet->name }}
</td>
<td>
{{ $wallet->pivot->balance }}
</td>
<td>
<a href="{{ route('user.WalletTransaction', ['walletId'=>$wallet->id,'userId'=>$user->id]) }}" class="fa fa-exchange text-dark mt-1" />
</td>
</tr>
#empty
<td colspan="5" class="text-center">No wallet exist</td>
#endforelse`
Doesn't $user->wallets returns collection of all wallets associated with each user?
maybe you need to check if any user has more than one wallet or if you want to show only one you can user $user->wallets[0] to show the first wallet model since its a collection

Laravel : Passing value from view to controller

My View:
<table border="1" style="text-align: left;">
<td>
<b>Firstname</b>
</td>
<td>
<b>Actions</b>
</td>
#foreach($Newslist as $News)
<tr>
<td>
{{ $News->lastname }}
</td>
<td>
<button type="button" class="btn btn-primary">Edit</button>
</td>
</tr>
#endforeach
</table>
My routes/web.php:
Route::Post('NewsEdit/{{$News->id}}/EditForm','NewsControllere#EditForm');
My Controller.php:
<?php
namespace App\Http\Controllers;
use App\Newsmodel;
use Illuminate\Http\Request;
class NewsControllere extends Controller
{
public function EditForm($NewsId)
{ dd(request()->all());
echo "deepakkeynes";exit();
//$Newsmodel = Newsmodel::find($NewsId);
//return view('/News')->with('News',$Newsmodel);
}
}
While clicking the edit button in the view, the following result is obtained:
Result: 404 page
Expected Result: The Id of the edit button along with the echo value!
Can anyone help me out? I am a newbie in laravel..!
Have a look at this LaraCast tutorial on Route Model Binding. This explains the underlying documentation well.
Essentially:
routes/web.php:
Route::get('NewsEdit/{news}/EditForm','NewsControllere#EditForm');
newsController.php:
public function EditForm(Newsmodel $news)
{
return view('/News')->with('News',$news);
}

how can i fix "Trying to get property of non-object"

i have a favorite list in my website for the users and they can add their favorite house to the wishlist
it goes well but he can not see the wishlist page and an error comes like this:
Trying to get property 'image' of non-object
it's my relations
class Home extends Model
{
protected $guarded = [];
public function favorite()
{
return $this->hasMany(favorite::class,'house_id');
}
}
class favorite extends Model
{
protected $guarded = [];
public function house()
{
return $this->belongsTo(home::class);
}
}
my index function in controller:
public function index()
{
$favorite = favorite::where('user_id',auth()->user()->id)->get();
return view('favorite.index',compact('favorite'));
}
my index:
#foreach($favorite as $fav)
<tr>
<td>
<a href="property-detail.html"><img src="{{$fav->home->image}}" alt=""
width="100"></a>
</td>
<td>{{$fav->home->title}}</td>
<td>خانه خانواده</td>
<td>اجاره</td>
<td>
<div class="price"><span>{{number_format($fav->home->price)}}</span><strong>تومان</strong>
</div>
</td>
<td>
<i class="fa fa-ban"></i> <span>حذف</span>
</td>
</tr>
#endforeach
First thing that your favorite model doesn't have home relation your relation is house and when you want to get its value you can use optional helper function like this:
optional($fav->house)->title
You are trying to access a relationship with a wrong name. You defined the relationship with name house:
public function house(){
return $this->belongsTo(home::class);
}
So you need to use this name to access:
#foreach($favorite as $fav)
<tr>
<td>
<img src="{{$fav->house->image}}" alt="" width="100">
</td>
<td>{{$fav->house->title}}</td>
<td>خانه خانواده</td>
<td>اجاره</td>
<td>
<div class="price"><span>{{number_format($fav->house->price)}}</span><strong>تومان</strong>
</div>
</td>
<td>
<i class="fa fa-ban"></i> <span>حذف</span>
</td>
</tr>
#endforeach
But you will have a problem too if the house relation come null. To avoid you can use the solution proposed by #Mohammed Aktaa:
optional($fav->house)->title

Fecthing the data from database in Laravel 5.1

Good day
I'm facing some errors while trying to fetch data from database
here are my codes
my table name is meeting and I used the formal documentation to write the codes
controllers.php
public function meetingdet()
{
$mretive = \DB::table('meeting')-> $name = $request->input('meetingname');}
view
<table border="1" >
<tr>
<td><b>blah</b> </td>
<td><b>blah</b> </td></tr>
<tr>
<td>
</td>
<td><li>foreach ($name as $retrive) {
echo $retrive-> meeting;}
#endforeach
</li>
</td>
</tr>
</table>
but this what is showing for me
syntax error, unexpected 'endforeach' (T_ENDFOREACH) and whan I remove
endforeach it display for me the for each without fetching the data
By the way I'm working on laravel 5.1
Regards
You can try with the following sample code:
controllers.php
//include your model file
use App\model;
public function meetingdet()
{
$meetingData = model::getMeetingData();
$view = array(
'name'=>$meetingData
);
return view('test', compact('meetingData'));
}
Model.php
public static function getMeetingData()
{
$value=DB::table('meeting')->orderBy('meeting_id', 'asc')->get();
return $value;
}
view.blade.php
<table border="1" >
<tr>
<td><b>blah</b> </td>
<td><b>blah</b> </td></tr>
<tr>
<td>
</td>
<td>
<ul>
<li>
#foreach ($meetingData as $retrive)
{{{ $retrive->meeting_id }}}
#endforeach
</li>
</ul>
</td>
</tr>
</table>
<li>
#foreach ($name as $retrive)
{{ $retrive->meeting }}
#endforeach
</li>
you didn't add # to foreach..

I cannot get value from an object in blade template Laravel 4

I have a loop foreach in a blade template where i print data from a specific model, the problem is I am not able of getting the value of "$pedido->proveedor()->first()->name" in the code giveing me this error "ErrorException (E_UNKNOWN) Trying to get property of non-object (View: C:..":
#foreach($pedidos as $pedido)
<tr>
<td>
{{ $pedido->id }}
</td>
<td>
{{ $pedido->proveedor()->first()->name }}
</td>
<td>
{{ date('d/m/Y', $pedido->fecha) }}
</td>
<td>
<a onclick="return confirm('deseas borar este registro?')" class="btn btn-danger btn-xs fullButton">Borrar</a>
</td>
</tr>
#endforeach
The weird thing here is when I code this "$pedido->proveedor()->first()" in side the loop of the template I get an object like this:
{"name":"nombre","domicilio":"domicilio","cp":"46006","poblacion":"poblacion","ciudad":"ciudad","pais":"pais"}
but coding this "$pedido->proveedor()->first()->name" I get the error:
the data is sent from a controller:
public function listPedidos()
{
$pedidos = Pedido::all();
// this next pice of code shows me i can get the name as spected but only from php
// foreach($pedidos as $pedido){
// ddd($pedido->proveedor()->first()->name);exit;
// }
return View::make('pedidos/pedidos-list')->with('pedidos', $pedidos);
}
Another weirder thing is that I have the same code with different model and it is working.
Thanks in advance for any help. ;)
You should use:
{{ $pedido->proveedor->name }}
Problem solved:
the previos answer was ok. I can use:
{{ $pedido->proveedor()->first()->name }}
or
{{ $pedido->proveedor->name }}
but because in one of the way of the loop there was not content to refer to i.e($pedido->proveedor didn't exixt) I use this:
{{ isset($pedido->Proveedor->name )?$pedido->Proveedor->name :''; }}

Categories