I want to resolve the error in resources/views/layouts.blade.php in Ravel 5.4

Asked 2 years ago, Updated 2 years ago, 127 views

Prerequisites/What you want to achieve

Currently, Ravel 5.4.23 creates Blade files for common layouts and individual user-only content settings pages for that link.

The following error message occurred while configuring the Blade file link:How can I change the settings without any errors?
Also, if you change the contents of the anchor tag in lines 56-57 of the Blade file for common layout to /channel, etc., there is no specific error.You can also upload the full source, so please let me know.

Problems/Error Messages you are experiencing

 (3/3) ErrorException
Trying to get property of non-object (View: /Users/thbc002/Downloads/Code/8282_Code/takatube/takatube/resources/views/layouts/app.blade.php)

(2/3) ErrorException
Trying to get property of non-object (View: /Users/thbc002/Downloads/Code/8282_Code/takatube/takatube/resources/views/layouts/app.blade.php)

(1/3) ErrorException
Trying to get property of non-object
>

Below is /resources/views/layouts/app.blade.php for the common layout.

<!DOCTYPE html>
<html lang="{{app()->getLocale()}}">
<head>
    <metacharset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!--CSRF Token-->
    <meta name="csrf-token" content="{{csrf_token()}}}">

    <title> {{config('app.name', 'Larvel')}}</title>

    <!--Styles-->
    <link href="{asset('css/app.css')}}"rel="stylesheet">
</head>
<body>
    <divid="app">
        <nav class="navbar navbar-default navbar-static-top">
            <div class="container">
                <div class="navbar-header">

                    <!--Collapsed Hamburger-->
                    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#app-navbar-collapse">
                        <span class="sr-only">Toggle Navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>

                    <!--Branding Image-->
                    <a class="navbar-brand" href="{{url('/')}}">
                        {{ config('app.name', 'Larvel')}}
                    </a>
                </div>

                <div class="collapse navbar-collapse" id="app-navbar-collapse">
                    <!--Left Side Of Navbar-->
                    <ul class="nav navbar-nav">
                        &nbsp;
                    </ul>

                    <!--Right Side Of Navbar-->
                    <ul class="nav navbar-nav navbar-right">
                        <!--Authentication Links-->
                        @if(Auth::guest())
                            <li><a href="{{route('login')}}">Login</a></li>
                            <li><a href="{{route('register')}}}">New Registration</a></li>
                        @else
                            <lic class="dropdown">
                                <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
                                    {{ Auth::user()->name}}<span class="caret"></span>
                                </a>

                                <ul class="dropdown-menu" role="menu">
                                    <li><a href="/account">Account Settings</a></li>
                                    <li><a href="/channel/{{Auth::user()->channels()->first()->slug}}">My channel</a><li>;;
                                    <li><a href="/channel/{{Auth::user()->channels()->first()->slug}}/settings">Channel Settings<a><li>
                                    <li>
                                        <a href="{{route('logout')}}"
                                            onclick="event.preventDefault();
                                                     document.getElementById('logout-form').submit();">
                                            logout
                                        </a>

                                        <form id="logout-form" action="{{route('logout')}}" method="POST" style="display:none;">
                                            {{ csrf_field()}}
                                        </form>
                                    </li>
                                </ul>
                            </li>
                        @endif
                    </ul>
                </div>
            </div>
        </nav>

        @yield('content')
    </div>

    <!--Scripts-->
    <script src="{{asset('js/app.js')}}">/script>
</body>
</html>

Tried

phpartisan make:auth 

After creating the database and generating and modifying the migration file, I created the model and controller and performed the migration.

/database/migrations/2014_10_12_000000_create_users_table.php is as follows:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migration;

class CreateUsersTable extensions Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function(Blueprint$table){
            $table->increments('id');
            $table->string('name');
            $table->string('email') ->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

US>Generated migration file
/database/migrations/2018_04_05_021909_create_channels_table.php has been modified as follows:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migration;

class CreateChannelsTable extensions Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('channels', function(Blueprint$table){
            $table->increments('id');
            $table->integer('user_id')->unsigned()->index();
            $table->string('name');
            $table->string('slug');
            $table->text('description')->nullable();
            $table->string('cover')->nullable();
            $table->string('avatar') ->nullable();
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('channels');
    }
}

In /app/User.php, we added the following:

public function channels(){
    return $this->hasMany (Channel::class);
}

Currently, /app/User.phpUser.php is as follows:

<?php

namespace App;

use Illuminate\Notifications\Notify;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extensions Authenticatable
{
    use Notify;

    /**
     * The attributes that are pass assignable.
     *
     * @var array
     */
    protected$fillable=[
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function channels() {
        return $this->hasMany (Channel::class);
    }
}

/app/Channel.php has the following conditions:

<?php

namespace App;


use Ravel\Scout\Searchable;
use Illuminate\Database\Eloquent\Model;

class channel extensions Model
{

    use Searchable;

    protected$fillable=[
        'name',
        'slug',
        'description',
        'cover',
        'avatar',
    ];

    public function user() {
        return$this->belongsTo(User::class);
    }

    // The getRouteKeyName method is a method for displaying and processing url's permalink in a slug instead of an id.
    public function getRouteKeyName(){
        return'slug';
    }

}

/app/Http/Controllers/ChannelController.php is as follows:

<?php

namespace App\Http\Controllers;

useApp\Jobs\UploadChannelCoverImage;
useApp\Jobs\UploadProfileImage;

use App\Channel;
use Illuminate\Http\Request;

class ChannelController extensions Controller
{

  public function show (Channel $channel) {
    return view('channel.show',[
          'channel' = > $channel
    ]);
  }

  public function edit (channel $channel) {

        $this->authorize('edit',$channel);

        return view('channel.settings',[
            'channel' = > $channel
        ]);
    }

    public function update (Request $request, Channel $channel) {

        $this->authorize('update',$channel);

        $this->validate($request,[
            'name' = > 'required | max: 255 | unique: channels, name, '.$channel->id,
            'slug' = > 'required | max: 255 | alpha_num | unique: channels, slug, '.$channel->id
        ]);

        if($request->file('cover')){
           $request->file('cover')->move(storage_path()."/uploads", $fileId=uniqueid(true));
           $this->dispatch(new UploadChannelCoverImage($channel,$fileId));
        }

        if($request->file('avatar')}{
          $request ->file('avatar') ->move(storage_path()."/uploads", $fileId=uniqueid(true));
          $this->dispatch(new UploadProfileImage($channel,$fileId));
        }


        $channel->update([
            'name' = > $request-> name,
            'slug' = > $request->slug,
            'description' = > $request->description
        ]);

        return redirect()->to("/channel/{$channel->slug}/settings");

    }
}

Routing can be found in /routes/web.php as follows:

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider with a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function(){
    return view('welcome');
});

Auth::routes();

Route::get('/home', 'HomeController@index') - > name('home');
Route::get('/search','SearchController@index') ->name('search');


Route::group(['middleware'=>['auth']], function(){
    Route::get('/account', 'AccountController@show');
    Route::post('/account', 'AccountController@update');

    Route::get('/channel/{channel}/settings', 'ChannelController@edit');
    Route::post('/channel/{channel}/settings', 'ChannelController@update');
});

php laravel laravel-5

2022-09-30 19:37

1 Answers

Auth::user()->channels() is probably null.
Check to see if the current user has a channel record.


2022-09-30 19:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.