I am developing a system similar to what would be a "playstore" for an academic project. In the application there are 2 types of users, a developer user who can make a CRUD of applications for the system and another client user, who can see and list the apps of said developers (both types of users are in the Users table, use the Spatie Roles package to differentiate them and that each one has their access to certain routes, I understand that this is not a good practice, I am in the process of iterations).
I have created the application table migration with the foreign key pointing to the user id inside the "created_by" field. The relationship would be from 1 to N since 1 developer can be related to several applications, and an application is related to 1 developer.
Schema::create('applications', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('category');
$table->string('description');
$table->integer('price');
$table->text('logo_url');
$table->timestamps();
$table->engine = 'InnoDB';
});
Here I define the foreign key in this migration:
Schema::table('applications', function (Blueprint $table) {
$table->unsignedBigInteger('created_by');
$table->foreign('created_by')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
});
Now I define the relations, in the User model:
public function apps() {
return $this->hasMany(Application::class,'created_by');
}
And in the Application model:
public function developer() {
return $this->belongsTo(User::class,'created_by');
}
Now, when the user logs in as a developer, he has to be redirected to a page where he can see the applications he created, to perform the corresponding CRUD, and at this point is where I basically have my problem ., I don't know if I'm using eloquent correctly (almost certainly not xd), but I made different tests that lead to the same undesired result. What I am looking for is to send to the view of the developer user, the applications that are related to their ID, which therefore will be the value of the "created_by" field of the previously mentioned applications table. And if there is no application for this developer, display a custom message with a button that points to the corresponding path to create your first application (although the latter is perhaps not as relevant).
At the moment what I have in the controller is this:
class DashboardController extends Controller
{
public function index(User $dev) {
$appsDev = $dev::has('apps')->get();
return view('dev.dashboard',compact('appsDev'));
}
}
However, when doing a print_r of the appsDev variable on the view, I run into this monstrosity:
App\Models\User Object
(
[fillable:protected] => Array
(
[0] => name
[1] => surname
[2] => alias
[3] => email
[4] => password
)
[hidden:protected] => Array
(
[0] => password
[1] => remember_token
[2] => two_factor_recovery_codes
[3] => two_factor_secret
)
[casts:protected] => Array
(
[email_verified_at] => datetime
)
[connection:protected] => mysql
[table:protected] => users
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[id] => 3
[name] => Esteban
[surname] => Perez
[alias] => Arrow
[email] => [email protected]
[password] => testing1234
[two_factor_secret] =>
[two_factor_recovery_codes] =>
[remember_token] =>
[created_at] => 2021-03-15 03:46:24
[updated_at] => 2021-03-15 03:46:24
)
[original:protected] => Array
(
[id] => 3
[name] => Esteban
[surname] => Perez
[alias] => Arrow
[email] => [email protected]
[password] => testing1234
[two_factor_secret] =>
[two_factor_recovery_codes] =>
[remember_token] =>
[created_at] => 2021-03-15 03:46:24
[updated_at] => 2021-03-15 03:46:24
)
[changes:protected] => Array
(
)
[classCastCache:protected] => Array
(
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[dispatchesEvents:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
)
[touches:protected] => Array
(
)
[timestamps] => 1
[visible:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
It should be clarified that I did some inserts to see what this returned to me, I was reading several articles and looking at tutorials but I haven't found any solution at the moment, how can I obtain the records of the applications corresponding to the logged in user?
Did you try using
with
? With that query, what you do is return the users that have apps, as I understand it.I think if you want to see if the user has apps, you should do something like this
return auth()->user()->apps;
If your structure is well done, if there is data in that user with that relationship, it should show you the data, otherwise the result will be null. So in your view, you can evaluate something like this;