返回视图:未定义的变量Laravel

时间:2018-06-05 10:39:31

标签: php html sql laravel

ERORR未定义变量:列表

用户创建待售广告列表:

    $listing->user_id = auth()->user()->id;
    $listing->email= auth()->user()->email;
    $listing->phone_number= auth()->user()->phone_number;
    $listing->package = $request->session()->get('package');
    $listing->save();
    return view('user.dashboard');

将它们转移到仪表板页面后

@if($listings->isEmpty())
<div class="padding-top"> 
    <h1 class="centre">None</h1>
</div>
@else

@endif

@foreach ($listings as $listing)

<div class="row">

  <div class="container">
    <div class="card">
      <div class="card-body row">
        <h5 class="card-title cardtitle col-lg-12 centre">{{$listing->address}}</h5>

不完整的HTML,但只是一个提取

编辑添加完整控制器使用返回视图(&#39; user.dashboard&#39;,[&#39; listing&#39; =&gt; $ listing]);仍然会抛出错误

 public function store(Request $request)
    {

        $this->validate($request, [
            'image1' => 'image|nullable|max:1999',
            'image2' => 'image|nullable|max:1999'
        ]);

        // Handle File Upload
        if($request->hasFile('image1')){
            // Get filename with the extension
            $filenameWithExt = $request->file('image1')->getClientOriginalName();
            // Get just filename
            $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
            // Get just ext
            $extension = $request->file('image1')->getClientOriginalExtension();
            // Filename to store
            $fileNameToStore= $filename.'_'.time().'.'.$extension;
            // Upload Image
            $path = $request->file('image1')->storeAs('public/cover_images', $fileNameToStore);
        } else {
            $fileNameToStore = 'noimage.jpg';
        }

        $listing = new Listings;
        $listing->user_id = auth()->user()->id;
        $listing->email= auth()->user()->email;
        $listing->phone_number= auth()->user()->phone_number;
        $listing->package = $request->session()->get('package');
        $listing->save();
        $listing->image1 = $fileNameToStore;
        return view('user.dashboard', ['listings' => $listing]);

}

3 个答案:

答案 0 :(得分:1)

您需要将$listing传递给您的观点,如下所示:

return view('user.dashboard', compact('listing');

编辑:

当然,您的变量必须在控制器和视图中命名相同。您现在在控制器中为变量$listing命名时会出现不匹配,但在视图中将其称为$listings。你的代码应该是这样的:

$listings->user_id = auth()->user()->id;
$listings->email= auth()->user()->email;
$listings->phone_number= auth()->user()->phone_number;
$listings->package = $request->session()->get('package');
$listings->save();
return view('user.dashboard', compact('listings');

然后,您可以在视图中将变量调用为$listings,这样您的视图代码就不必更改了。

编辑2:

$listingListings模型的一个实例。因此,您无法在对象上调用empty()方法。我想你想做这样的事情:

$listing = new Listings;
$listing->user_id = auth()->user()->id;
$listing->email= auth()->user()->email;
$listing->phone_number= auth()->user()->phone_number;
$listing->package = $request->session()->get('package');
$listing->save();
$listing->image1 = $fileNameToStore;
$listings = Listings::all();
return view('user.dashboard', compact('listings'));

这对你有用吗?

聚苯乙烯。是否要在保存后设置image1属性?这会迷路。

答案 1 :(得分:1)

更改

return view('user.dashboard');

return view('user.dashboard', compact('listing'));

我认为原因非常清楚。

答案 2 :(得分:0)

我认为您不使用命名空间在控制器中导入模型。 尝试检查使用&#34;使用auth&#34;命名空间

use App\Listing_Model;
use auth;

class DashboardController extends Controller {
    public function index() {


    $listing = new Listing_Model; 
    $listing->user_id = auth()->user()->id;
    $listing->email= auth()->user()->email;
    $listing->phone_number= auth()->user()->phone_number;
    $listing->package = $request->session()->get('package');
    $listing->save();
    return view('user.dashboard');

    }
}