Hallo Breadfish Benutzer,
ich hab eine frage ich will ein News System erstellen in Laravel.
da hab nun ein Problem, wenn die die daten von der datenbank abrufe habe ich eine author_id,
will daraus eine authorname also Benutzername der dieses erstellt hat haben.
Kurze Info:
Ich benutze Laravel 5.4
Meine Daten:
User.php:
PHP: User.php
<?php
namespace App;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
protected $table = 'users';
protected $fillable = ['name', 'email', 'password'];
protected $hidden = ['password', 'remember_token'];
protected $dates = ['logged_in_at' , 'logged_out_at'];
public function sendPasswordResetNotification($token)
{
$this->notify(new ResetPassword($token));
}
public function news(){
return $this->hasMany('App\News');
}
}
Alles anzeigen
News.php:
PHP: News.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class News extends Model
{
protected $table = 'news';
protected $fillable = ['author_id', 'title', 'slug', 'body', 'excerpt', 'published_at'];
protected $dates = ['published_at'];
public function user(){
return $this->belongsTo('App\User');
}
}
Alles anzeigen
NewsController.php:
PHP: NewsController.php
namespace App\Http\Controllers;
use App\News;
use App\User;
class NewsController extends Controller
{
protected $news;
public function __construct( News $news)
{
$this->news = $news;
}
public function show(){
$news = News::all();
return view('news.show',compact('news'));
}
}
Alles anzeigen
show.blade.php:
PHP: show.blade.php
@extends('layouts.app')
@section('content')
@foreach($news as $p)
<blockquote>
<div>{{$p->title}}</div>
<div>{{$p->desc}}</div>
<div>
<a href="{{ url('profile/'.$p->author_id) }}">{{$p->name}}</a>
</div>
</blockquote>
@endforeach
@endsection
Alles anzeigen
Bitte um schnelle hilfe.
Mit Freundlichen Grüßen,
DarkEvolution