I have actionView($id) method in my controller. And have 2 models Posts and User and I want to get one posts(with user['name']). I have main gridview a when clicked for item have url like that:
http://localhost/test/basic/site/1
last 1 it's a this $id parameter.
Now my method is:
public function actionView($id)
{
if(Yii::$app->user->isGuest)
{
return $this->redirect(['login']);
}
else
{
$data = Posts::find($id)->joinWith('user');
return $this->render('detail',['data'=>$data]);
}
}
but I have error:
Setting unknown property: yii\widgets\DetailView::0
You have to define the relation in your Posts-Model like this:
public function getUser()
{
return $this->hasOne(User::className(), ['id' => 'user_id']);
}
After that you can get the User with this:
Posts::find($id)->with('user')
You can find it in the documentation:
http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#relational-data
Related
I'm using Laravel 5.8 and I wanted to retrieve some data from an Api route:
Route::prefix('v1')->namespace('Api\v1')->group(function(){
Route::get('/articles','ArticleController#index');;
});
Then I made a Resource Collection and a Controller as well:
ArticleCollection.php:
public function toArray($request)
{
return parent::toArray($request);
}
ArticleController:
public function index()
{
$articles = Article::all();
return new ArticleCollection($articles);
}
Now it properly shows data:
{"data":[{"art_id":3,"art_cat_id":15,"art_author_id":23973,"art_title":"\u0627\u062d\u06cc\u0627\u06cc...
But when I change the Resource Collection to this:
public function toArray($request)
{
return [
'title' => $this->art_title,
'desc' => $this->art_description,
];
}
And trying to find an specific article in the Controller:
public function index()
{
$articles = Article::find(1);
return new ArticleCollection($articles);
}
I will get this error somehow:
FatalThrowableError
Call to a member function toBase() on null
So what's going wrong here? How can I solve this issue?
Change this:
public function index()
{
$articles = Article::find(1);
return new ArticleCollection($articles);
}
to this:
public function index()
{
$articles = Article::where('id',1)->get();
return new ArticleCollection($articles);
}
This is because the find returns just one record and you are converting that to a collection, by using get() you can return a collection as you intend to here.
I have 2 model, SurveyQuestion and SurveyQuestionOption
SurveyQuestionOption Model
public function opinions(){
return $this->hasMany('App\SurveyOpinions','option_id');
}
SurveyQuestion Model
public function options(){
return $this->hasMany('App\SurveyQuestionOption','question_id');
}
Controller
public function getQuestions(){
$data=SurveyQuestion::with('options')
->get();
if(count($data)>0)
{
return api::success(['data' => $data]);
}
return api::notFound(['errorMsg' => 'No Survey initiated yet!!!']);
}
When I hit get Question Api am getting an empty array from the model. I tried that by putting dd() for the $data Variable.
Too few arguments to function App\Http\Controllers\UsersController::edit(), 1 passed and exactly 2 expected
public function edit ($id , User $user) {
$user = $user->find($id);
return view('admin.user.edit',compact('user'));
}
If you url is something like: site.com/profile/1
And if this url corespond the this edit function
// you can just ask from your model to get the user that has id: 1
// which is coming from Eloquent model.
public function edit (User $user) {
return view('admin.user.edit',compact('user'));
}
If your url is not like this also function wouldn't work tho
You should edit you method to the following:
public function edit (int $id , UserRepository $repo) {
$user = $repo->find($id);
return view('admin.user.edit', compact('user'));
}
And make sure at the top of your file you have appropriate namespaces declared.
I have stored my slug in my database but i get 404 Not found when i load the url
NewsController.php
public function show(News $news, $slug)
{
return view('news.single', compact('news'));
}
News.php
protected static function boot() {
parent::boot();
static::creating(function ($news) {
$news->slug = Str::slug($news->subject);
});
}
Route
Route::get('/news/{slug}', 'NewsController#show')->name('news.show');
I am getting 404 not found if load e.g localhost:8000/news/sample-post
The problem is that you are type-hinting News $news in your controller method and Laravel is unable to find the correct object because 1. there's no {news} route parameter and 2. it's looking in the ID column by default.
There are two options to fix it:
1. Manually load the news
public function show($slug)
{
$news = News::where('slug', $slug)->firstOrFail();
return view('news.single', compact('news'));
}
2. Tell Laravel to use the slug column instead of id:
Add this method to your News model:
/**
* Get the route key for the model.
*
* #return string
*/
public function getRouteKeyName()
{
return 'slug';
}
Change the route parameter name:
Route::get('/news/{news}', 'NewsController#show')->name('news.show');
And finally, remove the $slug controller method parameter:
public function show(News $news)
{
return view('news.single', compact('news'));
}
App\Classes
public function enroll()
{
return $this->hasMany(Enrolls::class,'cid');
}
App\Enrolls
public function classes()
{
return $this->belongsTo(Classes::class);
}
controller
public function index()
{
$enrolls = Enrolls::all();
return view('home')->with('enrolls', $enrolls);
}
blade
{{$enroll->classes->title}}
I was trying to get data from enrolls table. this contains two foreign Keys.
SCREENSHOT
You should try this:
public function index()
{
$enrolls = Enrolls::with('classes')->get();
return view('home',compact('enrolls'));
}