我正在尝试将帐户表中的id变量传递到app.blade视图,以便为每个用户指定自定义URL。我试图将它存储在$ account中,但我显然没有做正确的事情。
错误消息:未定义的变量:account(查看:/home/vagrant/www/upsapp/resources/views/layouts/app.blade.php)(查看:/ home / vagrant / www / upsapp / resources / views /布局/ app.blade.php)
app.blade
<a class="navbar-brand" href=" {{ url('/account/'.$account->id).'/' }}">
{{ $account->accountname}}
</a>
原始AccountController
<?php
namespace App\Http\Controllers;
use App\Account;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Auth;
class AccountController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
$result = Auth::user()->account()->get();
if(!$result->isEmpty()){
return view('account.dashboard',['account'=>$result,'image'=>Auth::user()]);
}
else{
return view('account.dashboard',['account'=>false,'image'=>Auth::user()]);
}
}
更新了AccountController
public function index()
{
$result = Auth::user()->account()->get();
if(!$result->isEmpty()){
return view(layouts.app,['account'=>$result,'image'=>Auth::user()]);
}
else{
return view(layouts.app,['account'=>false,'image'=>Auth::user()]);
}
}
Account.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Account extends Model
{
protected $table = 'accounts';
protected $fillable = ['id', 'accountname', 'address1', 'address2', 'city', 'state', 'zip', 'accountalpha', 'accountnotes'];
public function account()
{
return $this->hasMany('App\User');
}
}
路线
Route::get('/', function () {
return redirect('/login');
});
Auth::routes();
Route::resource('account','AccountController');
Route::get('/home', 'HomeController@index');
'帐户'表
Schema::create('accounts', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->string('accountname');
$table->string('accountalpha')->nullable();
$table->string('address1');
$table->string('address2');
$table->string('city');
$table->string('state');
$table->integer('zip');
$table->string('accountnotes')->nullable();
$table->timestamps();
});
'用户'表
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('firstname');
$table->string('lastname');
$table->string('email')->unique();
$table->string('phonenumber');
$table->string('password');
$table->rememberToken();
$table->timestamps();
$table->integer('account_id');
$table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade');