Cố gắng lấy thuộc tính 'id' của laravel 8 không phải đối tượng

Tôi đã in kết quả của tên tuyến đường, nó chỉ trả về ba tên trong khi tôi có 4 mảng xe buýt, mảng xe buýt cuối cùng có cùng route_id với mảng xe buýt đầu tiên nhưng nó không trả về route_name gây ra lỗi này cho tôi vì nó không có giá trị nhưng

đây là hai bảng >>

Schema::create['routes', function [Blueprint $table] {
            $table->id[];
            $table->string['route_name'];
            $table->string['route_code'];
            
            $table->timestamps[];
        }];
Schema::create['buses', function [Blueprint $table] {
            $table->id[];
            $table->string['bus_code'];
            $table->foreignId['route_id']->constrained['routes']->onUpdate['cascade']->onDelete['cascade'];;
            $table->integer['total_seats'];
            $table->enum['status', ['avaliable', 'not_avaliable']];
            $table->timestamps[];
        }];

và đây là bộ điều khiển

và đây là các mô hình>>

và đây là lưỡi kiếm >>

@foreach[$buses as $bus]
   
     {{$bus->routes->route_name}}
   
@endforeach

Thêm giải pháp của bạn ở đây

 B   I   U   S  small BIG code var     &  link [^] encode untab case indent outdent

Xem trước 0

thành viên hiện tại

hoặc tham gia với chúng tôi

Tải xuống, Bình chọn, Nhận xét, Xuất bản

Email của bạn

Email này đang được sử dụng. Bạn có cần mật khẩu của bạn?

Mật khẩu tùy chọn

Khi trả lời câu hỏi, vui lòng.

  1. Đọc kỹ câu hỏi.
  2. Hiểu rằng tiếng Anh không phải là ngôn ngữ đầu tiên của mọi người, vì vậy hãy khoan dung với lỗi chính tả và ngữ pháp
  3. Nếu một câu hỏi được diễn đạt kém thì hãy yêu cầu làm rõ, bỏ qua nó hoặc chỉnh sửa câu hỏi và khắc phục sự cố. Xúc phạm không được chào đón
  4. Đừng bảo ai đó đọc hướng dẫn. Rất có thể họ có và không nhận được. Đưa ra câu trả lời hoặc chuyển sang câu hỏi tiếp theo
Let's work to help developers, not make them feel stupid.


Nội dung này, cùng với bất kỳ tệp và mã nguồn liên quan nào, được cấp phép theo Giấy phép Mở Dự án Code [CPOL]

Laravel - Đang cố lấy thuộc tính 'id' của đối tượng không

Lỗi là do $this->client không có giá trị khi 
@foreach[$buses as $bus]
   
     {{$bus->routes->route_name}}
   
@endforeach
0 không thể tìm thấy bản ghi

Bạn cần chắc chắn nếu bản ghi tồn tại hay không

Biến đổi

$this->client = Client::find[1];

Đến

$this->client = Client::findOrFail[1];

Nếu bạn làm việc với hoặc lặp trong laravel [for, foreach, v.v. ] hiển thị truy cập dữ liệu để lấy thuộc tính try to get của non-object laravel hoặc bất kỳ loại mối quan hệ nào như a [one to many, many to many, v.v. ], điều này có thể có nghĩa là một trong các truy vấn Cơ sở dữ liệu đang trả về biến dữ liệu null hoặc thành viên mối quan hệ null laravel 5/6/7

Truy vấn của bạn có trả về mảng hoặc đối tượng không?

Đây là mô hình người dùng

 'int'
	];

	protected $dates = [
		'last_login'
	];

	protected $hidden = [
		'password',
		'remember_token'
	];

	protected $fillable = [
		'user_type',
		'username',
		'password',
		'last_login',
		'remember_token',
		'settings'
	];
	public function toString[]
	{
		return "hello";
	}

	public function customer[]
	{
		return $this->hasOne['App\Models\Customer'];
	}

	public function merchant[]
	{
		return $this->hasOne['App\Models\Merchant'];
	}

	public function admin[]{

		return $this->hasOne['App\Models\Admin'];
	}

 

	
}

Đây là Mô hình thương gia

 'int',
		'status' => 'int'
	];

	protected $fillable = [
		'user_id',
		'store_name',
		'store_name_url',
		'title',
		'firstname',
		'lastname',
		'bvn_mobile_no',
		'street',
		'city',
		'state',
		'country',
		'status'
	];

	public function user[]{

		return $this->belongsTo['App\Models\User'];
	}

	public function name[]{
		return $this->firstname . ' ' . $this->lastname;
	}
    
    public function service[]{
    	return $this->hasMany['App\Models\Service'];
    }
}

Bảng thương gia có cột user_id và mô hình trên xác định mối quan hệ

Tôi đang làm việc với mã của người khác và khi tôi triển khai ứng dụng laravel, trang đăng nhập hoạt động nhưng khi tôi nhập thông tin xác thực kiểm tra thì nó báo lỗi này

    Trying to get property 'id' of non-object
    in helpers.php line 159
    at HandleExceptions->handleError[8, 'Trying to get property \'id\' of non-object', '/var/www/html/first-project/app/Helpers/helpers.php', 159, array['fields' => object[Collection], 'fieldsValues' => object[Collection], 'htmlFields' => array[], 'startSeparator' => '

', 'endSeparator' => '

', 'field' => object[CustomField], 'dynamicVars' => array['$RANDOM_VARIABLE$' => 'var15931958241638660037ble', '$FIELD_NAME$' => 'phone', '$DISABLED$' => '', '$REQUIRED$' => '"required" => "required",', '$MODEL_NAME_SNAKE$' => 'user', '$FIELD_VALUE$' => '\'+136 226 5660\'', '$INPUT_ARR_SELECTED$' => '+136 226 5660'], 'gf' => object[GeneratorField], 'value' => object[CustomFieldValue]]] in helpers.php line 159

Chức năng thực tế được gọi là như sau

function generateCustomField[$fields, $fieldsValues = null]
{
    $htmlFields = [];
    $startSeparator = '

'; $endSeparator = '

'; foreach [$fields as $field] { $dynamicVars = [ '$RANDOM_VARIABLE$' => 'var' . time[] . rand[] . 'ble', '$FIELD_NAME$' => $field->name, '$DISABLED$' => $field->disabled === true ? '"disabled" => "disabled",' : '', '$REQUIRED$' => $field->required === true ? '"required" => "required",' : '', '$MODEL_NAME_SNAKE$' => getOnlyClassName[$field->custom_field_model], '$FIELD_VALUE$' => 'null', '$INPUT_ARR_SELECTED$' => '[]', ]; $gf = new GeneratorField[]; if [$fieldsValues] { foreach [$fieldsValues as $value] { if [$field->id === $value->customField->id] { $dynamicVars['$INPUT_ARR_SELECTED$'] = $value->value ? $value->value : '[]'; $dynamicVars['$FIELD_VALUE$'] = '\'' . addslashes[$value->value] . '\''; $gf->validations[] = $value->value; continue; } } } // dd[$gf->validations]; $gf->htmlType = $field['type']; $gf->htmlValues = $field['values']; $gf->dbInput = ''; if [$field['type'] === 'selects'] { $gf->htmlType = 'select'; $gf->dbInput = 'hidden,mtm'; } $fieldTemplate = HTMLFieldGenerator::generateCustomFieldHTML[$gf, config['infyom.laravel_generator.templates', 'adminlte-templates']]; if [!empty[$fieldTemplate]] { foreach [$dynamicVars as $variable => $value] { $fieldTemplate = str_replace[$variable, $value, $fieldTemplate]; } $htmlFields[] = $fieldTemplate; } // dd[$fieldTemplate]; } foreach [$htmlFields as $index => $field] { if [round[count[$htmlFields] / 2] == $index + 1] { $htmlFields[$index] = $htmlFields[$index] . "\n" . $endSeparator . "\n" . $startSeparator; } } $htmlFieldsString = implode["\n\n", $htmlFields]; $htmlFieldsString = $startSeparator . "\n" . $htmlFieldsString . "\n" . $endSeparator; // dd[$htmlFieldsString]; $renderedHtml = ""; try { $renderedHtml = render[Blade::compileString[$htmlFieldsString]]; // dd[$renderedHtml]; } catch [FatalThrowableError $e] { } return $renderedHtml; }

cách sử dụng của nó như sau trong bộ điều khiển. Nó được sử dụng nhiều lần trong hầu hết các bộ điều khiển, ví dụ như trong UserController. php tôi nghĩ đây là phương thức gọi. Tôi không thành thạo lắm về laravel, xin lỗi vì bất kỳ lỗi nào của người mới

Chủ Đề